Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 19, 2025

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:

enum a {} // ✅ Correctly errors with "Unexpected token"

However, when the same syntax was exported, it was incorrectly accepted:

export enum a {} // ❌ Was incorrectly parsed successfully
export type B = {}; // ❌ Was incorrectly parsed successfully  
export interface C {}; // ❌ Was incorrectly parsed successfully

Root Cause

The issue occurred in the export declaration parsing path. When parsing export <declaration>, the code calls parse_declaration in ts/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_declaration for the following constructs:

  • enum declarations → TS8003: "'enum' can only be used in TypeScript files."
  • type alias declarations → TS8004: "'type' alias can only be used in TypeScript files."
  • interface declarations → TS8005: "'interface' can only be used in TypeScript files."
  • module declarations → TS8006: "'module' can only be used in TypeScript files."
  • namespace declarations → 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:

// JavaScript files now properly error:
export enum a {} // ❌ TS(8003): 'enum' can only be used in TypeScript files.
export type B = {}; // ❌ TS(8004): 'type' alias can only be used in TypeScript files.
export interface C {}; // ❌ TS(8005): 'interface' can only be used in TypeScript files.
// TypeScript files continue to work:
export enum a {} // ✅ Parses successfully
export type B = {}; // ✅ Parses successfully  
export interface C {}; // ✅ Parses successfully

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.

@graphite-app
Copy link
Contributor

graphite-app bot commented Aug 19, 2025

How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

Copilot AI and others added 3 commits August 19, 2025 07:00
…port declarations

Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
Add checks for TypeScript-only syntax when used in export declarations in JavaScript files:
- export enum A {}
- export type B = {}
- export interface C {}
- export module D {}
- export namespace E {}

These now properly error with TypeScript-specific diagnostic messages.

Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
Copilot AI changed the title [WIP] parser: export enum a {} is not rejected for lang=js parser: Reject TypeScript-only syntax in JavaScript export declarations Aug 19, 2025
Copilot AI requested a review from Boshen August 19, 2025 07:17
Copilot finished work on behalf of Boshen August 19, 2025 07:17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm I should git ignore but cache this in CI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

parser: export enum a {} is not rejected for lang=js

2 participants