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

Permit %%placeholder%% in left-hand-side of a let declaration #12725

Merged
merged 3 commits into from Feb 2, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/babel-parser/src/plugins/placeholders.js
Expand Up @@ -150,6 +150,28 @@ export default (superClass: Class<Parser>): Class<Parser> =>
* parser/statement.js *
* ============================================================ */

isLet(context: ?string): boolean {
if (super.isLet(context)) {
return true;
}

// Replicate the original checks that lead to looking ahead for an
// identifier.
if (!this.isContextual("let")) {
return false;
}
if (context) return false;

// Accept "let %%" as the start of "let %%placeholder%%", as though the
// placeholder were an identifier.
const nextToken = this.lookahead();
if (nextToken.type === tt.placeholder) {
return true;
}

return false;
}

verifyBreakContinue(node: N.BreakStatement | N.ContinueStatement) {
if (node.label && node.label.type === "Placeholder") return;
super.verifyBreakContinue(...arguments);
Expand Down
@@ -0,0 +1 @@
const %%LHS%% = %%RHS%%;
@@ -0,0 +1,44 @@
{
"type": "File",
"start":0,"end":24,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":24}},
"program": {
"type": "Program",
"start":0,"end":24,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":24}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start":0,"end":24,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":24}},
"declarations": [
{
"type": "VariableDeclarator",
"start":6,"end":23,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":23}},
"id": {
"type": "Placeholder",
"start":6,"end":13,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":13}},
"name": {
"type": "Identifier",
"start":8,"end":11,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":11},"identifierName":"LHS"},
"name": "LHS"
},
"expectedNode": "Pattern"
},
"init": {
"type": "Placeholder",
"start":16,"end":23,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":23}},
"name": {
"type": "Identifier",
"start":18,"end":21,"loc":{"start":{"line":1,"column":18},"end":{"line":1,"column":21},"identifierName":"RHS"},
"name": "RHS"
},
"expectedNode": "Expression"
}
}
],
"kind": "const"
}
],
"directives": []
}
}
@@ -0,0 +1 @@
if (cond) let;
@@ -0,0 +1,32 @@
{
"type": "File",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"program": {
"type": "Program",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "IfStatement",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"test": {
"type": "Identifier",
"start":4,"end":8,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":8},"identifierName":"cond"},
"name": "cond"
},
"consequent": {
"type": "ExpressionStatement",
"start":10,"end":14,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":14}},
"expression": {
"type": "Identifier",
"start":10,"end":13,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":13},"identifierName":"let"},
"name": "let"
}
},
"alternate": null
}
],
"directives": []
}
}
@@ -0,0 +1,2 @@
if (cond) let
%%LHS%% = %%RHS%%
@@ -0,0 +1,61 @@
{
"type": "File",
"start":0,"end":31,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":17}},
"program": {
"type": "Program",
"start":0,"end":31,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":17}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "IfStatement",
"start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}},
"test": {
"type": "Identifier",
"start":4,"end":8,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":8},"identifierName":"cond"},
"name": "cond"
},
"consequent": {
"type": "ExpressionStatement",
"start":10,"end":13,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":13}},
"expression": {
"type": "Identifier",
"start":10,"end":13,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":13},"identifierName":"let"},
"name": "let"
}
},
"alternate": null
},
{
"type": "ExpressionStatement",
"start":14,"end":31,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":17}},
"expression": {
"type": "AssignmentExpression",
"start":14,"end":31,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":17}},
"operator": "=",
"left": {
"type": "Placeholder",
"start":14,"end":21,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":7}},
"name": {
"type": "Identifier",
"start":16,"end":19,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":5},"identifierName":"LHS"},
"name": "LHS"
},
"expectedNode": "Pattern"
},
"right": {
"type": "Placeholder",
"start":24,"end":31,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":17}},
"name": {
"type": "Identifier",
"start":26,"end":29,"loc":{"start":{"line":2,"column":12},"end":{"line":2,"column":15},"identifierName":"RHS"},
"name": "RHS"
},
"expectedNode": "Expression"
}
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
if (cond) let %%LHS%% = %%RHS%%;
@@ -0,0 +1,64 @@
{
"type": "File",
"start":0,"end":32,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":32}},
"errors": [
"SyntaxError: Missing semicolon (1:13)"
],
"program": {
"type": "Program",
"start":0,"end":32,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":32}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "IfStatement",
"start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}},
"test": {
"type": "Identifier",
"start":4,"end":8,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":8},"identifierName":"cond"},
"name": "cond"
},
"consequent": {
"type": "ExpressionStatement",
"start":10,"end":13,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":13}},
"expression": {
"type": "Identifier",
"start":10,"end":13,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":13},"identifierName":"let"},
"name": "let"
}
},
"alternate": null
},
{
"type": "ExpressionStatement",
"start":14,"end":32,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":32}},
"expression": {
"type": "AssignmentExpression",
"start":14,"end":31,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":31}},
"operator": "=",
"left": {
"type": "Placeholder",
"start":14,"end":21,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":21}},
"name": {
"type": "Identifier",
"start":16,"end":19,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":19},"identifierName":"LHS"},
"name": "LHS"
},
"expectedNode": "Pattern"
},
"right": {
"type": "Placeholder",
"start":24,"end":31,"loc":{"start":{"line":1,"column":24},"end":{"line":1,"column":31}},
"name": {
"type": "Identifier",
"start":26,"end":29,"loc":{"start":{"line":1,"column":26},"end":{"line":1,"column":29},"identifierName":"RHS"},
"name": "RHS"
},
"expectedNode": "Expression"
}
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
let %%LHS%% = %%RHS%%;
@@ -0,0 +1,44 @@
{
"type": "File",
"start":0,"end":22,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":22}},
"program": {
"type": "Program",
"start":0,"end":22,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":22}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start":0,"end":22,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":22}},
"declarations": [
{
"type": "VariableDeclarator",
"start":4,"end":21,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":21}},
"id": {
"type": "Placeholder",
"start":4,"end":11,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":11}},
"name": {
"type": "Identifier",
"start":6,"end":9,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":9},"identifierName":"LHS"},
"name": "LHS"
},
"expectedNode": "Pattern"
},
"init": {
"type": "Placeholder",
"start":14,"end":21,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":21}},
"name": {
"type": "Identifier",
"start":16,"end":19,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":19},"identifierName":"RHS"},
"name": "RHS"
},
"expectedNode": "Expression"
}
}
],
"kind": "let"
}
],
"directives": []
}
}
@@ -0,0 +1 @@
let %LHS;
@@ -0,0 +1,32 @@
{
"type": "File",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":9}},
"program": {
"type": "Program",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":9}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":9}},
"expression": {
"type": "BinaryExpression",
"start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}},
"left": {
"type": "Identifier",
"start":0,"end":3,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":3},"identifierName":"let"},
"name": "let"
},
"operator": "%",
"right": {
"type": "Identifier",
"start":5,"end":8,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":8},"identifierName":"LHS"},
"name": "LHS"
}
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
var %%LHS%% = %%RHS%%;
@@ -0,0 +1,44 @@
{
"type": "File",
"start":0,"end":22,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":22}},
"program": {
"type": "Program",
"start":0,"end":22,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":22}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start":0,"end":22,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":22}},
"declarations": [
{
"type": "VariableDeclarator",
"start":4,"end":21,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":21}},
"id": {
"type": "Placeholder",
"start":4,"end":11,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":11}},
"name": {
"type": "Identifier",
"start":6,"end":9,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":9},"identifierName":"LHS"},
"name": "LHS"
},
"expectedNode": "Pattern"
},
"init": {
"type": "Placeholder",
"start":14,"end":21,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":21}},
"name": {
"type": "Identifier",
"start":16,"end":19,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":19},"identifierName":"RHS"},
"name": "RHS"
},
"expectedNode": "Expression"
}
}
],
"kind": "var"
}
],
"directives": []
}
}