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

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

* Test that "let" followed by modulo is still treated as an identifier

* More tests for edge-case handling of "let" with placeholders enabled
This commit is contained in:
Stuart Cook 2021-02-02 12:00:21 +11:00 committed by GitHub
parent ecfe20395b
commit 20664a430e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 375 additions and 0 deletions

View File

@ -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);

View File

@ -0,0 +1 @@
const %%LHS%% = %%RHS%%;

View File

@ -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": []
}
}

View File

@ -0,0 +1 @@
if (cond) let;

View File

@ -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": []
}
}

View File

@ -0,0 +1,2 @@
if (cond) let
%%LHS%% = %%RHS%%

View File

@ -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": []
}
}

View File

@ -0,0 +1 @@
if (cond) let %%LHS%% = %%RHS%%;

View File

@ -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": []
}
}

View File

@ -0,0 +1 @@
let %%LHS%% = %%RHS%%;

View File

@ -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": []
}
}

View File

@ -0,0 +1 @@
let %LHS;

View File

@ -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": []
}
}

View File

@ -0,0 +1 @@
var %%LHS%% = %%RHS%%;

View File

@ -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": []
}
}

View File

@ -393,5 +393,29 @@ describe("@babel/template", function () {
});
});
});
it("works in var declaration", () => {
const output = template("var %%LHS%% = %%RHS%%")({
LHS: t.identifier("x"),
RHS: t.numericLiteral(7),
});
expect(generator(output).code).toMatchInlineSnapshot(`"var x = 7;"`);
});
it("works in const declaration", () => {
const output = template("const %%LHS%% = %%RHS%%")({
LHS: t.identifier("x"),
RHS: t.numericLiteral(7),
});
expect(generator(output).code).toMatchInlineSnapshot(`"const x = 7;"`);
});
it("works in let declaration", () => {
const output = template("let %%LHS%% = %%RHS%%")({
LHS: t.identifier("x"),
RHS: t.numericLiteral(7),
});
expect(generator(output).code).toMatchInlineSnapshot(`"let x = 7;"`);
});
});
});