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
2 changes: 1 addition & 1 deletion packages/babel-parser/src/parser/statement.ts
Expand Up @@ -516,7 +516,7 @@ export default abstract class StatementParser extends ExpressionParser {
const next = this.nextTokenStart();
const nextCh = this.codePointAtPos(next);
if (nextCh !== charCodes.leftSquareBracket) {
if (!allowDeclaration) break;
if (!allowDeclaration && this.hasFollowingLineBreak()) break;
if (
!this.chStartsBindingIdentifier(nextCh, next) &&
nextCh !== charCodes.leftCurlyBrace
Expand Down
Expand Up @@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":13,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":13,"index":13}},
"errors": [
"SyntaxError: Missing semicolon. (1:8)"
"SyntaxError: Lexical declaration cannot appear in a single-statement context. (1:5)"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The input is

foo: let bar;

Now we throw a better error here.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, this improves the parsed AST result at the same time.

],
"program": {
"type": "Program",
Expand All @@ -12,30 +12,29 @@
"body": [
{
"type": "LabeledStatement",
"start":0,"end":8,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":8,"index":8}},
"start":0,"end":13,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":13,"index":13}},
"body": {
"type": "ExpressionStatement",
"start":5,"end":8,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":8,"index":8}},
"expression": {
"type": "Identifier",
"start":5,"end":8,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":8,"index":8},"identifierName":"let"},
"name": "let"
}
"type": "VariableDeclaration",
"start":5,"end":13,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":13,"index":13}},
"declarations": [
{
"type": "VariableDeclarator",
"start":9,"end":12,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":12,"index":12}},
"id": {
"type": "Identifier",
"start":9,"end":12,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":12,"index":12},"identifierName":"bar"},
"name": "bar"
},
"init": null
}
],
"kind": "let"
},
"label": {
"type": "Identifier",
"start":0,"end":3,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":3,"index":3},"identifierName":"foo"},
"name": "foo"
}
},
{
"type": "ExpressionStatement",
"start":9,"end":13,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":13,"index":13}},
"expression": {
"type": "Identifier",
"start":9,"end":12,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":12,"index":12},"identifierName":"bar"},
"name": "bar"
}
}
],
"directives": []
Expand Down
Expand Up @@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":18,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":18,"index":18}},
"errors": [
"SyntaxError: Missing semicolon. (1:10)"
"SyntaxError: Lexical declaration cannot appear in a single-statement context. (1:7)"
],
"program": {
"type": "Program",
Expand All @@ -12,7 +12,7 @@
"body": [
{
"type": "IfStatement",
"start":0,"end":10,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":10,"index":10}},
"start":0,"end":18,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":18,"index":18}},
"test": {
"type": "NumericLiteral",
"start":4,"end":5,"loc":{"start":{"line":1,"column":4,"index":4},"end":{"line":1,"column":5,"index":5}},
Expand All @@ -23,38 +23,31 @@
"value": 1
},
"consequent": {
"type": "ExpressionStatement",
"start":7,"end":10,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":10,"index":10}},
"expression": {
"type": "Identifier",
"start":7,"end":10,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":10,"index":10},"identifierName":"let"},
"name": "let"
}
"type": "VariableDeclaration",
"start":7,"end":18,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":18,"index":18}},
"declarations": [
{
"type": "VariableDeclarator",
"start":11,"end":17,"loc":{"start":{"line":1,"column":11,"index":11},"end":{"line":1,"column":17,"index":17}},
"id": {
"type": "Identifier",
"start":11,"end":12,"loc":{"start":{"line":1,"column":11,"index":11},"end":{"line":1,"column":12,"index":12},"identifierName":"x"},
"name": "x"
},
"init": {
"type": "NumericLiteral",
"start":15,"end":17,"loc":{"start":{"line":1,"column":15,"index":15},"end":{"line":1,"column":17,"index":17}},
"extra": {
"rawValue": 10,
"raw": "10"
},
"value": 10
}
}
],
"kind": "let"
},
"alternate": null
},
{
"type": "ExpressionStatement",
"start":11,"end":18,"loc":{"start":{"line":1,"column":11,"index":11},"end":{"line":1,"column":18,"index":18}},
"expression": {
"type": "AssignmentExpression",
"start":11,"end":17,"loc":{"start":{"line":1,"column":11,"index":11},"end":{"line":1,"column":17,"index":17}},
"operator": "=",
"left": {
"type": "Identifier",
"start":11,"end":12,"loc":{"start":{"line":1,"column":11,"index":11},"end":{"line":1,"column":12,"index":12},"identifierName":"x"},
"name": "x"
},
"right": {
"type": "NumericLiteral",
"start":15,"end":17,"loc":{"start":{"line":1,"column":15,"index":15},"end":{"line":1,"column":17,"index":17}},
"extra": {
"rawValue": 10,
"raw": "10"
},
"value": 10
}
}
}
],
"directives": []
Expand Down
Expand Up @@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":19,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":19,"index":19}},
"errors": [
"SyntaxError: Missing semicolon. (1:12)"
"SyntaxError: Lexical declaration cannot appear in a single-statement context. (1:9)"
],
"program": {
"type": "Program",
Expand All @@ -12,45 +12,38 @@
"body": [
{
"type": "IfStatement",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":12,"index":12}},
"start":0,"end":19,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":19,"index":19}},
"test": {
"type": "BooleanLiteral",
"start":3,"end":7,"loc":{"start":{"line":1,"column":3,"index":3},"end":{"line":1,"column":7,"index":7}},
"value": true
},
"consequent": {
"type": "ExpressionStatement",
"start":9,"end":12,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":12,"index":12}},
"expression": {
"type": "Identifier",
"start":9,"end":12,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":12,"index":12},"identifierName":"let"},
"name": "let"
}
"type": "VariableDeclaration",
"start":9,"end":19,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":19,"index":19}},
"declarations": [
{
"type": "VariableDeclarator",
"start":13,"end":18,"loc":{"start":{"line":1,"column":13,"index":13},"end":{"line":1,"column":18,"index":18}},
"id": {
"type": "Identifier",
"start":13,"end":14,"loc":{"start":{"line":1,"column":13,"index":13},"end":{"line":1,"column":14,"index":14},"identifierName":"a"},
"name": "a"
},
"init": {
"type": "NumericLiteral",
"start":17,"end":18,"loc":{"start":{"line":1,"column":17,"index":17},"end":{"line":1,"column":18,"index":18}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
}
],
"kind": "let"
},
"alternate": null
},
{
"type": "ExpressionStatement",
"start":13,"end":19,"loc":{"start":{"line":1,"column":13,"index":13},"end":{"line":1,"column":19,"index":19}},
"expression": {
"type": "AssignmentExpression",
"start":13,"end":18,"loc":{"start":{"line":1,"column":13,"index":13},"end":{"line":1,"column":18,"index":18}},
"operator": "=",
"left": {
"type": "Identifier",
"start":13,"end":14,"loc":{"start":{"line":1,"column":13,"index":13},"end":{"line":1,"column":14,"index":14},"identifierName":"a"},
"name": "a"
},
"right": {
"type": "NumericLiteral",
"start":17,"end":18,"loc":{"start":{"line":1,"column":17,"index":17},"end":{"line":1,"column":18,"index":18}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
}
}
],
"directives": []
Expand Down
Expand Up @@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":32,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":32,"index":32}},
"errors": [
"SyntaxError: Missing semicolon. (1:13)"
"SyntaxError: Lexical declaration cannot appear in a single-statement context. (1:10)"
],
"program": {
"type": "Program",
Expand All @@ -12,51 +12,44 @@
"body": [
{
"type": "IfStatement",
"start":0,"end":13,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":13,"index":13}},
"start":0,"end":32,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":32,"index":32}},
"test": {
"type": "Identifier",
"start":4,"end":8,"loc":{"start":{"line":1,"column":4,"index":4},"end":{"line":1,"column":8,"index":8},"identifierName":"cond"},
"name": "cond"
},
"consequent": {
"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": "VariableDeclaration",
"start":10,"end":32,"loc":{"start":{"line":1,"column":10,"index":10},"end":{"line":1,"column":32,"index":32}},
"declarations": [
{
"type": "VariableDeclarator",
"start":14,"end":31,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":31,"index":31}},
"id": {
"type": "Placeholder",
"start":14,"end":21,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":21,"index":21}},
"name": {
"type": "Identifier",
"start":16,"end":19,"loc":{"start":{"line":1,"column":16,"index":16},"end":{"line":1,"column":19,"index":19},"identifierName":"LHS"},
"name": "LHS"
},
"expectedNode": "Pattern"
},
"init": {
"type": "Placeholder",
"start":24,"end":31,"loc":{"start":{"line":1,"column":24,"index":24},"end":{"line":1,"column":31,"index":31}},
"name": {
"type": "Identifier",
"start":26,"end":29,"loc":{"start":{"line":1,"column":26,"index":26},"end":{"line":1,"column":29,"index":29},"identifierName":"RHS"},
"name": "RHS"
},
"expectedNode": "Expression"
}
}
],
"kind": "let"
},
"alternate": null
},
{
"type": "ExpressionStatement",
"start":14,"end":32,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":32,"index":32}},
"expression": {
"type": "AssignmentExpression",
"start":14,"end":31,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":31,"index":31}},
"operator": "=",
"left": {
"type": "Placeholder",
"start":14,"end":21,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":21,"index":21}},
"name": {
"type": "Identifier",
"start":16,"end":19,"loc":{"start":{"line":1,"column":16,"index":16},"end":{"line":1,"column":19,"index":19},"identifierName":"LHS"},
"name": "LHS"
},
"expectedNode": "Pattern"
},
"right": {
"type": "Placeholder",
"start":24,"end":31,"loc":{"start":{"line":1,"column":24,"index":24},"end":{"line":1,"column":31,"index":31}},
"name": {
"type": "Identifier",
"start":26,"end":29,"loc":{"start":{"line":1,"column":26,"index":26},"end":{"line":1,"column":29,"index":29},"identifierName":"RHS"},
"name": "RHS"
},
"expectedNode": "Expression"
}
}
}
],
"directives": []
Expand Down