Skip to content

Commit 837ef21

Browse files
committed
fix(parser): reject yield and await in object destructing shorthand property parameters (#15221)
Reject cases like `function* y({yield}) {}` and `async function y({await}) {}` in scripts (in modules, these were already rejected as `yield` and `await` are reserved keywords).
1 parent 2d6d3a8 commit 837ef21

File tree

5 files changed

+49
-9
lines changed

5 files changed

+49
-9
lines changed

crates/oxc_parser/src/js/binding.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ impl<'a> ParserImpl<'a> {
130130

131131
let mut shorthand = false;
132132
let is_binding_identifier = self.cur_kind().is_binding_identifier();
133+
let key_cur_kind = self.cur_kind();
133134
let (key, computed) = self.parse_property_name();
134135

135136
let value = if is_binding_identifier && !self.at(Kind::Colon) {
@@ -138,6 +139,7 @@ impl<'a> ParserImpl<'a> {
138139
// ^ BindingIdentifier
139140
if let PropertyKey::StaticIdentifier(ident) = &key {
140141
shorthand = true;
142+
self.check_identifier_with_span(key_cur_kind, self.ctx, ident.span);
141143
let identifier =
142144
self.ast.binding_pattern_kind_binding_identifier(ident.span, ident.name);
143145
let left = self.ast.binding_pattern(identifier, NONE, false);

crates/oxc_parser/src/js/expression.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,17 @@ impl<'a> ParserImpl<'a> {
120120
}
121121

122122
pub(crate) fn check_identifier(&mut self, kind: Kind, ctx: Context) {
123+
self.check_identifier_with_span(kind, ctx, self.cur_token().span());
124+
}
125+
126+
pub(crate) fn check_identifier_with_span(&mut self, kind: Kind, ctx: Context, span: Span) {
123127
// It is a Syntax Error if this production has an [Await] parameter.
124128
if ctx.has_await() && kind == Kind::Await {
125-
self.error(diagnostics::identifier_async("await", self.cur_token().span()));
129+
self.error(diagnostics::identifier_async("await", span));
126130
}
127131
// It is a Syntax Error if this production has a [Yield] parameter.
128132
if ctx.has_yield() && kind == Kind::Yield {
129-
self.error(diagnostics::identifier_generator("yield", self.cur_token().span()));
133+
self.error(diagnostics::identifier_generator("yield", span));
130134
}
131135
}
132136

tasks/coverage/snapshots/parser_babel.snap

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@ commit: 4cc3d888
33
parser_babel Summary:
44
AST Parsed : 2422/2440 (99.26%)
55
Positive Passed: 2395/2440 (98.16%)
6-
Negative Passed: 1672/1752 (95.43%)
7-
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/es2015/uncategorised/335/input.js
8-
6+
Negative Passed: 1675/1752 (95.61%)
97
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/es2017/async-functions/async-await-as-arrow-binding-identifier/input.js
108

11-
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/es2017/async-functions/await-binding-inside-arrow-params-inside-async-arrow-params/input.js
12-
139
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/es2018/object-rest-spread/comma-after-rest/input.js
1410

1511
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/es2018/object-rest-spread/comma-after-spread-for-in/input.js
@@ -42,8 +38,6 @@ Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/es
4238

4339
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/es2026/explicit-resource-management/invalid-script-top-level-using-binding/input.js
4440

45-
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/esprima/es2015-generator/generator-parameter-binding-property-reserved/input.js
46-
4741
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/estree/class-private-property/typescript-invalid-abstract/input.ts
4842

4943
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/estree/class-private-property/typescript-invalid-abstract-babel-7/input.ts
@@ -4828,6 +4822,12 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
48284822
· ────
48294823
╰────
48304824

4825+
× Cannot use `yield` as an identifier in a generator context
4826+
╭─[babel/packages/babel-parser/test/fixtures/es2015/uncategorised/335/input.js:1:14]
4827+
1 │ function* y({yield}) {}
4828+
· ─────
4829+
╰────
4830+
48314831
× Bad escape sequence in untagged template literal
48324832
╭─[babel/packages/babel-parser/test/fixtures/es2015/uncategorised/339/input.js:1:2]
48334833
1 │ `\07`
@@ -4912,6 +4912,12 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
49124912
· ─────
49134913
╰────
49144914

4915+
× Cannot use `await` as an identifier in an async context
4916+
╭─[babel/packages/babel-parser/test/fixtures/es2015/uncategorised/361/input.js:1:9]
4917+
1 │ const { await } = foo();
4918+
· ─────
4919+
╰────
4920+
49154921
× The keyword 'await' is reserved
49164922
╭─[babel/packages/babel-parser/test/fixtures/es2015/uncategorised/361/input.js:1:9]
49174923
1 │ const { await } = foo();
@@ -5639,6 +5645,12 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
56395645
· ─────
56405646
╰────
56415647

5648+
× Cannot use `await` as an identifier in an async context
5649+
╭─[babel/packages/babel-parser/test/fixtures/es2017/async-functions/await-binding-inside-arrow-params-inside-async-arrow-params/input.js:1:15]
5650+
1 │ async (a = ({ await }) => {}) => {};
5651+
· ─────
5652+
╰────
5653+
56425654
× Cannot use `await` as an identifier in an async context
56435655
╭─[babel/packages/babel-parser/test/fixtures/es2017/async-functions/await-function-declaration-name-inside-async-function/input.js:2:12]
56445656
1 │ async function foo() {
@@ -9732,6 +9744,12 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
97329744
3 │ })
97339745
╰────
97349746

9747+
× Cannot use `yield` as an identifier in a generator context
9748+
╭─[babel/packages/babel-parser/test/fixtures/esprima/es2015-generator/generator-parameter-binding-property-reserved/input.js:1:13]
9749+
1 │ (function*({yield}) {})
9750+
· ─────
9751+
╰────
9752+
97359753
× Expected function name
97369754
╭─[babel/packages/babel-parser/test/fixtures/esprima/es2015-generator/generator-parameter-computed-property-name/input.js:2:13]
97379755
1 │ (function*() {

tasks/coverage/snapshots/parser_test262.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38948,6 +38948,14 @@ Expect to Parse: tasks/coverage/test262/test/annexB/language/expressions/assignm
3894838948
31 │
3894938949
╰────
3895038950

38951+
× Cannot use `await` as an identifier in an async context
38952+
╭─[test262/test/language/statements/variable/dstr/obj-ptrn-elem-id-static-init-await-invalid.js:25:10]
38953+
24 │ static {
38954+
25 │ var {await} = {};
38955+
· ─────
38956+
26 │ }
38957+
╰────
38958+
3895138959
× Cannot use await in class static initialization block
3895238960
╭─[test262/test/language/statements/variable/dstr/obj-ptrn-elem-id-static-init-await-invalid.js:25:10]
3895338961
24 │ static {

tasks/coverage/snapshots/parser_typescript.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14978,6 +14978,14 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/parser/ecmasc
1497814978
5 │ }
1497914979
╰────
1498014980

14981+
× Cannot use `await` as an identifier in an async context
14982+
╭─[typescript/tests/cases/conformance/classes/classStaticBlock/classStaticBlock22.ts:7:11]
14983+
6 │ static {
14984+
7 │ let { await } = {} as any; // illegal, cannot declare a new binding for await
14985+
· ─────
14986+
8 │ }
14987+
╰────
14988+
1498114989
× Cannot use `await` as an identifier in an async context
1498214990
╭─[typescript/tests/cases/conformance/classes/classStaticBlock/classStaticBlock22.ts:13:9]
1498314991
12 │ static {

0 commit comments

Comments
 (0)