Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(eslint-plugin): [prefer-nullish-coalescing] support if 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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions packages/eslint-plugin/docs/rules/prefer-nullish-coalescing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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

<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.
Expand Down Expand Up @@ -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)
10 changes: 4 additions & 6 deletions packages/eslint-plugin/src/rules/consistent-type-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
if (sourceExports.typeOnlyNamedExport == null) {
// The export is a type export
sourceExports.typeOnlyNamedExport = node;
}
} else if (sourceExports.valueOnlyNamedExport == null) {
// The export is a type export
sourceExports.typeOnlyNamedExport ??= node;
} else {
// The export is a value export
sourceExports.valueOnlyNamedExport = node;
sourceExports.valueOnlyNamedExport ??= node;
}

// Next for the current export, we will separate type/value specifiers.
Expand Down
4 changes: 1 addition & 3 deletions packages/eslint-plugin/src/rules/no-unsafe-return.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ export default createRule({
ts.isArrowFunction(functionTSNode)
? getContextualType(checker, functionTSNode)
: services.getTypeAtLocation(functionNode);
if (!functionType) {
functionType = services.getTypeAtLocation(functionNode);
}
functionType ??= services.getTypeAtLocation(functionNode);
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)
Expand Down
Loading
Loading