Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): [prefer-nullish-coalescing] supportif
statement assignment (??=
) and fix several minor bugs#10861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
fcc9bea
4e3a723
185667d
f69b588
ed7be4c
2db7af7
e9b280a
fc478dc
5e59468
54e0f50
8e1a6a6
1e10987
168a228
4440b11
50bf59e
92ef701
727109a
7035591
fc806c5
8858559
271db29
513b085
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
OlivierZal marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -17,6 +17,54 @@ This rule reports when you may consider replacing: | ||
- An `||` operator with `??` | ||
- An `||=` operator with `??=` | ||
- Ternary expressions (`?:`) that are equivalent to `||` or `??` with `??` | ||
- Assignment expressions (`=`) that can be safely replaced by `??=` | ||
## Examples | ||
OlivierZal marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
<Tabs> | ||
<TabItem value="❌ Incorrect"> | ||
```ts | ||
declare const a: string | null; | ||
declare const b: string | null; | ||
const c = a || b; | ||
declare let foo: { a: string } | null; | ||
declare function makeFoo(): { a: string }; | ||
function lazyInitializeFooByTruthiness() { | ||
if (!foo) { | ||
foo = makeFoo(); | ||
} | ||
} | ||
function lazyInitializeFooByNullCheck() { | ||
if (foo == null) { | ||
foo = makeFoo(); | ||
} | ||
} | ||
``` | ||
</TabItem> | ||
<TabItem value="✅ Correct"> | ||
```ts | ||
declare const a: string | null; | ||
declare const b: string | null; | ||
const c = a ?? b; | ||
declare let foo: { a: string } | null; | ||
declare function makeFoo(): { a: string }; | ||
function lazyInitializeFoo() { | ||
foo ??= makeFoo(); | ||
} | ||
``` | ||
</TabItem> | ||
</Tabs> | ||
:::caution | ||
This rule will not work as expected if [`strictNullChecks`](https://www.typescriptlang.org/tsconfig#strictNullChecks) is not enabled. | ||
@@ -255,3 +303,4 @@ If you are not using TypeScript 3.7 (or greater), then you will not be able to u | ||
- [TypeScript 3.7 Release Notes](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html) | ||
- [Nullish Coalescing Operator Proposal](https://github.com/tc39/proposal-nullish-coalescing/) | ||
- [`logical-assignment-operators`](https://eslint.org/docs/latest/rules/logical-assignment-operators) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -203,13 +203,11 @@ export default createRule<Options, MessageIds>({ | ||
// Cache the first encountered exports for the package. We will need to come | ||
// back to these later when fixing the problems. | ||
if (node.exportKind === 'type') { | ||
// The export is a type export | ||
sourceExports.typeOnlyNamedExport ??= node; | ||
OlivierZal marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} else { | ||
// The export is a value export | ||
sourceExports.valueOnlyNamedExport??= node; | ||
OlivierZal marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
// Next for the current export, we will separate type/value specifiers. | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -80,9 +80,7 @@ export default createRule({ | ||
ts.isArrowFunction(functionTSNode) | ||
? getContextualType(checker, functionTSNode) | ||
: services.getTypeAtLocation(functionNode); | ||
functionType ??= services.getTypeAtLocation(functionNode); | ||
OlivierZal marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
const callSignatures = tsutils.getCallSignaturesOfType(functionType); | ||
// If there is an explicit type annotation *and* that type matches the actual | ||
// function return type, we shouldn't complain (it's intentional, even if unsafe) | ||
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.