-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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-optional-chain (#1469)
- Loading branch information
1 parent
9c5b857
commit 498aa24
Showing
7 changed files
with
364 additions
and
79 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
packages/eslint-plugin/docs/rules/no-non-null-asserted-optional-chain.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,39 @@ | ||
# Disallows using a non-null assertion after an optional chain expression (`no-non-null-asserted-optional-chain`) | ||
|
||
## Rule Details | ||
|
||
Optional chain expressions are designed to return `undefined` if the optional property is nullish. | ||
Using non-null assertions after an optional chain expression is wrong, and introduces a serious type safety hole into your code. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```ts | ||
/* eslint @typescript-eslint/no-non-null-asserted-optional-chain: "error" */ | ||
|
||
foo?.bar!; | ||
foo?.bar!.baz; | ||
foo?.bar()!; | ||
foo?.bar!(); | ||
foo?.bar!().baz; | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```ts | ||
/* eslint @typescript-eslint/no-non-null-asserted-optional-chain: "error" */ | ||
|
||
foo?.bar; | ||
(foo?.bar).baz; | ||
foo?.bar(); | ||
foo?.bar(); | ||
foo?.bar().baz; | ||
``` | ||
|
||
## 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) | ||
- [Optional Chaining Proposal](https://github.com/tc39/proposal-optional-chaining/) |
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
52 changes: 52 additions & 0 deletions
52
packages/eslint-plugin/src/rules/no-non-null-asserted-optional-chain.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,52 @@ | ||
import { TSESTree, TSESLint } from '@typescript-eslint/experimental-utils'; | ||
import * as util from '../util'; | ||
|
||
type MessageIds = 'noNonNullOptionalChain' | 'suggestRemovingNonNull'; | ||
|
||
export default util.createRule<[], MessageIds>({ | ||
name: 'no-non-null-asserted-optional-chain', | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: | ||
'Disallows using a non-null assertion after an optional chain expression', | ||
category: 'Possible Errors', | ||
recommended: false, | ||
}, | ||
messages: { | ||
noNonNullOptionalChain: | ||
'Optional chain expressions can return undefined by design - using a non-null assertion is unsafe and wrong.', | ||
suggestRemovingNonNull: 'You should remove the non-null assertion.', | ||
}, | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return { | ||
'TSNonNullExpression > :matches(OptionalMemberExpression, OptionalCallExpression)'( | ||
node: | ||
| TSESTree.OptionalCallExpression | ||
| TSESTree.OptionalMemberExpression, | ||
): void { | ||
// selector guarantees this assertion | ||
const parent = node.parent as TSESTree.TSNonNullExpression; | ||
context.report({ | ||
node, | ||
messageId: 'noNonNullOptionalChain', | ||
// use a suggestion instead of a fixer, because this can obviously break type checks | ||
suggest: [ | ||
{ | ||
messageId: 'suggestRemovingNonNull', | ||
fix(fixer): TSESLint.RuleFix { | ||
return fixer.removeRange([ | ||
parent.range[1] - 1, | ||
parent.range[1], | ||
]); | ||
}, | ||
}, | ||
], | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); |
187 changes: 187 additions & 0 deletions
187
packages/eslint-plugin/tests/rules/no-non-null-asserted-optional-chain.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,187 @@ | ||
import rule from '../../src/rules/no-non-null-asserted-optional-chain'; | ||
import { RuleTester } from '../RuleTester'; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: '@typescript-eslint/parser', | ||
}); | ||
|
||
ruleTester.run('no-non-null-asserted-optional-chain', rule, { | ||
valid: [ | ||
'foo.bar!', | ||
'foo.bar()!', | ||
'foo?.bar', | ||
'foo?.bar()', | ||
'(foo?.bar).baz!', | ||
'(foo?.bar()).baz!', | ||
], | ||
invalid: [ | ||
{ | ||
code: 'foo?.bar!', | ||
errors: [ | ||
{ | ||
messageId: 'noNonNullOptionalChain', | ||
suggestions: [ | ||
{ | ||
messageId: 'suggestRemovingNonNull', | ||
output: 'foo?.bar', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'foo?.["bar"]!', | ||
errors: [ | ||
{ | ||
messageId: 'noNonNullOptionalChain', | ||
suggestions: [ | ||
{ | ||
messageId: 'suggestRemovingNonNull', | ||
output: 'foo?.["bar"]', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'foo?.bar()!', | ||
errors: [ | ||
{ | ||
messageId: 'noNonNullOptionalChain', | ||
suggestions: [ | ||
{ | ||
messageId: 'suggestRemovingNonNull', | ||
output: 'foo?.bar()', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'foo.bar?.()!', | ||
errors: [ | ||
{ | ||
messageId: 'noNonNullOptionalChain', | ||
suggestions: [ | ||
{ | ||
messageId: 'suggestRemovingNonNull', | ||
output: 'foo.bar?.()', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'foo?.bar!()', | ||
errors: [ | ||
{ | ||
messageId: 'noNonNullOptionalChain', | ||
suggestions: [ | ||
{ | ||
messageId: 'suggestRemovingNonNull', | ||
output: 'foo?.bar()', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: '(foo?.bar)!.baz', | ||
errors: [ | ||
{ | ||
messageId: 'noNonNullOptionalChain', | ||
suggestions: [ | ||
{ | ||
messageId: 'suggestRemovingNonNull', | ||
output: '(foo?.bar).baz', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'foo?.["bar"]!.baz', | ||
errors: [ | ||
{ | ||
messageId: 'noNonNullOptionalChain', | ||
suggestions: [ | ||
{ | ||
messageId: 'suggestRemovingNonNull', | ||
output: 'foo?.["bar"].baz', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: '(foo?.bar)!().baz', | ||
errors: [ | ||
{ | ||
messageId: 'noNonNullOptionalChain', | ||
suggestions: [ | ||
{ | ||
messageId: 'suggestRemovingNonNull', | ||
output: '(foo?.bar)().baz', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: '(foo?.bar)!', | ||
errors: [ | ||
{ | ||
messageId: 'noNonNullOptionalChain', | ||
suggestions: [ | ||
{ | ||
messageId: 'suggestRemovingNonNull', | ||
output: '(foo?.bar)', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: '(foo?.bar)!()', | ||
errors: [ | ||
{ | ||
messageId: 'noNonNullOptionalChain', | ||
suggestions: [ | ||
{ | ||
messageId: 'suggestRemovingNonNull', | ||
output: '(foo?.bar)()', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: '(foo?.bar!)', | ||
errors: [ | ||
{ | ||
messageId: 'noNonNullOptionalChain', | ||
suggestions: [ | ||
{ | ||
messageId: 'suggestRemovingNonNull', | ||
output: '(foo?.bar)', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: '(foo?.bar!)()', | ||
errors: [ | ||
{ | ||
messageId: 'noNonNullOptionalChain', | ||
suggestions: [ | ||
{ | ||
messageId: 'suggestRemovingNonNull', | ||
output: '(foo?.bar)()', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
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