Skip to content

Commit 29ff54d

Browse files
committed
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.
1 parent 0549ae5 commit 29ff54d

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

crates/oxc_parser/src/diagnostics.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,3 +1124,10 @@ pub fn identifier_expected_after_question_dot(span: Span) -> OxcDiagnostic {
11241124
.with_label(span)
11251125
.with_help("Add an identifier after '?.'")
11261126
}
1127+
1128+
#[cold]
1129+
pub fn identifier_expected_jsx_no_hyphen(span: Span) -> OxcDiagnostic {
1130+
OxcDiagnostic::error("Identifiers in JSX cannot contain hyphens")
1131+
.with_label(span)
1132+
.with_help("Remove the hyphen from the identifier")
1133+
}

crates/oxc_parser/src/jsx/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ impl<'a> ParserImpl<'a> {
198198
let ident = self.parse_jsx_identifier();
199199
// `<foo.bar- />` is a syntax error.
200200
if ident.name.contains('-') {
201-
let error = diagnostics::unexpected_token(ident.span);
201+
let error = diagnostics::identifier_expected_jsx_no_hyphen(ident.span);
202202
return self.fatal_error(error);
203203
}
204204
property = Some(ident);

tasks/coverage/snapshots/parser_misc.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3494,11 +3494,12 @@ Negative Passed: 120/120 (100.00%)
34943494
╰────
34953495
help: Remove this `?`
34963496
3497-
× Unexpected token
3497+
× Identifiers in JSX cannot contain hyphens
34983498
╭─[misc/fail/oxc-5355.jsx:1:6]
34993499
1<Foo.bar-baz />
35003500
· ───────
35013501
╰────
3502+
help: Remove the hyphen from the identifier
35023503
35033504
× Optional declaration is not allowed here
35043505
╭─[misc/fail/oxc-5955-1.ts:1:8]

0 commit comments

Comments
 (0)