-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): create ban-ts-comment
rule
#1361
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
Merged
bradzacher
merged 6 commits into
typescript-eslint:master
from
G-Rath:add-ban-ts-nocheck-rule
Jan 21, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5523eb7
feat(eslint-plugin): create `ban-ts-nocheck` rule
G-Rath 2ae3261
feat(eslint-plugin): create `ban-ts-comment` rule
G-Rath f81e95a
docs(eslint-plugin): use code quotes for `ban-ts-comment`
G-Rath c11a788
chore(eslint-plugin): regenerate recommended config
G-Rath b464738
Update README.md
bradzacher 0fefafe
Merge branch 'master' into add-ban-ts-nocheck-rule
bradzacher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -61,6 +61,7 @@ | |
"estree", | ||
"linebreaks", | ||
"necroing", | ||
"nocheck", | ||
"nullish", | ||
"parameterised", | ||
"performant", | ||
|
This file contains hidden or 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 hidden or 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 @@ | ||
# Bans `// @ts-<directive>` comments from being used (`ban-ts-comment`) | ||
|
||
TypeScript provides several directive comments that can be used to alter how it processes files. | ||
Using these to suppress TypeScript Compiler Errors reduces the effectiveness of TypeScript overall. | ||
|
||
The directive comments supported by TypeScript are: | ||
|
||
``` | ||
// @ts-ignore | ||
// @ts-nocheck | ||
// @ts-check | ||
``` | ||
|
||
## Rule Details | ||
|
||
This rule lets you set which directive comments you want to allow in your codebase. | ||
By default, only `@ts-check` is allowed, as it enables rather then suppresses errors. | ||
|
||
The configuration looks like this: | ||
|
||
``` | ||
interface Options { | ||
'ts-ignore'?: boolean; | ||
'ts-nocheck'?: boolean; | ||
'ts-check'?: boolean; | ||
} | ||
|
||
const defaultOptions: Options = { | ||
'ts-ignore': true, | ||
'ts-nocheck': true, | ||
'ts-check': false | ||
} | ||
``` | ||
|
||
A value of `true` for a particular directive means that this rule will report if it finds any usage of said directive. | ||
|
||
For example, with the defaults above 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 want to use all of the TypeScript directives. | ||
|
||
## 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 hidden or 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 hidden or 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 hidden or 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,82 @@ | ||
import * as util from '../util'; | ||
|
||
interface Options { | ||
'ts-ignore'?: boolean; | ||
'ts-nocheck'?: boolean; | ||
'ts-check'?: boolean; | ||
} | ||
|
||
const defaultOptions: [Options] = [ | ||
{ | ||
'ts-ignore': true, | ||
'ts-nocheck': true, | ||
'ts-check': false, | ||
}, | ||
]; | ||
|
||
type MessageIds = 'tsDirectiveComment'; | ||
|
||
export default util.createRule<[Options], MessageIds>({ | ||
name: 'ban-ts-comment', | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Bans `// @ts-<directive>` comments from being used', | ||
category: 'Best Practices', | ||
recommended: false, | ||
}, | ||
messages: { | ||
tsDirectiveComment: | ||
'Do not use "// @ts-{directive}" because it alters compilation errors.', | ||
}, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
'ts-ignore': { | ||
type: 'boolean', | ||
default: true, | ||
}, | ||
'ts-nocheck': { | ||
type: 'boolean', | ||
default: true, | ||
}, | ||
'ts-check': { | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
}, | ||
defaultOptions, | ||
create(context, [options]) { | ||
const tsCommentRegExp = /^\/*\s*@ts-(ignore|check|nocheck)/; | ||
const sourceCode = context.getSourceCode(); | ||
|
||
return { | ||
Program(): void { | ||
const comments = sourceCode.getAllComments(); | ||
|
||
comments.forEach(comment => { | ||
if (comment.type !== 'Line') { | ||
return; | ||
} | ||
|
||
const [, directive] = tsCommentRegExp.exec(comment.value) ?? []; | ||
|
||
const fullDirective = `ts-${directive}` as keyof Options; | ||
bradzacher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (options[fullDirective]) { | ||
context.report({ | ||
data: { directive }, | ||
node: comment, | ||
messageId: 'tsDirectiveComment', | ||
}); | ||
} | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.