Skip to content
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

feat(eslint-plugin): add no-extra-non-null-assertion #1183

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/eslint-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ Then you should add `airbnb` (or `airbnb-base`) to your `extends` section of `.e
| [`@typescript-eslint/no-empty-function`](./docs/rules/no-empty-function.md) | Disallow empty functions | :heavy_check_mark: | | |
| [`@typescript-eslint/no-empty-interface`](./docs/rules/no-empty-interface.md) | Disallow the declaration of empty interfaces | :heavy_check_mark: | | |
| [`@typescript-eslint/no-explicit-any`](./docs/rules/no-explicit-any.md) | Disallow usage of the `any` type | :heavy_check_mark: | :wrench: | |
| [`@typescript-eslint/no-extra-non-null-assertion`](./docs/rules/no-extra-non-null-assertion.md) | Disallow extra non-null assertion | | | |
| [`@typescript-eslint/no-extra-parens`](./docs/rules/no-extra-parens.md) | Disallow unnecessary parentheses | | :wrench: | |
| [`@typescript-eslint/no-extraneous-class`](./docs/rules/no-extraneous-class.md) | Forbids the use of classes as namespaces | | | |
| [`@typescript-eslint/no-floating-promises`](./docs/rules/no-floating-promises.md) | Requires Promise-like values to be handled appropriately. | | | :thought_balloon: |
Expand Down
37 changes: 37 additions & 0 deletions packages/eslint-plugin/docs/rules/no-extra-non-null-assertion.md
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"]
}
```
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/configs/all.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@typescript-eslint/no-empty-interface": "error",
"@typescript-eslint/no-explicit-any": "error",
"no-extra-parens": "off",
"@typescript-eslint/no-extra-non-null-assertion": "error",
"@typescript-eslint/no-extra-parens": "error",
"@typescript-eslint/no-extraneous-class": "error",
"@typescript-eslint/no-floating-promises": "error",
Expand Down
2 changes: 2 additions & 0 deletions packages/eslint-plugin/src/rules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import noArrayConstructor from './no-array-constructor';
import noEmptyFunction from './no-empty-function';
import noEmptyInterface from './no-empty-interface';
import noExplicitAny from './no-explicit-any';
import noExtraNonNullAssertion from './no-extra-non-null-assertion';
import noExtraParens from './no-extra-parens';
import noExtraneousClass from './no-extraneous-class';
import noFloatingPromises from './no-floating-promises';
Expand Down Expand Up @@ -88,6 +89,7 @@ export default {
'no-empty-function': noEmptyFunction,
'no-empty-interface': noEmptyInterface,
'no-explicit-any': noExplicitAny,
'no-extra-non-null-assertion': noExtraNonNullAssertion,
'no-extra-parens': noExtraParens,
'no-extraneous-class': noExtraneousClass,
'no-floating-promises': noFloatingPromises,
Expand Down
25 changes: 25 additions & 0 deletions packages/eslint-plugin/src/rules/no-extra-non-null-assertion.ts
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 });
},
};
},
});
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,
},
],
},
],
});