From 17a8caa5dce338d1558bf13be21ee2a0c99b286b Mon Sep 17 00:00:00 2001 From: camchenry <1514176+camchenry@users.noreply.github.com> Date: Wed, 26 Nov 2025 09:23:33 +0000 Subject: [PATCH] feat(parser): add diagnostic for JSX identifiers with hyphens (#16133) Instead of just emitting "unexpected token", provide a better error message that says exactly what is wrong and how to fix the error. --- crates/oxc_parser/src/diagnostics.rs | 7 +++++++ crates/oxc_parser/src/jsx/mod.rs | 2 +- tasks/coverage/snapshots/parser_misc.snap | 3 ++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/crates/oxc_parser/src/diagnostics.rs b/crates/oxc_parser/src/diagnostics.rs index 40fa81f0a59b0..0f758cd1e2204 100644 --- a/crates/oxc_parser/src/diagnostics.rs +++ b/crates/oxc_parser/src/diagnostics.rs @@ -1124,3 +1124,10 @@ pub fn identifier_expected_after_question_dot(span: Span) -> OxcDiagnostic { .with_label(span) .with_help("Add an identifier after '?.'") } + +#[cold] +pub fn identifier_expected_jsx_no_hyphen(span: Span) -> OxcDiagnostic { + OxcDiagnostic::error("Identifiers in JSX cannot contain hyphens") + .with_label(span) + .with_help("Remove the hyphen from the identifier") +} diff --git a/crates/oxc_parser/src/jsx/mod.rs b/crates/oxc_parser/src/jsx/mod.rs index e6588dcc22744..1406b940a54bb 100644 --- a/crates/oxc_parser/src/jsx/mod.rs +++ b/crates/oxc_parser/src/jsx/mod.rs @@ -198,7 +198,7 @@ impl<'a> ParserImpl<'a> { let ident = self.parse_jsx_identifier(); // `` is a syntax error. if ident.name.contains('-') { - let error = diagnostics::unexpected_token(ident.span); + let error = diagnostics::identifier_expected_jsx_no_hyphen(ident.span); return self.fatal_error(error); } property = Some(ident); diff --git a/tasks/coverage/snapshots/parser_misc.snap b/tasks/coverage/snapshots/parser_misc.snap index 096358672c38c..906f2306dc0b1 100644 --- a/tasks/coverage/snapshots/parser_misc.snap +++ b/tasks/coverage/snapshots/parser_misc.snap @@ -3494,11 +3494,12 @@ Negative Passed: 120/120 (100.00%) ╰──── help: Remove this `?` - × Unexpected token + × Identifiers in JSX cannot contain hyphens ╭─[misc/fail/oxc-5355.jsx:1:6] 1 │ · ─────── ╰──── + help: Remove the hyphen from the identifier × Optional declaration is not allowed here ╭─[misc/fail/oxc-5955-1.ts:1:8]