Skip to content

Commit 859ab29

Browse files
ldrickbradzacher
authored andcommitted
feat(eslint-plugin): add ban-ts-ignore rule (typescript-eslint#276)
* feat(eslint-plugin): add ban-ts-ignore rule * test(eslint-plugin): add more tests
1 parent df8ed1f commit 859ab29

File tree

5 files changed

+152
-1
lines changed

5 files changed

+152
-1
lines changed

packages/eslint-plugin/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ Then you should add `airbnb` (or `airbnb-base`) to your `extends` section of `.e
113113
| [`@typescript-eslint/adjacent-overload-signatures`](./docs/rules/adjacent-overload-signatures.md) | Require that member overloads be consecutive (`adjacent-overload-signatures` from TSLint) | :heavy_check_mark: | |
114114
| [`@typescript-eslint/array-type`](./docs/rules/array-type.md) | Requires using either `T[]` or `Array<T>` for arrays (`array-type` from TSLint) | :heavy_check_mark: | :wrench: |
115115
| [`@typescript-eslint/ban-types`](./docs/rules/ban-types.md) | Enforces that types will not to be used (`ban-types` from TSLint) | :heavy_check_mark: | :wrench: |
116+
| [`@typescript-eslint/ban-ts-ignore`](./docs/rules/ban-ts-ignore.md) | Bans “// @ts-ignore” comments from being used (`ban-ts-ignore` from TSLint) | :heavy_check_mark: | |
116117
| [`@typescript-eslint/camelcase`](./docs/rules/camelcase.md) | Enforce camelCase naming convention | :heavy_check_mark: | |
117118
| [`@typescript-eslint/class-name-casing`](./docs/rules/class-name-casing.md) | Require PascalCased class and interface names (`class-name` from TSLint) | :heavy_check_mark: | |
118119
| [`@typescript-eslint/explicit-function-return-type`](./docs/rules/explicit-function-return-type.md) | Require explicit return types on functions and class methods | :heavy_check_mark: | |

packages/eslint-plugin/ROADMAP.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
| TSLint rule | | ESLint rule |
1414
| --------------------------------- | :-: | ---------------------------------------------------- |
1515
| [`adjacent-overload-signatures`] || [`@typescript-eslint/adjacent-overload-signatures`] |
16-
| [`ban-ts-ignore`] | 🛑 | N/A |
16+
| [`ban-ts-ignore`] | | [`@typescript-eslint/ban-ts-ignore`] |
1717
| [`ban-types`] || [`@typescript-eslint/ban-types`] |
1818
| [`member-access`] || [`@typescript-eslint/explicit-member-accessibility`] |
1919
| [`member-ordering`] || [`@typescript-eslint/member-ordering`] |
@@ -575,6 +575,7 @@ Relevant plugins: [`chai-expect-keywords`](https://github.com/gavinaiken/eslint-
575575

576576
[`@typescript-eslint/adjacent-overload-signatures`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/adjacent-overload-signatures.md
577577
[`@typescript-eslint/ban-types`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/ban-types.md
578+
[`@typescript-eslint/ban-ts-ignore`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/ban-ts-ignore.md
578579
[`@typescript-eslint/explicit-member-accessibility`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/explicit-member-accessibility.md
579580
[`@typescript-eslint/member-ordering`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/member-ordering.md
580581
[`@typescript-eslint/no-explicit-any`]: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-explicit-any.md
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Bans “// @ts-ignore” comments from being used (ban-ts-ignore)
2+
3+
Suppressing Typescript Compiler Errors can be hard to discover.
4+
5+
## Rule Details
6+
7+
Does not allow the use of `// @ts-ignore` comments.
8+
9+
The following patterns are considered warnings:
10+
11+
```ts
12+
if (false) {
13+
// @ts-ignore: Unreachable code error
14+
console.log('hello');
15+
}
16+
```
17+
18+
The following patterns are not warnings:
19+
20+
```ts
21+
if (false) {
22+
// Compiler warns about unreachable code error
23+
console.log('hello');
24+
}
25+
```
26+
27+
## When Not To Use It
28+
29+
If you are sure, compiler errors won't affect functionality and you need to disable them.
30+
31+
## Further Reading
32+
33+
- TypeScript [Type Checking JavaScript Files](https://www.typescriptlang.org/docs/handbook/type-checking-javascript-files.html)
34+
35+
## Compatibility
36+
37+
- TSLint: [ban-ts-ignore](https://palantir.github.io/tslint/rules/ban-ts-ignore/)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @fileoverview Bans “// @ts-ignore” comments from being used.
3+
* @author Ricky Lippmann <https://github.com/ldrick>
4+
*/
5+
6+
import * as util from '../util';
7+
8+
export default util.createRule({
9+
name: 'ban-ts-ignore',
10+
meta: {
11+
type: 'problem',
12+
docs: {
13+
description: 'Bans “// @ts-ignore” comments from being used.',
14+
tslintRuleName: 'ban-ts-ignore',
15+
category: 'Best Practices',
16+
recommended: 'error'
17+
},
18+
schema: [],
19+
messages: {
20+
tsIgnoreComment:
21+
'Do not use "// @ts-ignore" comments because they suppress compilation errors.'
22+
}
23+
},
24+
defaultOptions: [],
25+
create(context) {
26+
const tsIgnoreRegExp = /^\/*\s*@ts-ignore/;
27+
const sourceCode = context.getSourceCode();
28+
29+
return {
30+
Program(): void {
31+
const comments = sourceCode.getAllComments();
32+
33+
comments.forEach(comment => {
34+
if (comment.type !== 'Line') {
35+
return;
36+
}
37+
if (tsIgnoreRegExp.test(comment.value)) {
38+
context.report({
39+
node: comment,
40+
messageId: 'tsIgnoreComment'
41+
});
42+
}
43+
});
44+
}
45+
};
46+
}
47+
});
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import rule from '../../src/rules/ban-ts-ignore';
2+
import { RuleTester } from '../RuleTester';
3+
4+
const ruleTester = new RuleTester({
5+
parser: '@typescript-eslint/parser'
6+
});
7+
8+
ruleTester.run('ban-ts-ignore', rule, {
9+
valid: [
10+
`// just a comment containing @ts-ignore somewhere`,
11+
`/* @ts-ignore */`,
12+
`/** @ts-ignore */`,
13+
`/*
14+
// @ts-ignore in a block
15+
*/`
16+
],
17+
invalid: [
18+
{
19+
code: '// @ts-ignore',
20+
errors: [
21+
{
22+
messageId: 'tsIgnoreComment',
23+
line: 1,
24+
column: 1
25+
}
26+
]
27+
},
28+
{
29+
code: '// @ts-ignore: Suppress next line',
30+
errors: [
31+
{
32+
messageId: 'tsIgnoreComment',
33+
line: 1,
34+
column: 1
35+
}
36+
]
37+
},
38+
{
39+
code: '/////@ts-ignore: Suppress next line',
40+
errors: [
41+
{
42+
messageId: 'tsIgnoreComment',
43+
line: 1,
44+
column: 1
45+
}
46+
]
47+
},
48+
{
49+
code: `
50+
if (false) {
51+
// @ts-ignore: Unreachable code error
52+
console.log("hello");
53+
}
54+
`,
55+
parser: '@typescript-eslint/parser',
56+
errors: [
57+
{
58+
messageId: 'tsIgnoreComment',
59+
line: 3,
60+
column: 3
61+
}
62+
]
63+
}
64+
]
65+
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy