fix: remove unexpected space in output format string#281
fix: remove unexpected space in output format string#281hardfist merged 2 commits intoweb-infra-dev:mainfrom
Conversation
✅ Deploy Preview for rslint canceled.
|
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a formatting issue in the autofix functionality of the no-unnecessary-type-assertion rule where extra spaces were being left in the output after removing unnecessary type assertions. The fix ensures that when removing type assertions like foo() as number, the result is properly formatted as foo(); instead of foo() ;.
Key changes:
- Improved whitespace handling in the autofix logic to include preceding spaces when removing 'as' keyword
- Simplified the fix generation to use a single removal range instead of multiple separate fixes
- Updated test expectations to reflect the corrected output format
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| no_unnecessary_type_assertion.go | Enhanced autofix logic to properly handle whitespace removal before 'as' keyword and simplified fix generation |
| no_unnecessary_type_assertion_test.go | Updated test case expectation to reflect the corrected output format without extra spaces |
...rnal/plugins/typescript/rules/no_unnecessary_type_assertion/no_unnecessary_type_assertion.go
Outdated
Show resolved
Hide resolved
e0e5af3 to
9a5f9f4
Compare
|
Updated: I tested the original issue (#280) locally with the fixed build bin and it was verified to be resolved: Before
After
|
Improves the autofix for no-unnecessary-type-assertion rule to properly remove whitespace before 'as' keyword, preventing extra spaces in output. Before: foo() as number -> foo() ; After: foo() as number -> foo(); refactor: remove redundant comments from no_unnecessary_type_assertion Remove duplicate explanatory comments that were repeating the obvious functionality of the code below them.
Add bounds check before accessing sourceText[startPos-2] to prevent potential panic when startPos-2 < 0.
88a844e to
60ba29e
Compare
| sourceText := ctx.SourceFile.Text() | ||
| startPos := asKeywordRange.Pos() | ||
|
|
||
| if startPos > expression.End() && sourceText[startPos-1] == ' ' { |
There was a problem hiding this comment.
this seems can't handle following case well
const foo = 3 as 3; // multi space
const foo = 3 /* some comment here*/ as 3 // contains commentthe origin typescript-eslint implementation use a util to handle space and comment https://github.com/typescript-eslint/typescript-eslint/blob/b2ee794265c4c727009e65a4eb5f06fad9686cf8/packages/eslint-plugin/src/rules/no-unnecessary-type-assertion.ts#L304
|
I'm gonna merge this first and provide utils to simplify format logic(align with typeccript-eslint) in following pr |
|
@hardfist Thanks! I am still working on some releases this week and haven't had time to follow up. If this problem still exists by then, I will be happy to contribute. |


Summary
Improves the autofix for
no-unnecessary-type-assertionrule to properly remove whitespace before 'as' keyword, preventing extra spaces in output.Close: #280
foo() as number -> foo() ;foo() as number -> foo();Edge Case
This Pull Request does not solve an edge case: