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): create
ban-ts-comment
rule (#1361)
- Loading branch information
1 parent
0b5f6f5
commit 2a83d13
Showing
9 changed files
with
389 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,7 @@ | |
"estree", | ||
"linebreaks", | ||
"necroing", | ||
"nocheck", | ||
"nullish", | ||
"parameterised", | ||
"performant", | ||
|
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,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 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,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; | ||
|
||
if (options[fullDirective]) { | ||
context.report({ | ||
data: { directive }, | ||
node: comment, | ||
messageId: 'tsDirectiveComment', | ||
}); | ||
} | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); |
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
Oops, something went wrong.