fix(linter/plugins): use correct ScriptKind for tokens#17185
fix(linter/plugins): use correct ScriptKind for tokens#17185overlookmotel merged 9 commits intooxc-project:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug where the TypeScript parser always used ScriptKind.TSX regardless of file extension, causing generic arrow functions in .ts files to be incorrectly parsed and create overlapping JsxText tokens with comments.
Key Changes:
- Added
getScriptKind()function to determine correct TypeScriptScriptKindbased on file extension - Modified
parseTokens()to use extension-specificScriptKindinstead of hardcodedTSX
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| apps/oxlint/src-js/plugins/tokens_parse.ts | Implements getScriptKind() function and updates parser to use correct ScriptKind |
| apps/oxlint/test/tokens_script_kind.test.ts | Adds comprehensive test coverage for ScriptKind detection and regression tests |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
I'm not sure this is the very best solution, but it does address a real issue with incorrect parsing. |
overlookmotel
left a comment
There was a problem hiding this comment.
I'm afraid this is not the right way to solve this. We need to get the "is it JSX" flag from the Program. It's not completely trivial, hence why I left it as a TODO.
|
I've marked it as "requested changes" not necessarily to request that you make the changes, but just to stop the PR getting merged in current state by accident. If you want to tackle it, need to create a I don't know if that's enough to go on... |
Merging this PR will not alter performance
Comparing Footnotes
|
There was a problem hiding this comment.
Thank you very much for working on this.
Actually - and sorry to change my mind - there's an easier way to do this which I hadn't spotted before.
RawTransferMetadata already contains an is_ts field. We should add an is_jsx field too.
For convoluted reasons, there are 2 copies of RawTransferMetadata in apps/oxlint and napi/parser. Both must be identical, so you'd need to add the field to both.
Then get ast_tools codegen to generate an IS_JSX_FLAG_POS constant, same as for IS_TS_FLAG_POS.
Please also run the following after making the change, and commit any change to the conformance snapshot:
cd apps/oxlint
pnpm run init-conformance
pnpm run build-conformance
pnpm run conformanceinit-conformance only needs to be run once - it initializes the ESLint submodule. If you make changes after that, just run the last 2 commands again.
I don't know if this is covered by ESLint's conformance tests, but it'd be good to find out!
ddb614f to
bf2fbcb
Compare
a69742a to
1a81946
Compare
|
I think CI fail is caused by an oddity in snapshot encoding. If you build |
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1a81946 to
3cf7e06
Compare
|
You've waited a long time for review, so I've taken the liberty of making the changes I requested myself. Will merge once CI passes. |
Summary
When parsing tokens for JS plugins, the TypeScript parser was always using
ScriptKind.TSXregardless of file extension. This caused TypeScript to incorrectly parse generic arrow functions like<T>() => {}in.tsfiles, creating bogusJsxTexttokens that overlap with comments.The fix reads the JSX and TypeScript flags from the buffer metadata:
isJsx()reads theis_jsxflag fromRawTransferMetadataisTypescript()reads theis_tsflag fromRawTransferMetadataThis ensures the
ScriptKindreflects what Oxc actually parsed, not just what the file extension suggests.The Bug
Using the wrong ScriptKind (e.g., TSX for a .ts file) causes TypeScript to parse generic arrow functions like
<T>() => {}incorrectly, creating bogusJsxTexttokens that overlap with comments. This manifests as errors like:Changes
is_jsxfield toRawTransferMetadatastruct (in bothnapi/parserandcrates/oxc_linter)IS_JSX_FLAG_POSconstant to generated constants (viaast_tools)isJsx()andisTypescript()helper functions insource_code.tsgetScriptKind()intokens_parse.tsto use buffer flags instead of file extension matchinggeneric_arrow.tstest fileTest plan
generic_arrow.tstest file that triggers the bug when parsed as TSX🤖 Generated with Claude Code