forked from typescript-eslint/typescript-eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): add
no-non-null-asserted-nullish-coalescing
rule
fixes typescript-eslint#2853
- Loading branch information
Showing
6 changed files
with
302 additions
and
82 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
38 changes: 38 additions & 0 deletions
38
packages/eslint-plugin/docs/rules/no-non-null-asserted-nullish-coalescing.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Disallows using a non-null assertion in the left operand of the nullish coalescing operator (`no-non-null-asserted-nullish-coalescing`) | ||
|
||
## Rule Details | ||
|
||
The nullish coalescing operator is designed to provide a default value when dealing with `null` or `undefined`. | ||
Using non-null assertions in the left operand of the nullish coalescing operator is redundant. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```ts | ||
/* eslint @typescript-eslint/no-non-null-asserted-nullish-coalescing: "error" */ | ||
|
||
foo! ?? bar; | ||
foo.bazz! ?? bar; | ||
foo!.bazz! ?? bar; | ||
foo()! ?? bar; | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```ts | ||
/* eslint @typescript-eslint/no-non-null-asserted-nullish-coalescing: "error" */ | ||
|
||
foo ?? bar; | ||
foo ?? bar!; | ||
foo!.bazz ?? bar; | ||
foo!.bazz ?? bar!; | ||
foo() ?? bar; | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you are not using TypeScript 3.7 (or greater), then you will not need to use this rule, as the operator is not supported. | ||
|
||
## Further Reading | ||
|
||
- [TypeScript 3.7 Release Notes](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html) | ||
- [Nullish Coalescing Proposal](https://github.com/tc39/proposal-nullish-coalescing) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
packages/eslint-plugin/src/rules/no-non-null-asserted-nullish-coalescing.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { TSESTree, TSESLint } from '@typescript-eslint/experimental-utils'; | ||
import * as util from '../util'; | ||
|
||
export default util.createRule({ | ||
name: 'no-non-null-asserted-nullish-coalescing', | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: | ||
'Disallows using a non-null assertion in the left operand of the nullish coalescing operator', | ||
category: 'Possible Errors', | ||
recommended: false, | ||
suggestion: true, | ||
}, | ||
messages: { | ||
noNonNullAssertedNullishCoalescing: | ||
'The nullish coalescing operator is designed to handle undefined and null - using a non-null assertion is not needed.', | ||
suggestRemovingNonNull: 'You should remove the non-null assertion.', | ||
}, | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return { | ||
'LogicalExpression[operator = "??"] > TSNonNullExpression.left'( | ||
node: TSESTree.TSNonNullExpression, | ||
): void { | ||
context.report({ | ||
node, | ||
messageId: 'noNonNullAssertedNullishCoalescing', | ||
// use a suggestion instead of a fixer, because this can obviously break type checks | ||
suggest: [ | ||
{ | ||
messageId: 'suggestRemovingNonNull', | ||
fix(fixer): TSESLint.RuleFix { | ||
return fixer.removeRange([node.range[1] - 1, node.range[1]]); | ||
}, | ||
}, | ||
], | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); |
134 changes: 134 additions & 0 deletions
134
packages/eslint-plugin/tests/rules/no-non-null-asserted-nullish-coalescing.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import rule from '../../src/rules/no-non-null-asserted-nullish-coalescing'; | ||
import { RuleTester } from '../RuleTester'; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: '@typescript-eslint/parser', | ||
}); | ||
|
||
ruleTester.run('no-non-null-asserted-nullish-coalescing', rule, { | ||
valid: [ | ||
'foo ?? bar;', | ||
'foo ?? bar!;', | ||
'foo.bazz ?? bar;', | ||
'foo.bazz ?? bar!;', | ||
'foo!.bazz ?? bar;', | ||
'foo!.bazz ?? bar!;', | ||
'foo() ?? bar;', | ||
'foo() ?? bar!;', | ||
'(foo ?? bar)!;', | ||
], | ||
invalid: [ | ||
{ | ||
code: 'foo! ?? bar;', | ||
errors: [ | ||
{ | ||
messageId: 'noNonNullAssertedNullishCoalescing', | ||
suggestions: [ | ||
{ | ||
messageId: 'suggestRemovingNonNull', | ||
output: 'foo ?? bar;', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'foo! ?? bar!;', | ||
errors: [ | ||
{ | ||
messageId: 'noNonNullAssertedNullishCoalescing', | ||
suggestions: [ | ||
{ | ||
messageId: 'suggestRemovingNonNull', | ||
output: 'foo ?? bar!;', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'foo.bazz! ?? bar;', | ||
errors: [ | ||
{ | ||
messageId: 'noNonNullAssertedNullishCoalescing', | ||
suggestions: [ | ||
{ | ||
messageId: 'suggestRemovingNonNull', | ||
output: 'foo.bazz ?? bar;', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'foo.bazz! ?? bar!;', | ||
errors: [ | ||
{ | ||
messageId: 'noNonNullAssertedNullishCoalescing', | ||
suggestions: [ | ||
{ | ||
messageId: 'suggestRemovingNonNull', | ||
output: 'foo.bazz ?? bar!;', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'foo!.bazz! ?? bar;', | ||
errors: [ | ||
{ | ||
messageId: 'noNonNullAssertedNullishCoalescing', | ||
suggestions: [ | ||
{ | ||
messageId: 'suggestRemovingNonNull', | ||
output: 'foo!.bazz ?? bar;', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'foo!.bazz! ?? bar!;', | ||
errors: [ | ||
{ | ||
messageId: 'noNonNullAssertedNullishCoalescing', | ||
suggestions: [ | ||
{ | ||
messageId: 'suggestRemovingNonNull', | ||
output: 'foo!.bazz ?? bar!;', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'foo()! ?? bar;', | ||
errors: [ | ||
{ | ||
messageId: 'noNonNullAssertedNullishCoalescing', | ||
suggestions: [ | ||
{ | ||
messageId: 'suggestRemovingNonNull', | ||
output: 'foo() ?? bar;', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'foo()! ?? bar!;', | ||
errors: [ | ||
{ | ||
messageId: 'noNonNullAssertedNullishCoalescing', | ||
suggestions: [ | ||
{ | ||
messageId: 'suggestRemovingNonNull', | ||
output: 'foo() ?? bar!;', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}); |