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

Forbid initializerless using #16403

Merged
merged 2 commits into from Apr 3, 2024
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
2 changes: 1 addition & 1 deletion packages/babel-parser/src/parse-error/standard-errors.ts
Expand Up @@ -52,7 +52,7 @@ export default {
DeclarationMissingInitializer: ({
kind,
}: {
kind: "const" | "destructuring";
kind: "await using" | "const" | "destructuring" | "using";
}) => `Missing initializer in ${kind} declaration.`,
DecoratorArgumentsOutsideParentheses:
"Decorator arguments must be moved inside parentheses: use '@(decorator(args))' instead of '@(decorator)(args)'.",
Expand Down
6 changes: 2 additions & 4 deletions packages/babel-parser/src/parser/statement.ts
Expand Up @@ -1541,15 +1541,13 @@ export default abstract class StatementParser extends ExpressionParser {
},
);
} else if (
kind === "const" &&
(kind === "const" || kind === "using" || kind === "await using") &&
!(this.match(tt._in) || this.isContextual(tt._of))
) {
this.raise(
Errors.DeclarationMissingInitializer,
this.state.lastTokEndLoc,
{
kind: "const",
},
{ kind },
);
}
}
Expand Down
@@ -1,6 +1,6 @@
async function f() {
{
await using f, f = foo();
await using f = foo(), f = foo();
}
{
await using g = foo();
Expand Down
@@ -1,19 +1,19 @@
{
"type": "File",
"start":0,"end":122,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":9,"column":1,"index":122}},
"start":0,"end":130,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":9,"column":1,"index":130}},
"errors": [
"SyntaxError: Identifier 'f' has already been declared. (3:19)",
"SyntaxError: Identifier 'f' has already been declared. (3:27)",
"SyntaxError: Identifier 'g' has already been declared. (7:16)"
],
"program": {
"type": "Program",
"start":0,"end":122,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":9,"column":1,"index":122}},
"start":0,"end":130,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":9,"column":1,"index":130}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "FunctionDeclaration",
"start":0,"end":122,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":9,"column":1,"index":122}},
"start":0,"end":130,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":9,"column":1,"index":130}},
"id": {
"type": "Identifier",
"start":15,"end":16,"loc":{"start":{"line":1,"column":15,"index":15},"end":{"line":1,"column":16,"index":16},"identifierName":"f"},
Expand All @@ -24,40 +24,49 @@
"params": [],
"body": {
"type": "BlockStatement",
"start":19,"end":122,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":9,"column":1,"index":122}},
"start":19,"end":130,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":9,"column":1,"index":130}},
"body": [
{
"type": "BlockStatement",
"start":23,"end":58,"loc":{"start":{"line":2,"column":2,"index":23},"end":{"line":4,"column":3,"index":58}},
"start":23,"end":66,"loc":{"start":{"line":2,"column":2,"index":23},"end":{"line":4,"column":3,"index":66}},
"body": [
{
"type": "VariableDeclaration",
"start":29,"end":54,"loc":{"start":{"line":3,"column":4,"index":29},"end":{"line":3,"column":29,"index":54}},
"start":29,"end":62,"loc":{"start":{"line":3,"column":4,"index":29},"end":{"line":3,"column":37,"index":62}},
"declarations": [
{
"type": "VariableDeclarator",
"start":41,"end":42,"loc":{"start":{"line":3,"column":16,"index":41},"end":{"line":3,"column":17,"index":42}},
"start":41,"end":50,"loc":{"start":{"line":3,"column":16,"index":41},"end":{"line":3,"column":25,"index":50}},
"id": {
"type": "Identifier",
"start":41,"end":42,"loc":{"start":{"line":3,"column":16,"index":41},"end":{"line":3,"column":17,"index":42},"identifierName":"f"},
"name": "f"
},
"init": null
"init": {
"type": "CallExpression",
"start":45,"end":50,"loc":{"start":{"line":3,"column":20,"index":45},"end":{"line":3,"column":25,"index":50}},
"callee": {
"type": "Identifier",
"start":45,"end":48,"loc":{"start":{"line":3,"column":20,"index":45},"end":{"line":3,"column":23,"index":48},"identifierName":"foo"},
"name": "foo"
},
"arguments": []
}
},
{
"type": "VariableDeclarator",
"start":44,"end":53,"loc":{"start":{"line":3,"column":19,"index":44},"end":{"line":3,"column":28,"index":53}},
"start":52,"end":61,"loc":{"start":{"line":3,"column":27,"index":52},"end":{"line":3,"column":36,"index":61}},
"id": {
"type": "Identifier",
"start":44,"end":45,"loc":{"start":{"line":3,"column":19,"index":44},"end":{"line":3,"column":20,"index":45},"identifierName":"f"},
"start":52,"end":53,"loc":{"start":{"line":3,"column":27,"index":52},"end":{"line":3,"column":28,"index":53},"identifierName":"f"},
"name": "f"
},
"init": {
"type": "CallExpression",
"start":48,"end":53,"loc":{"start":{"line":3,"column":23,"index":48},"end":{"line":3,"column":28,"index":53}},
"start":56,"end":61,"loc":{"start":{"line":3,"column":31,"index":56},"end":{"line":3,"column":36,"index":61}},
"callee": {
"type": "Identifier",
"start":48,"end":51,"loc":{"start":{"line":3,"column":23,"index":48},"end":{"line":3,"column":26,"index":51},"identifierName":"foo"},
"start":56,"end":59,"loc":{"start":{"line":3,"column":31,"index":56},"end":{"line":3,"column":34,"index":59},"identifierName":"foo"},
"name": "foo"
},
"arguments": []
Expand All @@ -71,26 +80,26 @@
},
{
"type": "BlockStatement",
"start":61,"end":120,"loc":{"start":{"line":5,"column":2,"index":61},"end":{"line":8,"column":3,"index":120}},
"start":69,"end":128,"loc":{"start":{"line":5,"column":2,"index":69},"end":{"line":8,"column":3,"index":128}},
"body": [
{
"type": "VariableDeclaration",
"start":67,"end":89,"loc":{"start":{"line":6,"column":4,"index":67},"end":{"line":6,"column":26,"index":89}},
"start":75,"end":97,"loc":{"start":{"line":6,"column":4,"index":75},"end":{"line":6,"column":26,"index":97}},
"declarations": [
{
"type": "VariableDeclarator",
"start":79,"end":88,"loc":{"start":{"line":6,"column":16,"index":79},"end":{"line":6,"column":25,"index":88}},
"start":87,"end":96,"loc":{"start":{"line":6,"column":16,"index":87},"end":{"line":6,"column":25,"index":96}},
"id": {
"type": "Identifier",
"start":79,"end":80,"loc":{"start":{"line":6,"column":16,"index":79},"end":{"line":6,"column":17,"index":80},"identifierName":"g"},
"start":87,"end":88,"loc":{"start":{"line":6,"column":16,"index":87},"end":{"line":6,"column":17,"index":88},"identifierName":"g"},
"name": "g"
},
"init": {
"type": "CallExpression",
"start":83,"end":88,"loc":{"start":{"line":6,"column":20,"index":83},"end":{"line":6,"column":25,"index":88}},
"start":91,"end":96,"loc":{"start":{"line":6,"column":20,"index":91},"end":{"line":6,"column":25,"index":96}},
"callee": {
"type": "Identifier",
"start":83,"end":86,"loc":{"start":{"line":6,"column":20,"index":83},"end":{"line":6,"column":23,"index":86},"identifierName":"foo"},
"start":91,"end":94,"loc":{"start":{"line":6,"column":20,"index":91},"end":{"line":6,"column":23,"index":94},"identifierName":"foo"},
"name": "foo"
},
"arguments": []
Expand All @@ -101,22 +110,22 @@
},
{
"type": "VariableDeclaration",
"start":94,"end":116,"loc":{"start":{"line":7,"column":4,"index":94},"end":{"line":7,"column":26,"index":116}},
"start":102,"end":124,"loc":{"start":{"line":7,"column":4,"index":102},"end":{"line":7,"column":26,"index":124}},
"declarations": [
{
"type": "VariableDeclarator",
"start":106,"end":115,"loc":{"start":{"line":7,"column":16,"index":106},"end":{"line":7,"column":25,"index":115}},
"start":114,"end":123,"loc":{"start":{"line":7,"column":16,"index":114},"end":{"line":7,"column":25,"index":123}},
"id": {
"type": "Identifier",
"start":106,"end":107,"loc":{"start":{"line":7,"column":16,"index":106},"end":{"line":7,"column":17,"index":107},"identifierName":"g"},
"start":114,"end":115,"loc":{"start":{"line":7,"column":16,"index":114},"end":{"line":7,"column":17,"index":115},"identifierName":"g"},
"name": "g"
},
"init": {
"type": "CallExpression",
"start":110,"end":115,"loc":{"start":{"line":7,"column":20,"index":110},"end":{"line":7,"column":25,"index":115}},
"start":118,"end":123,"loc":{"start":{"line":7,"column":20,"index":118},"end":{"line":7,"column":25,"index":123}},
"callee": {
"type": "Identifier",
"start":110,"end":113,"loc":{"start":{"line":7,"column":20,"index":110},"end":{"line":7,"column":23,"index":113},"identifierName":"foo"},
"start":118,"end":121,"loc":{"start":{"line":7,"column":20,"index":118},"end":{"line":7,"column":23,"index":121},"identifierName":"foo"},
"name": "foo"
},
"arguments": []
Expand Down
@@ -1,8 +1,8 @@
async function f() {
while (1) await using a;
for (;;) await using b;
do await using c; while (1);
if (1) await using d;
with (1) await using e;
label: await using f;
while (1) await using a = foo;
for (;;) await using b = foo;
do await using c = foo; while (1);
if (1) await using d = foo;
with (1) await using e = foo;
label: await using f = foo;
}