-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): add rule ban-tslint-comment
#2140
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 14 commits into
typescript-eslint:master
from
drewwyatt:dw/ban-tslint-comment
Jun 7, 2020
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0971245
feat(eslint-plugin): add ban-tslint-comment rule
drewwyatt f8006eb
test(eslint-plugin): add tests for ban-tslint-comment
drewwyatt d21aa56
fix(eslint-plugin): export ban-tslint-comment from rules/index.ts
drewwyatt b476194
docs(eslint-plugin): add ban-tslint-comment docs
drewwyatt cbf9fe0
style(eslint-plugin): capitalize "TSLint" and "ESLint" in ban-tslint-…
drewwyatt ee72229
docs(eslint-plugin): add ban-tslint-comment to readme and remove "@" …
drewwyatt 5b1be24
feat(eslint-plugin): add ban-tslint-comment to all/recommended configs
drewwyatt 2335645
Update packages/eslint-plugin/src/rules/ban-tslint-comment.ts
drewwyatt c53d50c
test(eslint-plugin): add ".test" to file name
drewwyatt 1f1317b
docs(eslint-plugin): remove recommended checkmark
drewwyatt 9bfaf16
Merge branch 'master' into dw/ban-tslint-comment
drewwyatt 73d9ff7
Merge branch 'master' into dw/ban-tslint-comment
drewwyatt 1fe1d7d
Merge branch 'master' into dw/ban-tslint-comment
drewwyatt a1807e4
Merge branch 'master' into dw/ban-tslint-comment
drewwyatt 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
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,29 @@ | ||
# Bans `// tslint:<rule-flag>` comments from being used (`ban-tslint-comment`) | ||
|
||
Useful when migrating from TSLint to ESLint. Once TSLint has been removed, this rule helps locate TSLint annotations (e.g. `// tslint:disable`). | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
All TSLint [rule flags](https://palantir.github.io/tslint/usage/rule-flags/) | ||
|
||
```js | ||
/* tslint:disable */ | ||
/* tslint:enable */ | ||
/* tslint:disable:rule1 rule2 rule3... */ | ||
/* tslint:enable:rule1 rule2 rule3... */ | ||
// tslint:disable-next-line | ||
someCode(); // tslint:disable-line | ||
// tslint:disable-next-line:rule1 rule2 rule3... | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
// This is a comment that just happens to mention tslint | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you are still using TSLint. |
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,60 @@ | ||
import { AST_TOKEN_TYPES } from '@typescript-eslint/experimental-utils'; | ||
import * as util from '../util'; | ||
|
||
// tslint regex | ||
// https://github.com/palantir/tslint/blob/95d9d958833fd9dc0002d18cbe34db20d0fbf437/src/enableDisableRules.ts#L32 | ||
const ENABLE_DISABLE_REGEX = /^\s*tslint:(enable|disable)(?:-(line|next-line))?(:|\s|$)/; | ||
|
||
const toText = ( | ||
text: string, | ||
type: AST_TOKEN_TYPES.Line | AST_TOKEN_TYPES.Block, | ||
): string => | ||
type === AST_TOKEN_TYPES.Line | ||
? ['//', text.trim()].join(' ') | ||
: ['/*', text.trim(), '*/'].join(' '); | ||
|
||
export default util.createRule({ | ||
name: 'ban-tslint-comment', | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Bans `// tslint:<rule-flag>` comments from being used', | ||
category: 'Stylistic Issues', | ||
recommended: false, | ||
}, | ||
messages: { | ||
commentDetected: 'tslint comment detected: "{{ text }}"', | ||
}, | ||
schema: [], | ||
fixable: 'code', | ||
}, | ||
defaultOptions: [], | ||
create: context => { | ||
const sourceCode = context.getSourceCode(); | ||
return { | ||
Program(): void { | ||
const comments = sourceCode.getAllComments(); | ||
comments.forEach(c => { | ||
if (ENABLE_DISABLE_REGEX.test(c.value)) { | ||
context.report({ | ||
data: { text: toText(c.value, c.type) }, | ||
node: c, | ||
messageId: 'commentDetected', | ||
fix(fixer) { | ||
const rangeStart = sourceCode.getIndexFromLoc({ | ||
column: c.loc.start.column > 0 ? c.loc.start.column - 1 : 0, | ||
line: c.loc.start.line, | ||
}); | ||
const rangeEnd = sourceCode.getIndexFromLoc({ | ||
column: c.loc.end.column, | ||
line: c.loc.end.line, | ||
}); | ||
return fixer.removeRange([rangeStart, rangeEnd + 1]); | ||
}, | ||
}); | ||
} | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); |
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
84 changes: 84 additions & 0 deletions
84
packages/eslint-plugin/tests/rules/ban-tslint-comment.test.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,84 @@ | ||
import rule from '../../src/rules/ban-tslint-comment'; | ||
import { RuleTester } from '../RuleTester'; | ||
|
||
interface Testable { | ||
code: string; | ||
text?: string; | ||
column?: number; | ||
line?: number; | ||
output?: string; | ||
} | ||
|
||
const PALANTIR_EXAMPLES: Testable[] = [ | ||
{ code: '/* tslint:disable */' }, // Disable all rules for the rest of the file | ||
{ code: '/* tslint:enable */' }, // Enable all rules for the rest of the file | ||
{ | ||
code: '/* tslint:disable:rule1 rule2 rule3... */', | ||
}, // Disable the listed rules for the rest of the file | ||
{ | ||
code: '/* tslint:enable:rule1 rule2 rule3... */', | ||
}, // Enable the listed rules for the rest of the file | ||
{ code: '// tslint:disable-next-line' }, // Disables all rules for the following line | ||
{ | ||
code: 'someCode(); // tslint:disable-line', | ||
text: '// tslint:disable-line', | ||
column: 13, | ||
output: 'someCode();', | ||
}, // Disables all rules for the current line | ||
{ | ||
code: '// tslint:disable-next-line:rule1 rule2 rule3...', | ||
}, // Disables the listed rules for the next line | ||
]; | ||
|
||
// prettier-ignore | ||
const MORE_EXAMPLES: Testable[] = [ | ||
{ | ||
code: `const woah = doSomeStuff(); | ||
// tslint:disable-line | ||
console.log(woah); | ||
`, | ||
output: `const woah = doSomeStuff(); | ||
console.log(woah); | ||
`, | ||
text: '// tslint:disable-line', | ||
line: 2, | ||
}, | ||
] | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: '@typescript-eslint/parser', | ||
}); | ||
|
||
ruleTester.run('ban-tslint-comment', rule, { | ||
valid: [ | ||
{ | ||
code: 'let a: readonly any[] = [];', | ||
}, | ||
{ | ||
code: 'let a = new Array();', | ||
}, | ||
{ | ||
code: '// some other comment', | ||
}, | ||
{ | ||
code: '// TODO: this is a comment that mentions tslint', | ||
}, | ||
{ | ||
code: '/* another comment that mentions tslint */', | ||
}, | ||
], | ||
invalid: [...PALANTIR_EXAMPLES, ...MORE_EXAMPLES].map( | ||
({ code, column, line, output, text }) => ({ | ||
code, | ||
output: output ?? '', | ||
errors: [ | ||
{ | ||
column: column ?? 1, | ||
line: line ?? 1, | ||
data: { text: text ?? code }, | ||
messageId: 'commentDetected' as const, | ||
}, | ||
], | ||
}), | ||
), | ||
}); |
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.