Skip to content

Commit cb17dae

Browse files
committed
fix(parser): report error on malformed template expressions (#11540)
fixes #11538
1 parent 993204c commit cb17dae

File tree

5 files changed

+22
-14
lines changed

5 files changed

+22
-14
lines changed

crates/oxc_parser/src/js/expression.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -471,18 +471,16 @@ impl<'a> ParserImpl<'a> {
471471
}
472472
Kind::TemplateMiddle => {
473473
quasis.push(self.parse_template_element(tagged));
474-
}
475-
Kind::Eof => {
476-
self.expect(Kind::TemplateTail);
477-
break;
478-
}
479-
_ => {
480474
// TemplateMiddle Expression[+In, ?Yield, ?Await]
481475
let expr =
482476
self.context(Context::In, Context::empty(), Self::parse_expr);
483477
expressions.push(expr);
484478
self.re_lex_template_substitution_tail();
485479
}
480+
_ => {
481+
self.expect(Kind::TemplateTail);
482+
break;
483+
}
486484
}
487485
}
488486
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const y = `foo ${x function() {} function () {}}`;

tasks/coverage/snapshots/parser_babel.snap

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4459,10 +4459,11 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
44594459
1 │ `hello ${10 `test`
44604460
╰────
44614461

4462-
× Unexpected token
4462+
× Expected `}` but found `;`
44634463
╭─[babel/packages/babel-parser/test/fixtures/es2015/uncategorised/267/input.js:1:12]
44644464
1 │ `hello ${10;test`
4465-
· ─
4465+
· ┬
4466+
· ╰── `}` expected
44664467
╰────
44674468

44684469
× Unexpected token
@@ -9516,10 +9517,11 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
95169517
· ──────
95179518
╰────
95189519

9519-
× Unexpected token
9520+
× Expected `}` but found `;`
95209521
╭─[babel/packages/babel-parser/test/fixtures/esprima/es2015-template-literals/unclosed-interpolation/input.js:1:12]
95219522
1 │ `hello ${10;test`
9522-
· ─
9523+
· ┬
9524+
· ╰── `}` expected
95239525
╰────
95249526

95259527
× Expected `}` but found `EOF`

tasks/coverage/snapshots/parser_misc.snap

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
parser_misc Summary:
22
AST Parsed : 40/40 (100.00%)
33
Positive Passed: 40/40 (100.00%)
4-
Negative Passed: 39/39 (100.00%)
4+
Negative Passed: 40/40 (100.00%)
55

66
× Identifier `b` has already been declared
77
╭─[misc/fail/oxc-10159.js:1:22]
@@ -68,6 +68,13 @@ Negative Passed: 39/39 (100.00%)
6868
2 │ import defer { x } from "x";
6969
╰────
7070
71+
× Expected `}` but found `function`
72+
╭─[misc/fail/oxc-11538.js:1:20]
73+
1const y = `foo ${x function() {} function () {}}`;
74+
· ────┬───
75+
· ╰── `}` expected
76+
╰────
77+
7178
× Unexpected token
7279
╭─[misc/fail/oxc-169.js:2:1]
7380
11<(V=82<<t-j0<(V=$<LBI<(V=ut<I<(V=$<LBI<(V=uIV=82<<t-j0<(V=$<LBI<(V=ut<I<(V=$<LBI<(V<II>

tasks/coverage/snapshots/parser_typescript.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17766,7 +17766,7 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/private
1776617766
· ─
1776717767
╰────
1776817768

17769-
× Expected `}` but found `EOF`
17769+
× Unexpected token
1777017770
╭─[typescript/tests/cases/compiler/taggedTemplatesWithIncompleteTemplateExpressions3.ts:5:23]
1777117771
4 │ // Incomplete call, not enough parameters.
1777217772
5 │ f `123qdawdrqw${ 1 }${
@@ -17786,13 +17786,13 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/private
1778617786
· ─
1778717787
╰────
1778817788

17789-
× Expected `}` but found `EOF`
17789+
× Unexpected token
1779017790
╭─[typescript/tests/cases/compiler/taggedTemplatesWithIncompleteTemplateExpressions5.ts:5:30]
1779117791
4 │ // Incomplete call, but too many parameters.
1779217792
5 │ f `123qdawdrqw${ 1 }${ 2 }${
1779317793
╰────
1779417794

17795-
× Expected `}` but found `EOF`
17795+
× Unexpected token
1779617796
╭─[typescript/tests/cases/compiler/taggedTemplatesWithIncompleteTemplateExpressions6.ts:5:23]
1779717797
4 │ // Incomplete call, not enough parameters, at EOF.
1779817798
5 │ f `123qdawdrqw${ 1 }${

0 commit comments

Comments
 (0)