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 (#1183)
Co-authored-by: Brad Zacher <brad.zacher@gmail.com>
- Loading branch information
1 parent
a9117f5
commit 2b3b5d6
Showing
6 changed files
with
132 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
25 changes: 25 additions & 0 deletions
25
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,25 @@ | ||
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) { | ||
return { | ||
'TSNonNullExpression > TSNonNullExpression'(node): void { | ||
context.report({ messageId: 'noExtraNonNullAssertion', node }); | ||
}, | ||
}; | ||
}, | ||
}); |
66 changes: 66 additions & 0 deletions
66
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,66 @@ | ||
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: 19, | ||
column: 13, | ||
line: 3, | ||
}, | ||
{ | ||
messageId: 'noExtraNonNullAssertion', | ||
endColumn: 18, | ||
column: 13, | ||
line: 3, | ||
}, | ||
{ | ||
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, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |