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

Disallow await as bound name in using declaration #15391

Merged
merged 5 commits into from Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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: 2 additions & 0 deletions packages/babel-parser/src/parse-error/standard-errors.ts
Expand Up @@ -33,6 +33,8 @@ export default {
"Can not use 'await' as identifier inside a static block.",
AwaitExpressionFormalParameter:
"'await' is not allowed in async function parameters.",
AwaitInUsingBinding:
"'await' is not allowed to be used as a name in 'using' declarations.",
AwaitNotInAsyncContext:
"'await' is only allowed within async functions and at the top levels of modules.",
AwaitNotInAsyncFunction: "'await' is only allowed within async functions.",
Expand Down
7 changes: 6 additions & 1 deletion packages/babel-parser/src/parser/statement.ts
Expand Up @@ -471,7 +471,7 @@ export default abstract class StatementParser extends ExpressionParser {
return this.parseTryStatement(node as Undone<N.TryStatement>);

case tt._using:
// using [no LineTerminator here] BindingList[+Using]
// using [no LineTerminator here][lookahead != `await`] BindingList[+Using]
if (
this.hasFollowingLineBreak() ||
this.state.containsEsc ||
Expand Down Expand Up @@ -1507,6 +1507,11 @@ export default abstract class StatementParser extends ExpressionParser {
decl: Undone<N.VariableDeclarator>,
kind: "var" | "let" | "const" | "using",
): void {
// Unlike "let" which must be handled in checkLVal, it suffices to check
// await here because `using` must not precede binding patterns.
if (kind === "using" && !this.inModule && this.match(tt._await)) {
this.raise(Errors.AwaitInUsingBinding, { at: this.state.startLoc });
}
const id = this.parseBindingAtom();
this.checkLVal(id, {
in: { type: "VariableDeclarator" },
Expand Down
@@ -0,0 +1,12 @@
{
using await = h();
}
{
using \u0061wait = h();
}
{
using x, await = h();
}
{
for (using await of []);
}
@@ -0,0 +1,3 @@
{
"sourceType": "module"
}
@@ -0,0 +1,166 @@
{
"type": "File",
"start":0,"end":113,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":12,"column":1,"index":113}},
"errors": [
"SyntaxError: Can not use 'await' as identifier inside an async function. (2:8)",
Copy link
Member

Choose a reason for hiding this comment

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

Unrelated, but this error message is wrong (we are not in an async function, but at the top-level of a module).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't differentiate between module top level and async function as long as [Await] production parameter set. Maybe rephrase it as "Can not use 'await' as identifier inside an async context"?

"SyntaxError: Can not use 'await' as identifier inside an async function. (5:8)",
"SyntaxError: Can not use 'await' as identifier inside an async function. (8:11)",
"SyntaxError: Can not use 'await' as identifier inside an async function. (11:13)"
],
"program": {
"type": "Program",
"start":0,"end":113,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":12,"column":1,"index":113}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "BlockStatement",
"start":0,"end":24,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":24}},
"body": [
{
"type": "VariableDeclaration",
"start":4,"end":22,"loc":{"start":{"line":2,"column":2,"index":4},"end":{"line":2,"column":20,"index":22}},
"declarations": [
{
"type": "VariableDeclarator",
"start":10,"end":21,"loc":{"start":{"line":2,"column":8,"index":10},"end":{"line":2,"column":19,"index":21}},
"id": {
"type": "Identifier",
"start":10,"end":15,"loc":{"start":{"line":2,"column":8,"index":10},"end":{"line":2,"column":13,"index":15},"identifierName":"await"},
"name": "await"
},
"init": {
"type": "CallExpression",
"start":18,"end":21,"loc":{"start":{"line":2,"column":16,"index":18},"end":{"line":2,"column":19,"index":21}},
"callee": {
"type": "Identifier",
"start":18,"end":19,"loc":{"start":{"line":2,"column":16,"index":18},"end":{"line":2,"column":17,"index":19},"identifierName":"h"},
"name": "h"
},
"arguments": []
}
}
],
"kind": "using"
}
],
"directives": []
},
{
"type": "BlockStatement",
"start":25,"end":54,"loc":{"start":{"line":4,"column":0,"index":25},"end":{"line":6,"column":1,"index":54}},
"body": [
{
"type": "VariableDeclaration",
"start":29,"end":52,"loc":{"start":{"line":5,"column":2,"index":29},"end":{"line":5,"column":25,"index":52}},
"declarations": [
{
"type": "VariableDeclarator",
"start":35,"end":51,"loc":{"start":{"line":5,"column":8,"index":35},"end":{"line":5,"column":24,"index":51}},
"id": {
"type": "Identifier",
"start":35,"end":45,"loc":{"start":{"line":5,"column":8,"index":35},"end":{"line":5,"column":18,"index":45},"identifierName":"await"},
"name": "await"
},
"init": {
"type": "CallExpression",
"start":48,"end":51,"loc":{"start":{"line":5,"column":21,"index":48},"end":{"line":5,"column":24,"index":51}},
"callee": {
"type": "Identifier",
"start":48,"end":49,"loc":{"start":{"line":5,"column":21,"index":48},"end":{"line":5,"column":22,"index":49},"identifierName":"h"},
"name": "h"
},
"arguments": []
}
}
],
"kind": "using"
}
],
"directives": []
},
{
"type": "BlockStatement",
"start":55,"end":82,"loc":{"start":{"line":7,"column":0,"index":55},"end":{"line":9,"column":1,"index":82}},
"body": [
{
"type": "VariableDeclaration",
"start":59,"end":80,"loc":{"start":{"line":8,"column":2,"index":59},"end":{"line":8,"column":23,"index":80}},
"declarations": [
{
"type": "VariableDeclarator",
"start":65,"end":66,"loc":{"start":{"line":8,"column":8,"index":65},"end":{"line":8,"column":9,"index":66}},
"id": {
"type": "Identifier",
"start":65,"end":66,"loc":{"start":{"line":8,"column":8,"index":65},"end":{"line":8,"column":9,"index":66},"identifierName":"x"},
"name": "x"
},
"init": null
},
{
"type": "VariableDeclarator",
"start":68,"end":79,"loc":{"start":{"line":8,"column":11,"index":68},"end":{"line":8,"column":22,"index":79}},
"id": {
"type": "Identifier",
"start":68,"end":73,"loc":{"start":{"line":8,"column":11,"index":68},"end":{"line":8,"column":16,"index":73},"identifierName":"await"},
"name": "await"
},
"init": {
"type": "CallExpression",
"start":76,"end":79,"loc":{"start":{"line":8,"column":19,"index":76},"end":{"line":8,"column":22,"index":79}},
"callee": {
"type": "Identifier",
"start":76,"end":77,"loc":{"start":{"line":8,"column":19,"index":76},"end":{"line":8,"column":20,"index":77},"identifierName":"h"},
"name": "h"
},
"arguments": []
}
}
],
"kind": "using"
}
],
"directives": []
},
{
"type": "BlockStatement",
"start":83,"end":113,"loc":{"start":{"line":10,"column":0,"index":83},"end":{"line":12,"column":1,"index":113}},
"body": [
{
"type": "ForOfStatement",
"start":87,"end":111,"loc":{"start":{"line":11,"column":2,"index":87},"end":{"line":11,"column":26,"index":111}},
"await": false,
"left": {
"type": "VariableDeclaration",
"start":92,"end":103,"loc":{"start":{"line":11,"column":7,"index":92},"end":{"line":11,"column":18,"index":103}},
"declarations": [
{
"type": "VariableDeclarator",
"start":98,"end":103,"loc":{"start":{"line":11,"column":13,"index":98},"end":{"line":11,"column":18,"index":103}},
"id": {
"type": "Identifier",
"start":98,"end":103,"loc":{"start":{"line":11,"column":13,"index":98},"end":{"line":11,"column":18,"index":103},"identifierName":"await"},
"name": "await"
},
"init": null
}
],
"kind": "using"
},
"right": {
"type": "ArrayExpression",
"start":107,"end":109,"loc":{"start":{"line":11,"column":22,"index":107},"end":{"line":11,"column":24,"index":109}},
"elements": []
},
"body": {
"type": "EmptyStatement",
"start":110,"end":111,"loc":{"start":{"line":11,"column":25,"index":110},"end":{"line":11,"column":26,"index":111}}
}
}
],
"directives": []
}
],
"directives": []
}
}
@@ -0,0 +1,12 @@
{
using await = h();
}
{
using \u0061wait = h();
}
{
using x, await = h();
}
{
for (using await of []);
}
@@ -0,0 +1,3 @@
{
"sourceType": "script"
}
@@ -0,0 +1,166 @@
{
"type": "File",
"start":0,"end":113,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":12,"column":1,"index":113}},
"errors": [
"SyntaxError: 'await' is not allowed to be used as a name in 'using' declarations. (2:8)",
"SyntaxError: 'await' is not allowed to be used as a name in 'using' declarations. (5:8)",
"SyntaxError: 'await' is not allowed to be used as a name in 'using' declarations. (8:11)",
"SyntaxError: 'await' is not allowed to be used as a name in 'using' declarations. (11:13)"
],
"program": {
"type": "Program",
"start":0,"end":113,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":12,"column":1,"index":113}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "BlockStatement",
"start":0,"end":24,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":24}},
"body": [
{
"type": "VariableDeclaration",
"start":4,"end":22,"loc":{"start":{"line":2,"column":2,"index":4},"end":{"line":2,"column":20,"index":22}},
"declarations": [
{
"type": "VariableDeclarator",
"start":10,"end":21,"loc":{"start":{"line":2,"column":8,"index":10},"end":{"line":2,"column":19,"index":21}},
"id": {
"type": "Identifier",
"start":10,"end":15,"loc":{"start":{"line":2,"column":8,"index":10},"end":{"line":2,"column":13,"index":15},"identifierName":"await"},
"name": "await"
},
"init": {
"type": "CallExpression",
"start":18,"end":21,"loc":{"start":{"line":2,"column":16,"index":18},"end":{"line":2,"column":19,"index":21}},
"callee": {
"type": "Identifier",
"start":18,"end":19,"loc":{"start":{"line":2,"column":16,"index":18},"end":{"line":2,"column":17,"index":19},"identifierName":"h"},
"name": "h"
},
"arguments": []
}
}
],
"kind": "using"
}
],
"directives": []
},
{
"type": "BlockStatement",
"start":25,"end":54,"loc":{"start":{"line":4,"column":0,"index":25},"end":{"line":6,"column":1,"index":54}},
"body": [
{
"type": "VariableDeclaration",
"start":29,"end":52,"loc":{"start":{"line":5,"column":2,"index":29},"end":{"line":5,"column":25,"index":52}},
"declarations": [
{
"type": "VariableDeclarator",
"start":35,"end":51,"loc":{"start":{"line":5,"column":8,"index":35},"end":{"line":5,"column":24,"index":51}},
"id": {
"type": "Identifier",
"start":35,"end":45,"loc":{"start":{"line":5,"column":8,"index":35},"end":{"line":5,"column":18,"index":45},"identifierName":"await"},
"name": "await"
},
"init": {
"type": "CallExpression",
"start":48,"end":51,"loc":{"start":{"line":5,"column":21,"index":48},"end":{"line":5,"column":24,"index":51}},
"callee": {
"type": "Identifier",
"start":48,"end":49,"loc":{"start":{"line":5,"column":21,"index":48},"end":{"line":5,"column":22,"index":49},"identifierName":"h"},
"name": "h"
},
"arguments": []
}
}
],
"kind": "using"
}
],
"directives": []
},
{
"type": "BlockStatement",
"start":55,"end":82,"loc":{"start":{"line":7,"column":0,"index":55},"end":{"line":9,"column":1,"index":82}},
"body": [
{
"type": "VariableDeclaration",
"start":59,"end":80,"loc":{"start":{"line":8,"column":2,"index":59},"end":{"line":8,"column":23,"index":80}},
"declarations": [
{
"type": "VariableDeclarator",
"start":65,"end":66,"loc":{"start":{"line":8,"column":8,"index":65},"end":{"line":8,"column":9,"index":66}},
"id": {
"type": "Identifier",
"start":65,"end":66,"loc":{"start":{"line":8,"column":8,"index":65},"end":{"line":8,"column":9,"index":66},"identifierName":"x"},
"name": "x"
},
"init": null
},
{
"type": "VariableDeclarator",
"start":68,"end":79,"loc":{"start":{"line":8,"column":11,"index":68},"end":{"line":8,"column":22,"index":79}},
"id": {
"type": "Identifier",
"start":68,"end":73,"loc":{"start":{"line":8,"column":11,"index":68},"end":{"line":8,"column":16,"index":73},"identifierName":"await"},
"name": "await"
},
"init": {
"type": "CallExpression",
"start":76,"end":79,"loc":{"start":{"line":8,"column":19,"index":76},"end":{"line":8,"column":22,"index":79}},
"callee": {
"type": "Identifier",
"start":76,"end":77,"loc":{"start":{"line":8,"column":19,"index":76},"end":{"line":8,"column":20,"index":77},"identifierName":"h"},
"name": "h"
},
"arguments": []
}
}
],
"kind": "using"
}
],
"directives": []
},
{
"type": "BlockStatement",
"start":83,"end":113,"loc":{"start":{"line":10,"column":0,"index":83},"end":{"line":12,"column":1,"index":113}},
"body": [
{
"type": "ForOfStatement",
"start":87,"end":111,"loc":{"start":{"line":11,"column":2,"index":87},"end":{"line":11,"column":26,"index":111}},
"await": false,
"left": {
"type": "VariableDeclaration",
"start":92,"end":103,"loc":{"start":{"line":11,"column":7,"index":92},"end":{"line":11,"column":18,"index":103}},
"declarations": [
{
"type": "VariableDeclarator",
"start":98,"end":103,"loc":{"start":{"line":11,"column":13,"index":98},"end":{"line":11,"column":18,"index":103}},
"id": {
"type": "Identifier",
"start":98,"end":103,"loc":{"start":{"line":11,"column":13,"index":98},"end":{"line":11,"column":18,"index":103},"identifierName":"await"},
"name": "await"
},
"init": null
}
],
"kind": "using"
},
"right": {
"type": "ArrayExpression",
"start":107,"end":109,"loc":{"start":{"line":11,"column":22,"index":107},"end":{"line":11,"column":24,"index":109}},
"elements": []
},
"body": {
"type": "EmptyStatement",
"start":110,"end":111,"loc":{"start":{"line":11,"column":25,"index":110},"end":{"line":11,"column":26,"index":111}}
}
}
],
"directives": []
}
],
"directives": []
}
}