parser: Reject TypeScript-only syntax in JavaScript export declarations #13206
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.
This PR fixes an issue where TypeScript-only syntax like
export enum a {}was not being rejected when parsing JavaScript files (lang=js).Problem
The parser correctly rejected standalone TypeScript syntax in JavaScript files:
However, when the same syntax was exported, it was incorrectly accepted:
Root Cause
The issue occurred in the export declaration parsing path. When parsing
export <declaration>, the code callsparse_declarationints/statement.rs, which directly parsed TypeScript-only constructs without checking if the parser was in TypeScript mode (is_ts).Solution
Added TypeScript mode checks in
parse_declarationfor the following constructs:enumdeclarations → TS8003: "'enum' can only be used in TypeScript files."typealias declarations → TS8004: "'type' alias can only be used in TypeScript files."interfacedeclarations → TS8005: "'interface' can only be used in TypeScript files."moduledeclarations → TS8006: "'module' can only be used in TypeScript files."namespacedeclarations → TS8007: "'namespace' can only be used in TypeScript files."The parser still constructs a complete AST (for tooling compatibility) but emits appropriate TypeScript-specific diagnostic messages when these constructs are used in JavaScript files.
Verification
After the fix:
Fixes #13205.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.