Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse using[foo] as computed member expression #15225

Merged
merged 13 commits into from Nov 28, 2022
Merged
22 changes: 16 additions & 6 deletions packages/babel-parser/src/parser/statement.ts
Expand Up @@ -498,14 +498,24 @@ export default abstract class StatementParser extends ExpressionParser {
node as Undone<N.VariableDeclaration>,
"using",
);
case tt._let:
if (
this.state.containsEsc ||
!allowDeclaration ||
!this.hasFollowingBindingAtom()
) {
case tt._let: {
if (this.state.containsEsc) {
break;
}
// `let [` is an explicit negative lookahead for
// ExpressionStatement, so special-case it first.
const next = this.nextTokenStart();
const nextCh = this.codePointAtPos(next);
if (nextCh !== charCodes.leftSquareBracket) {
if (!allowDeclaration) break;
if (
!this.chStartsBindingIdentifier(nextCh, next) &&
nextCh !== charCodes.leftCurlyBrace
) {
break;
}
}
}
// fall through
case tt._const: {
if (!allowDeclaration) {
Expand Down
@@ -0,0 +1,3 @@
do let
[x] = 0
while (false);
@@ -0,0 +1,56 @@
{
"type": "File",
"start":0,"end":29,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":14,"index":29}},
"errors": [
"SyntaxError: Lexical declaration cannot appear in a single-statement context. (1:3)"
],
"program": {
"type": "Program",
"start":0,"end":29,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":14,"index":29}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "DoWhileStatement",
"start":0,"end":29,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":14,"index":29}},
"body": {
"type": "VariableDeclaration",
"start":3,"end":14,"loc":{"start":{"line":1,"column":3,"index":3},"end":{"line":2,"column":7,"index":14}},
"declarations": [
{
"type": "VariableDeclarator",
"start":7,"end":14,"loc":{"start":{"line":2,"column":0,"index":7},"end":{"line":2,"column":7,"index":14}},
"id": {
"type": "ArrayPattern",
"start":7,"end":10,"loc":{"start":{"line":2,"column":0,"index":7},"end":{"line":2,"column":3,"index":10}},
"elements": [
{
"type": "Identifier",
"start":8,"end":9,"loc":{"start":{"line":2,"column":1,"index":8},"end":{"line":2,"column":2,"index":9},"identifierName":"x"},
"name": "x"
}
]
},
"init": {
"type": "NumericLiteral",
"start":13,"end":14,"loc":{"start":{"line":2,"column":6,"index":13},"end":{"line":2,"column":7,"index":14}},
"extra": {
"rawValue": 0,
"raw": "0"
},
"value": 0
}
}
],
"kind": "let"
},
"test": {
"type": "BooleanLiteral",
"start":22,"end":27,"loc":{"start":{"line":3,"column":7,"index":22},"end":{"line":3,"column":12,"index":27}},
"value": false
}
}
],
"directives": []
}
}
@@ -0,0 +1,2 @@
while (0) let
\u0061 = 1;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently Babel parses it as a WhileStatement including let variable declaration. This is incorrect as it should share the AST structure with

while (0) let
a = 1;

which consists of a WhileStatement and an ExpressionStatement.

@@ -0,0 +1,58 @@
{
"type": "File",
"start":0,"end":25,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":11,"index":25}},
"program": {
"type": "Program",
"start":0,"end":25,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":11,"index":25}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "WhileStatement",
"start":0,"end":13,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":13,"index":13}},
"test": {
"type": "NumericLiteral",
"start":7,"end":8,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":8,"index":8}},
"extra": {
"rawValue": 0,
"raw": "0"
},
"value": 0
},
"body": {
"type": "ExpressionStatement",
"start":10,"end":13,"loc":{"start":{"line":1,"column":10,"index":10},"end":{"line":1,"column":13,"index":13}},
"expression": {
"type": "Identifier",
"start":10,"end":13,"loc":{"start":{"line":1,"column":10,"index":10},"end":{"line":1,"column":13,"index":13},"identifierName":"let"},
"name": "let"
}
}
},
{
"type": "ExpressionStatement",
"start":14,"end":25,"loc":{"start":{"line":2,"column":0,"index":14},"end":{"line":2,"column":11,"index":25}},
"expression": {
"type": "AssignmentExpression",
"start":14,"end":24,"loc":{"start":{"line":2,"column":0,"index":14},"end":{"line":2,"column":10,"index":24}},
"operator": "=",
"left": {
"type": "Identifier",
"start":14,"end":20,"loc":{"start":{"line":2,"column":0,"index":14},"end":{"line":2,"column":6,"index":20},"identifierName":"a"},
"name": "a"
},
"right": {
"type": "NumericLiteral",
"start":23,"end":24,"loc":{"start":{"line":2,"column":9,"index":23},"end":{"line":2,"column":10,"index":24}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
}
}
],
"directives": []
}
}