Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): createban-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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
bradzacher merged 6 commits intotypescript-eslint:masterfromG-Rath:add-ban-ts-nocheck-ruleJan 21, 2020
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
6 commits Select commitHold shift + click to select a range
5523eb7
feat(eslint-plugin): create `ban-ts-nocheck` rule
G-Rath2ae3261
feat(eslint-plugin): create `ban-ts-comment` rule
G-Rathf81e95a
docs(eslint-plugin): use code quotes for `ban-ts-comment`
G-Rathc11a788
chore(eslint-plugin): regenerate recommended config
G-Rathb464738
Update README.md
bradzacher0fefafe
Merge branch 'master' into add-ban-ts-nocheck-rule
bradzacherFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions.cspell.json
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", | ||
2 changes: 1 addition & 1 deletionpackages/eslint-plugin/README.md
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
65 changes: 65 additions & 0 deletionspackages/eslint-plugin/docs/rules/ban-ts-comment.md
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/) |
2 changes: 2 additions & 0 deletionspackages/eslint-plugin/docs/rules/ban-ts-ignore.md
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
2 changes: 1 addition & 1 deletionpackages/eslint-plugin/src/configs/all.json
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
82 changes: 82 additions & 0 deletionspackages/eslint-plugin/src/rules/ban-ts-comment.ts
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)/; | ||
bradzacher marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
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 resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
if (options[fullDirective]) { | ||
context.report({ | ||
data: { directive }, | ||
node: comment, | ||
messageId: 'tsDirectiveComment', | ||
}); | ||
} | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); |
2 changes: 2 additions & 0 deletionspackages/eslint-plugin/src/rules/ban-ts-ignore.ts
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
2 changes: 2 additions & 0 deletionspackages/eslint-plugin/src/rules/index.ts
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.
Uh oh!
There was an error while loading.Please reload this page.
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.