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
fcc9bea4e3a723185667df69b588ed7be4c2db7af7e9b280afc478dc5e5946854e0f508e1a6a61e10987168a2284440b1150bf59e92ef701727109a7035591fc806c58858559271db29513b085File 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.