-
Notifications
You must be signed in to change notification settings - Fork 19
@typescript-eslint/prefer-ts-expect-error #484
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
7
commits into
main
Choose a base branch
from
codex/ts-eslint-prefer-ts-expect-error-57f3
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
7 commits
Select commit
Hold shift + click to select a range
fc37c93
feat: port rule @typescript-eslint/prefer-ts-expect-error
ScriptedAlchemy fc8cbc2
fix: align prefer-ts-expect-error with upstream
ScriptedAlchemy 570682e
Merge branch 'main' into codex/ts-eslint-prefer-ts-expect-error-57f3
ScriptedAlchemy 93dc011
refactor(typescript/prefer-ts-expect-error): simplify kind dispatch
ScriptedAlchemy b0ff86f
fix: enforce ts-ignore directive token boundary
ScriptedAlchemy bf79b48
fix: tighten prefer-ts-expect-error matching
ScriptedAlchemy f068e44
Fix staticcheck in prefer-ts-expect-error
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
131 changes: 131 additions & 0 deletions
131
internal/plugins/typescript/rules/prefer_ts_expect_error/prefer_ts_expect_error.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,131 @@ | ||
| package prefer_ts_expect_error | ||
|
|
||
| import ( | ||
| "strings" | ||
|
|
||
| "github.com/microsoft/typescript-go/shim/ast" | ||
| "github.com/microsoft/typescript-go/shim/core" | ||
| "github.com/web-infra-dev/rslint/internal/rule" | ||
| "github.com/web-infra-dev/rslint/internal/utils" | ||
| ) | ||
|
|
||
| const tsIgnoreDirective = "@ts-ignore" | ||
| const tsExpectErrorDirective = "@ts-expect-error" | ||
|
|
||
| func buildPreferExpectErrorMessage() rule.RuleMessage { | ||
| return rule.RuleMessage{ | ||
| Id: "preferExpectErrorComment", | ||
| Description: "Use @ts-expect-error instead of @ts-ignore.", | ||
| } | ||
| } | ||
|
|
||
| func findDirectiveInLineComment(commentText string) (int, int, bool) { | ||
| if len(commentText) < 2 || commentText[0] != '/' || commentText[1] != '/' { | ||
| return 0, 0, false | ||
| } | ||
| idx := 2 | ||
| for idx < len(commentText) && (commentText[idx] == ' ' || commentText[idx] == '\t') { | ||
| idx++ | ||
| } | ||
| if idx < len(commentText) && commentText[idx] == '/' { | ||
| idx++ | ||
| } | ||
| for idx < len(commentText) && (commentText[idx] == ' ' || commentText[idx] == '\t') { | ||
| idx++ | ||
| } | ||
| if hasTsIgnoreDirectiveAt(commentText, idx) { | ||
| return idx, idx + len(tsIgnoreDirective), true | ||
| } | ||
| return 0, 0, false | ||
| } | ||
|
|
||
| func isDirectiveBoundaryChar(ch byte) bool { | ||
| return (ch < 'a' || ch > 'z') && | ||
| (ch < 'A' || ch > 'Z') && | ||
| (ch < '0' || ch > '9') && | ||
| ch != '_' && | ||
| ch != '$' | ||
| } | ||
|
|
||
| func hasTsIgnoreDirectiveAt(text string, idx int) bool { | ||
| if idx < 0 || idx >= len(text) || !strings.HasPrefix(text[idx:], tsIgnoreDirective) { | ||
| return false | ||
| } | ||
| end := idx + len(tsIgnoreDirective) | ||
| if end >= len(text) { | ||
| return true | ||
| } | ||
| return isDirectiveBoundaryChar(text[end]) | ||
| } | ||
|
|
||
| func findDirectiveInBlockComment(commentText string) (int, int, bool) { | ||
| if len(commentText) < 4 { | ||
| return 0, 0, false | ||
| } | ||
|
|
||
| contentStart := 2 | ||
| contentEnd := len(commentText) - 2 | ||
| if contentEnd <= contentStart { | ||
| return 0, 0, false | ||
| } | ||
| content := commentText[contentStart:contentEnd] | ||
|
|
||
| lastLineStart := strings.LastIndexByte(content, '\n') | ||
| if lastLineStart == -1 { | ||
| lastLineStart = 0 | ||
| } else { | ||
| lastLineStart++ | ||
| } | ||
|
|
||
| line := content[lastLineStart:] | ||
| idx := 0 | ||
| for idx < len(line) && (line[idx] == ' ' || line[idx] == '\t' || line[idx] == '\r') { | ||
| idx++ | ||
| } | ||
| for idx < len(line) && (line[idx] == '/' || line[idx] == '*') { | ||
| idx++ | ||
| } | ||
| for idx < len(line) && (line[idx] == ' ' || line[idx] == '\t') { | ||
| idx++ | ||
| } | ||
| if idx < len(line) && hasTsIgnoreDirectiveAt(line, idx) { | ||
| start := contentStart + lastLineStart + idx | ||
| return start, start + len(tsIgnoreDirective), true | ||
| } | ||
|
|
||
| return 0, 0, false | ||
| } | ||
|
|
||
| func findTsIgnoreDirective(commentText string, kind ast.Kind) (int, int, bool) { | ||
| switch kind { | ||
| case ast.KindSingleLineCommentTrivia: | ||
| return findDirectiveInLineComment(commentText) | ||
| case ast.KindMultiLineCommentTrivia: | ||
| return findDirectiveInBlockComment(commentText) | ||
| } | ||
| return 0, 0, false | ||
| } | ||
|
|
||
| var PreferTsExpectErrorRule = rule.CreateRule(rule.Rule{ | ||
| Name: "prefer-ts-expect-error", | ||
| Run: func(ctx rule.RuleContext, options any) rule.RuleListeners { | ||
| fullText := ctx.SourceFile.Text() | ||
| utils.ForEachComment(ctx.SourceFile.AsNode(), func(comment *ast.CommentRange) { | ||
| if comment == nil { | ||
| return | ||
| } | ||
| commentText := fullText[comment.Pos():comment.End()] | ||
| start, end, ok := findTsIgnoreDirective(commentText, comment.Kind) | ||
| if !ok { | ||
| return | ||
| } | ||
|
|
||
| fixRange := core.NewTextRange(comment.Pos()+start, comment.Pos()+end) | ||
| fix := rule.RuleFixReplaceRange(fixRange, tsExpectErrorDirective) | ||
| commentRange := core.NewTextRange(comment.Pos(), comment.End()) | ||
| ctx.ReportRangeWithFixes(commentRange, buildPreferExpectErrorMessage(), fix) | ||
| }, ctx.SourceFile) | ||
|
|
||
| return rule.RuleListeners{} | ||
| }, | ||
| }) | ||
21 changes: 21 additions & 0 deletions
21
internal/plugins/typescript/rules/prefer_ts_expect_error/prefer_ts_expect_error.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,21 @@ | ||
| # prefer-ts-expect-error | ||
|
|
||
| ## Rule Details | ||
|
|
||
| Prefer `@ts-expect-error` over `@ts-ignore` in TypeScript directive comments. | ||
|
|
||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ```typescript | ||
| // @ts-ignore | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule: | ||
|
|
||
| ```typescript | ||
| // @ts-expect-error | ||
| ``` | ||
|
|
||
| ## Original Documentation | ||
|
|
||
| https://typescript-eslint.io/rules/prefer-ts-expect-error |
155 changes: 155 additions & 0 deletions
155
internal/plugins/typescript/rules/prefer_ts_expect_error/prefer_ts_expect_error_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,155 @@ | ||
| package prefer_ts_expect_error | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/web-infra-dev/rslint/internal/plugins/typescript/rules/fixtures" | ||
| "github.com/web-infra-dev/rslint/internal/rule_tester" | ||
| ) | ||
|
|
||
| func TestPreferTsExpectErrorRule(t *testing.T) { | ||
| rule_tester.RunRuleTester(fixtures.GetRootDir(), "tsconfig.json", t, &PreferTsExpectErrorRule, []rule_tester.ValidTestCase{ | ||
| {Code: `// @ts-nocheck`}, | ||
| {Code: `// @ts-check`}, | ||
| {Code: `// just a comment containing @ts-ignore somewhere`}, | ||
| {Code: `// @ts-ignorefoo`}, | ||
| {Code: `/* @ts-ignorefoo */`}, | ||
| { | ||
| Code: ` | ||
| { | ||
| /* | ||
| just a comment containing @ts-ignore somewhere in a block | ||
| */ | ||
| } | ||
| `, | ||
| }, | ||
| {Code: `// @ts-expect-error`}, | ||
| { | ||
| Code: ` | ||
| if (false) { | ||
| // @ts-expect-error: Unreachable code error | ||
| console.log('hello'); | ||
| } | ||
| `, | ||
| }, | ||
| { | ||
| Code: ` | ||
| /** | ||
| * Explaining comment | ||
| * | ||
| * @ts-expect-error | ||
| * | ||
| * Not last line | ||
| * */ | ||
| `, | ||
| }, | ||
| }, []rule_tester.InvalidTestCase{ | ||
| { | ||
| Code: `// @ts-ignore`, | ||
| Output: []string{ | ||
| `// @ts-expect-error`, | ||
| }, | ||
| Errors: []rule_tester.InvalidTestCaseError{ | ||
| {MessageId: "preferExpectErrorComment", Line: 1, Column: 1, EndLine: 1, EndColumn: 14}, | ||
| }, | ||
| }, | ||
| { | ||
| Code: `// @ts-ignore: Suppress next line`, | ||
| Output: []string{ | ||
| `// @ts-expect-error: Suppress next line`, | ||
| }, | ||
| Errors: []rule_tester.InvalidTestCaseError{ | ||
| {MessageId: "preferExpectErrorComment", Line: 1, Column: 1, EndLine: 1, EndColumn: 34}, | ||
| }, | ||
| }, | ||
| { | ||
| Code: `///@ts-ignore: Suppress next line`, | ||
| Output: []string{ | ||
| `///@ts-expect-error: Suppress next line`, | ||
| }, | ||
| Errors: []rule_tester.InvalidTestCaseError{ | ||
| {MessageId: "preferExpectErrorComment", Line: 1, Column: 1, EndLine: 1, EndColumn: 34}, | ||
| }, | ||
| }, | ||
| { | ||
| Code: `/// @ts-ignore: Suppress next line`, | ||
| Output: []string{ | ||
| `/// @ts-expect-error: Suppress next line`, | ||
| }, | ||
| Errors: []rule_tester.InvalidTestCaseError{ | ||
| {MessageId: "preferExpectErrorComment", Line: 1, Column: 1, EndLine: 1, EndColumn: 35}, | ||
| }, | ||
| }, | ||
| { | ||
| Code: ` | ||
| if (false) { | ||
| // @ts-ignore: Unreachable code error | ||
| console.log('hello'); | ||
| } | ||
| `, | ||
| Output: []string{ | ||
| ` | ||
| if (false) { | ||
| // @ts-expect-error: Unreachable code error | ||
| console.log('hello'); | ||
| } | ||
| `, | ||
| }, | ||
| Errors: []rule_tester.InvalidTestCaseError{ | ||
| {MessageId: "preferExpectErrorComment", Line: 3, Column: 3, EndLine: 3, EndColumn: 40}, | ||
| }, | ||
| }, | ||
| { | ||
| Code: `/* @ts-ignore */`, | ||
| Output: []string{ | ||
| `/* @ts-expect-error */`, | ||
| }, | ||
| Errors: []rule_tester.InvalidTestCaseError{ | ||
| {MessageId: "preferExpectErrorComment", Line: 1, Column: 1, EndLine: 1, EndColumn: 17}, | ||
| }, | ||
| }, | ||
| { | ||
| Code: ` | ||
| /** | ||
| * Explaining comment | ||
| * | ||
| * @ts-ignore */ | ||
| `, | ||
| Output: []string{ | ||
| ` | ||
| /** | ||
| * Explaining comment | ||
| * | ||
| * @ts-expect-error */ | ||
| `, | ||
| }, | ||
| Errors: []rule_tester.InvalidTestCaseError{ | ||
| {MessageId: "preferExpectErrorComment", Line: 2, Column: 1, EndLine: 5, EndColumn: 17}, | ||
| }, | ||
| }, | ||
| { | ||
| Code: `/* @ts-ignore in a single block */`, | ||
| Output: []string{ | ||
| `/* @ts-expect-error in a single block */`, | ||
| }, | ||
| Errors: []rule_tester.InvalidTestCaseError{ | ||
| {MessageId: "preferExpectErrorComment", Line: 1, Column: 1, EndLine: 1, EndColumn: 35}, | ||
| }, | ||
| }, | ||
| { | ||
| Code: ` | ||
| /* | ||
| // @ts-ignore in a block with single line comments */ | ||
| `, | ||
| Output: []string{ | ||
| ` | ||
| /* | ||
| // @ts-expect-error in a block with single line comments */ | ||
| `, | ||
| }, | ||
| Errors: []rule_tester.InvalidTestCaseError{ | ||
| {MessageId: "preferExpectErrorComment", Line: 2, Column: 1, EndLine: 3, EndColumn: 54}, | ||
| }, | ||
| }, | ||
| }) | ||
| } |
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.