diff --git a/crates/oxc_parser/src/diagnostics.rs b/crates/oxc_parser/src/diagnostics.rs index 08e14dcb7b4f7..e633628eb5343 100644 --- a/crates/oxc_parser/src/diagnostics.rs +++ b/crates/oxc_parser/src/diagnostics.rs @@ -44,6 +44,13 @@ pub fn expect_token(x0: &str, x1: &str, span: Span) -> OxcDiagnostic { .with_label(span.label(format!("`{x0}` expected"))) } +#[cold] +pub fn unexpected_trailing_comma(name: &'static str, span: Span) -> OxcDiagnostic { + OxcDiagnostic::error(format!("{name} may not have a trailing comma.")) + .with_label(span) + .with_help("Remove the trailing comma here") +} + #[cold] pub fn invalid_escape_sequence(span: Span) -> OxcDiagnostic { OxcDiagnostic::error("Invalid escape sequence").with_label(span) @@ -236,9 +243,7 @@ pub fn spread_last_element(span: Span) -> OxcDiagnostic { #[cold] pub fn rest_element_trailing_comma(span: Span) -> OxcDiagnostic { - OxcDiagnostic::error("A rest parameter or binding pattern may not have a trailing comma.") - .with_label(span) - .with_help("Remove the trailing comma here") + unexpected_trailing_comma("A rest parameter or binding pattern", span) } #[cold] diff --git a/crates/oxc_parser/src/js/expression.rs b/crates/oxc_parser/src/js/expression.rs index 364dcd697c3d0..23f9edad07a17 100644 --- a/crates/oxc_parser/src/js/expression.rs +++ b/crates/oxc_parser/src/js/expression.rs @@ -217,7 +217,10 @@ impl<'a> ParserImpl<'a> { }); if let Some(comma_span) = comma_span { - let error = diagnostics::expect_token(")", ",", self.end_span(comma_span)); + let error = diagnostics::unexpected_trailing_comma( + "Parenthesized expressions", + self.end_span(comma_span), + ); return self.fatal_error(error); } diff --git a/crates/oxc_parser/src/ts/types.rs b/crates/oxc_parser/src/ts/types.rs index c7c3bd63249d6..ddb265d910d3d 100644 --- a/crates/oxc_parser/src/ts/types.rs +++ b/crates/oxc_parser/src/ts/types.rs @@ -1222,7 +1222,10 @@ impl<'a> ParserImpl<'a> { Self::parse_ts_index_signature_name, ); if let Some(comma_span) = comma_span { - self.error(diagnostics::expect_token("]", ",", self.end_span(comma_span))); + self.error(diagnostics::unexpected_trailing_comma( + "Index signature declarations", + self.end_span(comma_span), + )); } self.expect(Kind::RBrack); if params.len() != 1 { diff --git a/tasks/coverage/snapshots/parser_babel.snap b/tasks/coverage/snapshots/parser_babel.snap index b75c44a79cda5..dd46de13a4fec 100644 --- a/tasks/coverage/snapshots/parser_babel.snap +++ b/tasks/coverage/snapshots/parser_babel.snap @@ -5912,12 +5912,12 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc · ─ ╰──── - × Expected `)` but found `,` + × Parenthesized expressions may not have a trailing comma. ╭─[babel/packages/babel-parser/test/fixtures/es2017/trailing-function-commas/7/input.js:1:7] 1 │ ('foo',) - · ┬ - · ╰── `)` expected + · ─ ╰──── + help: Remove the trailing comma here × `await` is only allowed within async functions and at the top levels of modules ╭─[babel/packages/babel-parser/test/fixtures/es2018/async-generators/for-await-async-context/input.js:2:7] diff --git a/tasks/coverage/snapshots/parser_typescript.snap b/tasks/coverage/snapshots/parser_typescript.snap index 67e8317364c4c..4f9007c477fad 100644 --- a/tasks/coverage/snapshots/parser_typescript.snap +++ b/tasks/coverage/snapshots/parser_typescript.snap @@ -8738,32 +8738,32 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/parser/ecmasc 3 │ } ╰──── - × Expected `]` but found `,` + × Index signature declarations may not have a trailing comma. ╭─[typescript/tests/cases/compiler/indexSignatureWithTrailingComma.ts:2:17] 1 │ type A = { 2 │ [key: string,]: string; - · ┬ - · ╰── `]` expected + · ─ 3 │ }; ╰──── + help: Remove the trailing comma here - × Expected `]` but found `,` + × Index signature declarations may not have a trailing comma. ╭─[typescript/tests/cases/compiler/indexSignatureWithTrailingComma.ts:6:17] 5 │ interface B { 6 │ [key: string,]: string; - · ┬ - · ╰── `]` expected + · ─ 7 │ } ╰──── + help: Remove the trailing comma here - × Expected `]` but found `,` + × Index signature declarations may not have a trailing comma. ╭─[typescript/tests/cases/compiler/indexSignatureWithTrailingComma.ts:10:17] 9 │ class C { 10 │ [key: string,]: null; - · ┬ - · ╰── `]` expected + · ─ 11 │ } ╰──── + help: Remove the trailing comma here × TS(1021): An index signature must have a type annotation. ╭─[typescript/tests/cases/compiler/indexSignatureWithoutTypeAnnotation1.ts:2:3] @@ -20386,14 +20386,14 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/parser/ecmasc 8 │ this += value; ╰──── - × Expected `)` but found `,` + × Parenthesized expressions may not have a trailing comma. ╭─[typescript/tests/cases/conformance/expressions/commaOperator/commaOperatorWithoutOperand.ts:9:5] 8 │ // Missing the second operand 9 │ (ANY, ); - · ┬ - · ╰── `)` expected + · ─ 10 │ (BOOLEAN, ); ╰──── + help: Remove the trailing comma here × 'with' statements are not allowed ╭─[typescript/tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts:2:1] diff --git a/tasks/track_memory_allocations/allocs_parser.snap b/tasks/track_memory_allocations/allocs_parser.snap index ffda685d691a2..976faf52bc98c 100644 --- a/tasks/track_memory_allocations/allocs_parser.snap +++ b/tasks/track_memory_allocations/allocs_parser.snap @@ -2,7 +2,7 @@ File | File size || Sys allocs | Sys reallocs | ------------------------------------------------------------------------------------------------------------------------------------------- checker.ts | 2.92 MB || 10165 | 21 || 268665 | 23341 -cal.com.tsx | 1.06 MB || 2209 | 61 || 138188 | 13712 +cal.com.tsx | 1.06 MB || 2210 | 62 || 138188 | 13712 RadixUIAdoptionSection.jsx | 2.52 kB || 1 | 0 || 365 | 66