Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/oxc_parser/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ pub fn merge_conflict_marker(
diagnostic
}

#[cold]
pub fn jsx_in_non_jsx(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Unexpected JSX expression")
.with_label(span)
.with_help("JSX syntax is disabled and should be enabled via the parser options")
}

#[cold]
pub fn expect_token(x0: &str, x1: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("Expected `{x0}` but found `{x1}`"))
Expand Down
11 changes: 10 additions & 1 deletion crates/oxc_parser/src/js/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,16 @@ impl<'a> ParserImpl<'a> {
if self.is_ts {
return self.parse_ts_type_assertion();
}
self.unexpected()

let checkpoint = self.checkpoint_with_error_recovery();
let start = self.start_span();
self.parse_jsx_expression();
if self.fatal_error.is_none() {
self.fatal_error(diagnostics::jsx_in_non_jsx(self.end_span(start)))
} else {
self.rewind(checkpoint);
self.unexpected()
}
}
Kind::Await if self.is_await_expression() => self.parse_await_expression(lhs_span),
_ => self.parse_update_expression(lhs_span),
Expand Down
1 change: 1 addition & 0 deletions tasks/coverage/misc/fail/jsx-in-js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = <Foo />;
1 change: 1 addition & 0 deletions tasks/coverage/misc/fail/jsx-like-in-js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = <Foo;
26 changes: 16 additions & 10 deletions tasks/coverage/snapshots/parser_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12607,38 +12607,44 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
· ────
╰────

× Unexpected token
× Unexpected JSX expression
╭─[babel/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-fragment/input.js:3:5]
2 │ return (
3 │ <>Hello</>
· ─
· ──────────
4 │ );
╰────
help: JSX syntax is disabled and should be enabled via the parser options

× Unexpected token
× Unexpected JSX expression
╭─[babel/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-jsx-expression/input.js:1:1]
1 │ <div>{name}</div>
· ─
· ─────────────────
╰────
help: JSX syntax is disabled and should be enabled via the parser options

× Unexpected token
╭─[babel/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-type-param/input.js:1:1]
1 │ <div>() => {}
· ─
╰────

× Unexpected token
× Unexpected JSX expression
╭─[babel/packages/babel-parser/test/fixtures/jsx/errors/_no_plugin/input.js:1:1]
1 │ <div></div>
· ─
· ───────────
╰────
help: JSX syntax is disabled and should be enabled via the parser options

× Unexpected token
× Unexpected JSX expression
╭─[babel/packages/babel-parser/test/fixtures/jsx/errors/_no_plugin-non-BMP-identifier/input.js:1:1]
1 │ <
· ─
2 │ 𠮷
1 │ ╭─▶ <
2 │ │ 𠮷
3 │ │ ></
4 │ │ 𠮷
5 │ ╰─▶ >
╰────
help: JSX syntax is disabled and should be enabled via the parser options

× Unexpected token. Did you mean `{'>'}` or `&gt;`?
╭─[babel/packages/babel-parser/test/fixtures/jsx/errors/unclosed-jsx-element/input.js:1:10]
Expand Down
15 changes: 14 additions & 1 deletion tasks/coverage/snapshots/parser_misc.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
parser_misc Summary:
AST Parsed : 49/49 (100.00%)
Positive Passed: 49/49 (100.00%)
Negative Passed: 108/108 (100.00%)
Negative Passed: 110/110 (100.00%)

× Cannot assign to 'arguments' in strict mode
╭─[misc/fail/arguments-eval.ts:1:10]
Expand Down Expand Up @@ -180,6 +180,19 @@ Negative Passed: 108/108 (100.00%)
· ╰── `,` or `]` expected
╰────

× Unexpected JSX expression
╭─[misc/fail/jsx-in-js.js:1:20]
1 │ export const foo = <Foo />;
· ───────
╰────
help: JSX syntax is disabled and should be enabled via the parser options

× Unexpected token
╭─[misc/fail/jsx-like-in-js.js:1:20]
1 │ export const foo = <Foo;
· ─
╰────

× Expected `:` but found `EOF`
╭─[misc/fail/missing-conditional-alternative-type.ts:2:1]
1 │ type A = 1 extends 2 ? 3
Expand Down
5 changes: 3 additions & 2 deletions tasks/coverage/snapshots/parser_typescript.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9435,11 +9435,12 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/statements/Va
· ─
╰────

× Unexpected token
× Unexpected JSX expression
╭─[typescript/tests/cases/compiler/parseJsxElementInUnaryExpressionNoCrash2.ts:1:2]
1 │ ~<></> <
· ─
· ─────
╰────
help: JSX syntax is disabled and should be enabled via the parser options

× Unexpected token
╭─[typescript/tests/cases/compiler/parseJsxElementInUnaryExpressionNoCrash3.ts:1:2]
Expand Down
Loading