-
Notifications
You must be signed in to change notification settings - Fork 18
@typescript-eslint/ban-tslint-comment #464
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
Open
ScriptedAlchemy
wants to merge
13
commits into
web-infra-dev:main
Choose a base branch
from
ScriptedAlchemy:cursor/ban-tslint-comment-rule-c4ca
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b8a6b0d
feat: port ban-tslint-comment rule
cursoragent 4cde30d
Merge remote-tracking branch 'origin/main' into cursor/ban-tslint-com…
ScriptedAlchemy c67cceb
Merge branch 'main' into cursor/ban-tslint-comment-rule-c4ca
ScriptedAlchemy 00a0356
fix: refine ban-tslint-comment autofix range
ScriptedAlchemy 26de994
fix: preserve original tslint comment text
ScriptedAlchemy 3a16bff
fix: satisfy golangci-lint in ban-tslint-comment
ScriptedAlchemy 8427d22
fix: align ban-tslint-comment test fixture text
ScriptedAlchemy 4b5614b
fix: drop unused fmt import in ban-tslint-comment
ScriptedAlchemy 3f63323
Merge branch 'main' into cursor/ban-tslint-comment-rule-c4ca
ScriptedAlchemy 38abd3d
fix: harden prefer-string-starts-ends-with parity behavior
ScriptedAlchemy 5b929ec
fix: preserve explicit output semantics in rule-tester snapshots
ScriptedAlchemy 20d4be2
chore: format rule-tester source
ScriptedAlchemy ac8dfdd
fix: keep nullable jsxPragma in rule-tester options
ScriptedAlchemy 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
121 changes: 121 additions & 0 deletions
121
internal/plugins/typescript/rules/ban_tslint_comment/ban_tslint_comment.go
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,121 @@ | ||
| package ban_tslint_comment | ||
|
|
||
| import ( | ||
| "regexp" | ||
| "strings" | ||
|
|
||
| "github.com/microsoft/typescript-go/shim/ast" | ||
| "github.com/microsoft/typescript-go/shim/core" | ||
| "github.com/microsoft/typescript-go/shim/scanner" | ||
| "github.com/web-infra-dev/rslint/internal/rule" | ||
| "github.com/web-infra-dev/rslint/internal/utils" | ||
| ) | ||
|
|
||
| // tslint regex | ||
| // https://github.com/palantir/tslint/blob/95d9d958833fd9dc0002d18cbe34db20d0fbf437/src/enableDisableRules.ts#L32 | ||
| var enableDisableRegex = regexp.MustCompile(`^\s*tslint:(enable|disable)(?:-(line|next-line))?(:|\s|$)`) | ||
|
|
||
| // BanTslintCommentRule implements the ban-tslint-comment rule. | ||
| // Disallows tslint directive comments like // tslint:disable | ||
| var BanTslintCommentRule = rule.CreateRule(rule.Rule{ | ||
| Name: "ban-tslint-comment", | ||
| Run: run, | ||
| }) | ||
|
|
||
| func run(ctx rule.RuleContext, _ any) rule.RuleListeners { | ||
| text := ctx.SourceFile.Text() | ||
|
|
||
| utils.ForEachComment(&ctx.SourceFile.Node, func(comment *ast.CommentRange) { | ||
| commentValue := extractCommentValue(text, comment) | ||
| if !enableDisableRegex.MatchString(commentValue) { | ||
| return | ||
| } | ||
|
|
||
| commentText := extractCommentText(text, comment) | ||
| if commentText == "" { | ||
| commentText = commentValue | ||
| } | ||
| message := rule.RuleMessage{ | ||
| Id: "commentDetected", | ||
| Description: "tslint comment detected: " + commentText, | ||
| } | ||
|
|
||
| fixRange := buildFixRange(ctx.SourceFile, comment, len(text)) | ||
| ctx.ReportRangeWithFixes( | ||
| core.NewTextRange(comment.Pos(), comment.End()), | ||
| message, | ||
| rule.RuleFixRemoveRange(fixRange), | ||
| ) | ||
| }, ctx.SourceFile) | ||
|
|
||
| return rule.RuleListeners{} | ||
| } | ||
|
|
||
| func extractCommentValue(text string, comment *ast.CommentRange) string { | ||
| if comment.Pos() < 0 || comment.End() > len(text) { | ||
| return "" | ||
| } | ||
|
|
||
| switch comment.Kind { | ||
| case ast.KindSingleLineCommentTrivia: | ||
| if comment.End() <= comment.Pos()+2 { | ||
| return "" | ||
| } | ||
| return text[comment.Pos()+2 : comment.End()] | ||
| case ast.KindMultiLineCommentTrivia: | ||
| if comment.End() <= comment.Pos()+4 { | ||
| return "" | ||
| } | ||
| return text[comment.Pos()+2 : comment.End()-2] | ||
| default: | ||
| return "" | ||
| } | ||
| } | ||
|
|
||
| func extractCommentText(text string, comment *ast.CommentRange) string { | ||
| if comment.End() <= comment.Pos() { | ||
| return "" | ||
| } | ||
| if comment.Pos() < 0 || comment.End() > len(text) { | ||
| return "" | ||
| } | ||
| return strings.TrimSpace(text[comment.Pos():comment.End()]) | ||
| } | ||
|
|
||
| func buildFixRange(sourceFile *ast.SourceFile, comment *ast.CommentRange, textLen int) core.TextRange { | ||
| text := sourceFile.Text() | ||
| start := comment.Pos() | ||
| end := comment.End() | ||
|
|
||
| startLine, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, start) | ||
| lineStart := scanner.GetECMAPositionOfLineAndCharacter(sourceFile, startLine, 0) | ||
|
|
||
| isStandalone := true | ||
| for i := lineStart; i < start; i++ { | ||
| if text[i] != ' ' && text[i] != '\t' { | ||
| isStandalone = false | ||
| break | ||
| } | ||
| } | ||
|
|
||
| if isStandalone { | ||
| start = lineStart | ||
| if end < textLen { | ||
| switch text[end] { | ||
| case '\r': | ||
| end++ | ||
| if end < textLen && text[end] == '\n' { | ||
| end++ | ||
| } | ||
| case '\n': | ||
| end++ | ||
| } | ||
| } | ||
| } else { | ||
| for start > lineStart && (text[start-1] == ' ' || text[start-1] == '\t') { | ||
| start-- | ||
| } | ||
| } | ||
|
|
||
| return core.NewTextRange(start, end) | ||
| } | ||
ScriptedAlchemy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
27 changes: 27 additions & 0 deletions
27
internal/plugins/typescript/rules/ban_tslint_comment/ban_tslint_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,27 @@ | ||
| # ban-tslint-comment | ||
|
|
||
| ## Rule Details | ||
|
|
||
| Disallow TSLint directive comments such as `// tslint:disable` and | ||
| `// tslint:disable-next-line`. These directives are not used by ESLint and are | ||
| typically left behind when migrating from TSLint. | ||
|
|
||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ```javascript | ||
| /* tslint:disable */ | ||
| /* tslint:enable */ | ||
| // tslint:disable-next-line | ||
| someCode(); // tslint:disable-line | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule: | ||
|
|
||
| ```javascript | ||
| // some other comment | ||
| /* another comment that mentions tslint */ | ||
| ``` | ||
|
|
||
| ## Original Documentation | ||
|
|
||
| - [typescript-eslint ban-tslint-comment](https://typescript-eslint.io/rules/ban-tslint-comment) |
75 changes: 75 additions & 0 deletions
75
internal/plugins/typescript/rules/ban_tslint_comment/ban_tslint_comment_test.go
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,75 @@ | ||
| package ban_tslint_comment | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/web-infra-dev/rslint/internal/plugins/typescript/rules/fixtures" | ||
| "github.com/web-infra-dev/rslint/internal/rule_tester" | ||
| ) | ||
|
|
||
| func TestBanTslintCommentRule(t *testing.T) { | ||
| rule_tester.RunRuleTester(fixtures.GetRootDir(), "tsconfig.json", t, &BanTslintCommentRule, []rule_tester.ValidTestCase{ | ||
| {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 */"}, | ||
| }, []rule_tester.InvalidTestCase{ | ||
| { | ||
| Code: "/* tslint:disable */", | ||
| Errors: []rule_tester.InvalidTestCaseError{ | ||
| {MessageId: "commentDetected", Line: 1, Column: 1}, | ||
| }, | ||
| Output: []string{""}, | ||
| }, | ||
| { | ||
| Code: "/* tslint:enable */", | ||
| Errors: []rule_tester.InvalidTestCaseError{ | ||
| {MessageId: "commentDetected", Line: 1, Column: 1}, | ||
| }, | ||
| Output: []string{""}, | ||
| }, | ||
| { | ||
| Code: "/* tslint:disable:rule1 rule2 rule3... */", | ||
| Errors: []rule_tester.InvalidTestCaseError{ | ||
| {MessageId: "commentDetected", Line: 1, Column: 1}, | ||
| }, | ||
| Output: []string{""}, | ||
| }, | ||
| { | ||
| Code: "/* tslint:enable:rule1 rule2 rule3... */", | ||
| Errors: []rule_tester.InvalidTestCaseError{ | ||
| {MessageId: "commentDetected", Line: 1, Column: 1}, | ||
| }, | ||
| Output: []string{""}, | ||
| }, | ||
| { | ||
| Code: "// tslint:disable-next-line", | ||
| Errors: []rule_tester.InvalidTestCaseError{ | ||
| {MessageId: "commentDetected", Line: 1, Column: 1}, | ||
| }, | ||
| Output: []string{""}, | ||
| }, | ||
| { | ||
| Code: "someCode(); // tslint:disable-line", | ||
| Errors: []rule_tester.InvalidTestCaseError{ | ||
| {MessageId: "commentDetected", Line: 1, Column: 13}, | ||
| }, | ||
| Output: []string{"someCode();"}, | ||
| }, | ||
| { | ||
| Code: "// tslint:disable-next-line:rule1 rule2 rule3...", | ||
| Errors: []rule_tester.InvalidTestCaseError{ | ||
| {MessageId: "commentDetected", Line: 1, Column: 1}, | ||
| }, | ||
| Output: []string{""}, | ||
| }, | ||
| { | ||
| Code: "const whoa = doSomeStuff();\n// tslint:disable-line\nconsole.log(whoa);\n", | ||
| Errors: []rule_tester.InvalidTestCaseError{ | ||
| {MessageId: "commentDetected", Line: 2, Column: 1}, | ||
| }, | ||
| Output: []string{"const whoa = doSomeStuff();\nconsole.log(whoa);\n"}, | ||
| }, | ||
| }) | ||
| } |
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
Oops, something went wrong.
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.