Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2a83d13

Browse files
G-Rathbradzacher
authored andcommitted
feat(eslint-plugin): create ban-ts-comment rule (typescript-eslint#1361)
1 parent 0b5f6f5 commit 2a83d13

File tree

9 files changed

+389
-2
lines changed

9 files changed

+389
-2
lines changed

.cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"estree",
6262
"linebreaks",
6363
"necroing",
64+
"nocheck",
6465
"nullish",
6566
"parameterised",
6667
"performant",

packages/eslint-plugin/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ Pro Tip: For larger codebases you may want to consider splitting our linting int
9898
| [`@typescript-eslint/adjacent-overload-signatures`](./docs/rules/adjacent-overload-signatures.md) | Require that member overloads be consecutive | :heavy_check_mark: | | |
9999
| [`@typescript-eslint/array-type`](./docs/rules/array-type.md) | Requires using either `T[]` or `Array<T>` for arrays | | :wrench: | |
100100
| [`@typescript-eslint/await-thenable`](./docs/rules/await-thenable.md) | Disallows awaiting a value that is not a Thenable | :heavy_check_mark: | | :thought_balloon: |
101-
| [`@typescript-eslint/ban-ts-ignore`](./docs/rules/ban-ts-ignore.md) | Bans // @ts-ignore comments from being used | :heavy_check_mark: | | |
101+
| [`@typescript-eslint/ban-ts-comment`](./docs/rules/ban-ts-comment.md) | Bans `// @ts-<directive>` comments from being used | | | |
102102
| [`@typescript-eslint/ban-types`](./docs/rules/ban-types.md) | Bans specific types from being used | :heavy_check_mark: | :wrench: | |
103103
| [`@typescript-eslint/consistent-type-assertions`](./docs/rules/consistent-type-assertions.md) | Enforces consistent usage of type assertions | :heavy_check_mark: | | |
104104
| [`@typescript-eslint/consistent-type-definitions`](./docs/rules/consistent-type-definitions.md) | Consistent with type definition either `interface` or `type` | | :wrench: | |
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Bans `// @ts-<directive>` comments from being used (`ban-ts-comment`)
2+
3+
TypeScript provides several directive comments that can be used to alter how it processes files.
4+
Using these to suppress TypeScript Compiler Errors reduces the effectiveness of TypeScript overall.
5+
6+
The directive comments supported by TypeScript are:
7+
8+
```
9+
// @ts-ignore
10+
// @ts-nocheck
11+
// @ts-check
12+
```
13+
14+
## Rule Details
15+
16+
This rule lets you set which directive comments you want to allow in your codebase.
17+
By default, only `@ts-check` is allowed, as it enables rather then suppresses errors.
18+
19+
The configuration looks like this:
20+
21+
```
22+
interface Options {
23+
'ts-ignore'?: boolean;
24+
'ts-nocheck'?: boolean;
25+
'ts-check'?: boolean;
26+
}
27+
28+
const defaultOptions: Options = {
29+
'ts-ignore': true,
30+
'ts-nocheck': true,
31+
'ts-check': false
32+
}
33+
```
34+
35+
A value of `true` for a particular directive means that this rule will report if it finds any usage of said directive.
36+
37+
For example, with the defaults above the following patterns are considered warnings:
38+
39+
```ts
40+
if (false) {
41+
// @ts-ignore: Unreachable code error
42+
console.log('hello');
43+
}
44+
```
45+
46+
The following patterns are not warnings:
47+
48+
```ts
49+
if (false) {
50+
// Compiler warns about unreachable code error
51+
console.log('hello');
52+
}
53+
```
54+
55+
## When Not To Use It
56+
57+
If you want to use all of the TypeScript directives.
58+
59+
## Further Reading
60+
61+
- TypeScript [Type Checking JavaScript Files](https://www.typescriptlang.org/docs/handbook/type-checking-javascript-files.html)
62+
63+
## Compatibility
64+
65+
- TSLint: [ban-ts-ignore](https://palantir.github.io/tslint/rules/ban-ts-ignore/)

packages/eslint-plugin/docs/rules/ban-ts-ignore.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Bans “// @ts-ignore” comments from being used (`ban-ts-ignore`)
22

3+
This rule has been deprecated in favor of [`ban-ts-comment`](./ban-ts-comment.md)
4+
35
Suppressing TypeScript Compiler Errors can be hard to discover.
46

57
## Rule Details

packages/eslint-plugin/src/configs/all.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"@typescript-eslint/adjacent-overload-signatures": "error",
55
"@typescript-eslint/array-type": "error",
66
"@typescript-eslint/await-thenable": "error",
7-
"@typescript-eslint/ban-ts-ignore": "error",
7+
"@typescript-eslint/ban-ts-comment": "error",
88
"@typescript-eslint/ban-types": "error",
99
"brace-style": "off",
1010
"@typescript-eslint/brace-style": "error",
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import * as util from '../util';
2+
3+
interface Options {
4+
'ts-ignore'?: boolean;
5+
'ts-nocheck'?: boolean;
6+
'ts-check'?: boolean;
7+
}
8+
9+
const defaultOptions: [Options] = [
10+
{
11+
'ts-ignore': true,
12+
'ts-nocheck': true,
13+
'ts-check': false,
14+
},
15+
];
16+
17+
type MessageIds = 'tsDirectiveComment';
18+
19+
export default util.createRule<[Options], MessageIds>({
20+
name: 'ban-ts-comment',
21+
meta: {
22+
type: 'problem',
23+
docs: {
24+
description: 'Bans `// @ts-<directive>` comments from being used',
25+
category: 'Best Practices',
26+
recommended: false,
27+
},
28+
messages: {
29+
tsDirectiveComment:
30+
'Do not use "// @ts-{directive}" because it alters compilation errors.',
31+
},
32+
schema: [
33+
{
34+
type: 'object',
35+
properties: {
36+
'ts-ignore': {
37+
type: 'boolean',
38+
default: true,
39+
},
40+
'ts-nocheck': {
41+
type: 'boolean',
42+
default: true,
43+
},
44+
'ts-check': {
45+
type: 'boolean',
46+
default: false,
47+
},
48+
},
49+
additionalProperties: false,
50+
},
51+
],
52+
},
53+
defaultOptions,
54+
create(context, [options]) {
55+
const tsCommentRegExp = /^\/*\s*@ts-(ignore|check|nocheck)/;
56+
const sourceCode = context.getSourceCode();
57+
58+
return {
59+
Program(): void {
60+
const comments = sourceCode.getAllComments();
61+
62+
comments.forEach(comment => {
63+
if (comment.type !== 'Line') {
64+
return;
65+
}
66+
67+
const [, directive] = tsCommentRegExp.exec(comment.value) ?? [];
68+
69+
const fullDirective = `ts-${directive}` as keyof Options;
70+
71+
if (options[fullDirective]) {
72+
context.report({
73+
data: { directive },
74+
node: comment,
75+
messageId: 'tsDirectiveComment',
76+
});
77+
}
78+
});
79+
},
80+
};
81+
},
82+
});

packages/eslint-plugin/src/rules/ban-ts-ignore.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export default util.createRule({
1414
tsIgnoreComment:
1515
'Do not use "// @ts-ignore" comments because they suppress compilation errors.',
1616
},
17+
deprecated: true,
18+
replacedBy: ['@typescript-eslint/ban-ts-comment'],
1719
},
1820
defaultOptions: [],
1921
create(context) {

packages/eslint-plugin/src/rules/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import adjacentOverloadSignatures from './adjacent-overload-signatures';
22
import arrayType from './array-type';
33
import awaitThenable from './await-thenable';
44
import banTsIgnore from './ban-ts-ignore';
5+
import banTsComment from './ban-ts-comment';
56
import banTypes from './ban-types';
67
import braceStyle from './brace-style';
78
import camelcase from './camelcase';
@@ -85,6 +86,7 @@ export default {
8586
'array-type': arrayType,
8687
'await-thenable': awaitThenable,
8788
'ban-ts-ignore': banTsIgnore,
89+
'ban-ts-comment': banTsComment,
8890
'ban-types': banTypes,
8991
'brace-style': braceStyle,
9092
camelcase: camelcase,

0 commit comments

Comments
 (0)