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:
parent
ecfe20395b
commit
20664a430e
@ -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);
|
||||
|
||||
1
packages/babel-parser/test/fixtures/placeholders/variable/const-init/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/placeholders/variable/const-init/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
const %%LHS%% = %%RHS%%;
|
||||
44
packages/babel-parser/test/fixtures/placeholders/variable/const-init/output.json
vendored
Normal file
44
packages/babel-parser/test/fixtures/placeholders/variable/const-init/output.json
vendored
Normal 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": []
|
||||
}
|
||||
}
|
||||
1
packages/babel-parser/test/fixtures/placeholders/variable/let-context-1/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/placeholders/variable/let-context-1/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
if (cond) let;
|
||||
32
packages/babel-parser/test/fixtures/placeholders/variable/let-context-1/output.json
vendored
Normal file
32
packages/babel-parser/test/fixtures/placeholders/variable/let-context-1/output.json
vendored
Normal 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": []
|
||||
}
|
||||
}
|
||||
2
packages/babel-parser/test/fixtures/placeholders/variable/let-context-2/input.js
vendored
Normal file
2
packages/babel-parser/test/fixtures/placeholders/variable/let-context-2/input.js
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
if (cond) let
|
||||
%%LHS%% = %%RHS%%
|
||||
61
packages/babel-parser/test/fixtures/placeholders/variable/let-context-2/output.json
vendored
Normal file
61
packages/babel-parser/test/fixtures/placeholders/variable/let-context-2/output.json
vendored
Normal 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": []
|
||||
}
|
||||
}
|
||||
1
packages/babel-parser/test/fixtures/placeholders/variable/let-context-3/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/placeholders/variable/let-context-3/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
if (cond) let %%LHS%% = %%RHS%%;
|
||||
64
packages/babel-parser/test/fixtures/placeholders/variable/let-context-3/output.json
vendored
Normal file
64
packages/babel-parser/test/fixtures/placeholders/variable/let-context-3/output.json
vendored
Normal 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": []
|
||||
}
|
||||
}
|
||||
1
packages/babel-parser/test/fixtures/placeholders/variable/let-init/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/placeholders/variable/let-init/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
let %%LHS%% = %%RHS%%;
|
||||
44
packages/babel-parser/test/fixtures/placeholders/variable/let-init/output.json
vendored
Normal file
44
packages/babel-parser/test/fixtures/placeholders/variable/let-init/output.json
vendored
Normal 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": []
|
||||
}
|
||||
}
|
||||
1
packages/babel-parser/test/fixtures/placeholders/variable/let-modulo/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/placeholders/variable/let-modulo/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
let %LHS;
|
||||
32
packages/babel-parser/test/fixtures/placeholders/variable/let-modulo/output.json
vendored
Normal file
32
packages/babel-parser/test/fixtures/placeholders/variable/let-modulo/output.json
vendored
Normal 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": []
|
||||
}
|
||||
}
|
||||
1
packages/babel-parser/test/fixtures/placeholders/variable/var-init/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/placeholders/variable/var-init/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
var %%LHS%% = %%RHS%%;
|
||||
44
packages/babel-parser/test/fixtures/placeholders/variable/var-init/output.json
vendored
Normal file
44
packages/babel-parser/test/fixtures/placeholders/variable/var-init/output.json
vendored
Normal 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": []
|
||||
}
|
||||
}
|
||||
@ -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;"`);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user