-
-
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 ban-ts-ignore rule (#276)
* feat(eslint-plugin): add ban-ts-ignore rule * test(eslint-plugin): add more tests
- Loading branch information
1 parent
df8ed1f
commit 859ab29
Showing
5 changed files
with
152 additions
and
1 deletion.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Bans “// @ts-ignore” comments from being used (ban-ts-ignore) | ||
|
||
Suppressing Typescript Compiler Errors can be hard to discover. | ||
|
||
## Rule Details | ||
|
||
Does not allow the use of `// @ts-ignore` comments. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```ts | ||
if (false) { | ||
// @ts-ignore: Unreachable code error | ||
console.log('hello'); | ||
} | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```ts | ||
if (false) { | ||
// Compiler warns about unreachable code error | ||
console.log('hello'); | ||
} | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you are sure, compiler errors won't affect functionality and you need to disable them. | ||
|
||
## Further Reading | ||
|
||
- TypeScript [Type Checking JavaScript Files](https://www.typescriptlang.org/docs/handbook/type-checking-javascript-files.html) | ||
|
||
## Compatibility | ||
|
||
- TSLint: [ban-ts-ignore](https://palantir.github.io/tslint/rules/ban-ts-ignore/) |
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,47 @@ | ||
/** | ||
* @fileoverview Bans “// @ts-ignore” comments from being used. | ||
* @author Ricky Lippmann <https://github.com/ldrick> | ||
*/ | ||
|
||
import * as util from '../util'; | ||
|
||
export default util.createRule({ | ||
name: 'ban-ts-ignore', | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Bans “// @ts-ignore” comments from being used.', | ||
tslintRuleName: 'ban-ts-ignore', | ||
category: 'Best Practices', | ||
recommended: 'error' | ||
}, | ||
schema: [], | ||
messages: { | ||
tsIgnoreComment: | ||
'Do not use "// @ts-ignore" comments because they suppress compilation errors.' | ||
} | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
const tsIgnoreRegExp = /^\/*\s*@ts-ignore/; | ||
const sourceCode = context.getSourceCode(); | ||
|
||
return { | ||
Program(): void { | ||
const comments = sourceCode.getAllComments(); | ||
|
||
comments.forEach(comment => { | ||
if (comment.type !== 'Line') { | ||
return; | ||
} | ||
if (tsIgnoreRegExp.test(comment.value)) { | ||
context.report({ | ||
node: comment, | ||
messageId: 'tsIgnoreComment' | ||
}); | ||
} | ||
}); | ||
} | ||
}; | ||
} | ||
}); |
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,65 @@ | ||
import rule from '../../src/rules/ban-ts-ignore'; | ||
import { RuleTester } from '../RuleTester'; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: '@typescript-eslint/parser' | ||
}); | ||
|
||
ruleTester.run('ban-ts-ignore', rule, { | ||
valid: [ | ||
`// just a comment containing @ts-ignore somewhere`, | ||
`/* @ts-ignore */`, | ||
`/** @ts-ignore */`, | ||
`/* | ||
// @ts-ignore in a block | ||
*/` | ||
], | ||
invalid: [ | ||
{ | ||
code: '// @ts-ignore', | ||
errors: [ | ||
{ | ||
messageId: 'tsIgnoreComment', | ||
line: 1, | ||
column: 1 | ||
} | ||
] | ||
}, | ||
{ | ||
code: '// @ts-ignore: Suppress next line', | ||
errors: [ | ||
{ | ||
messageId: 'tsIgnoreComment', | ||
line: 1, | ||
column: 1 | ||
} | ||
] | ||
}, | ||
{ | ||
code: '/////@ts-ignore: Suppress next line', | ||
errors: [ | ||
{ | ||
messageId: 'tsIgnoreComment', | ||
line: 1, | ||
column: 1 | ||
} | ||
] | ||
}, | ||
{ | ||
code: ` | ||
if (false) { | ||
// @ts-ignore: Unreachable code error | ||
console.log("hello"); | ||
} | ||
`, | ||
parser: '@typescript-eslint/parser', | ||
errors: [ | ||
{ | ||
messageId: 'tsIgnoreComment', | ||
line: 3, | ||
column: 3 | ||
} | ||
] | ||
} | ||
] | ||
}); |