-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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-extra-non-null-assertion
- Loading branch information
1 parent
c8fe515
commit f0831a9
Showing
6 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
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
37 changes: 37 additions & 0 deletions
37
packages/eslint-plugin/docs/rules/no-extra-non-null-assertion.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,37 @@ | ||
# Disallow extra non-null assertion | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```ts | ||
const foo: { bar: number } | null = null; | ||
const bar = foo!!!.bar; | ||
``` | ||
|
||
```ts | ||
function foo(bar: number | undefined) { | ||
const bar: number = bar!!!; | ||
} | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```ts | ||
const foo: { bar: number } | null = null; | ||
const bar = foo!.bar; | ||
``` | ||
|
||
```ts | ||
function foo(bar: number | undefined) { | ||
const bar: number = bar!; | ||
} | ||
``` | ||
|
||
## How to use | ||
|
||
```json | ||
{ | ||
"@typescript-eslint/no-extra-non-null-assertion": ["error"] | ||
} | ||
``` |
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
49 changes: 49 additions & 0 deletions
49
packages/eslint-plugin/src/rules/no-extra-non-null-assertion.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,49 @@ | ||
import { | ||
AST_NODE_TYPES, | ||
TSESTree, | ||
} from '@typescript-eslint/experimental-utils'; | ||
import * as util from '../util'; | ||
|
||
export default util.createRule({ | ||
name: 'no-extra-non-null-assertion', | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Disallow extra non-null assertion', | ||
category: 'Stylistic Issues', | ||
recommended: false, | ||
}, | ||
schema: [], | ||
messages: { | ||
noExtraNonNullAssertion: 'Forbidden extra non-null assertion.', | ||
}, | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
/** | ||
* checks if node has extra non null assertion | ||
* @param node the node to be validated | ||
*/ | ||
function hasExtraNonNullAssertion( | ||
node: TSESTree.TSNonNullExpression, | ||
): boolean { | ||
const parent = node.parent; | ||
if (!parent) { | ||
return false; | ||
} | ||
|
||
return ( | ||
parent.type === AST_NODE_TYPES.TSNonNullExpression && | ||
node.expression.type !== AST_NODE_TYPES.TSNonNullExpression | ||
); | ||
} | ||
|
||
return { | ||
TSNonNullExpression(node): void { | ||
if (hasExtraNonNullAssertion(node)) { | ||
context.report({ messageId: 'noExtraNonNullAssertion', node }); | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
54 changes: 54 additions & 0 deletions
54
packages/eslint-plugin/tests/rules/no-extra-non-null-assertion.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,54 @@ | ||
import rule from '../../src/rules/no-extra-non-null-assertion'; | ||
import { RuleTester } from '../RuleTester'; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: '@typescript-eslint/parser', | ||
}); | ||
|
||
ruleTester.run('no-extra-non-null-assertion', rule, { | ||
valid: [ | ||
{ | ||
code: ` | ||
const foo: { bar: number } | null = null; | ||
const bar = foo!.bar; | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
function foo(bar: number | undefined) { | ||
const bar: number = bar!; | ||
} `, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
const foo: { bar: number } | null = null; | ||
const bar = foo!!!.bar; | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'noExtraNonNullAssertion', | ||
endColumn: 17, | ||
column: 13, | ||
line: 3, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
function foo(bar: number | undefined) { | ||
const bar: number = bar!!; | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'noExtraNonNullAssertion', | ||
endColumn: 27, | ||
column: 23, | ||
line: 3, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |