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 4 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
3 changes: 3 additions & 0 deletions packages/babel-parser/src/parse-error/standard-errors.ts
Expand Up @@ -14,6 +14,7 @@ export type LValAncestor =
| "ImportSpecifier"
| "ImportNamespaceSpecifier"
| "ImportDefaultSpecifier"
| "ParenthesizedExpression"
| "ObjectPattern"
| "RestElement"
| "VariableDeclarator";
Expand All @@ -32,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
34 changes: 9 additions & 25 deletions packages/babel-parser/src/parser/lval.ts
Expand Up @@ -29,7 +29,7 @@ import { NodeUtils, type Undone } from "./node";
import {
type BindingTypes,
BIND_NONE,
BIND_SCOPE_LEXICAL,
BIND_FLAGS_NO_LET_IN_LEXICAL,
} from "../util/scopeflags";
import type { ExpressionErrors } from "./util";
import { Errors, type LValAncestor } from "../parse-error";
Expand Down Expand Up @@ -560,10 +560,6 @@ export default abstract class LValParser extends NodeUtils {
* `checkLVal` will add checked identifier name to `checkClashes` It is
* used in tracking duplicates in function parameter lists. If it is
* false, `checkLVal` will skip duplicate checks
* @param options.allowingSloppyLetBinding
* Whether an identifier named "let" should be allowed in sloppy mode.
* Defaults to `true` unless lexical scope its being used. This property
* is only relevant if the parser's state is in sloppy mode.
* @param options.strictModeChanged
* Whether an identifier has been parsed in a sloppy context but should
* be reinterpreted as strict-mode. e.g. `(arguments) => { "use strict "}`
Expand All @@ -579,14 +575,12 @@ export default abstract class LValParser extends NodeUtils {
binding = BIND_NONE,
checkClashes = false,
strictModeChanged = false,
allowingSloppyLetBinding = !(binding & BIND_SCOPE_LEXICAL),
hasParenthesizedAncestor = false,
}: {
in: LValAncestor;
binding?: BindingTypes;
checkClashes?: Set<string> | false;
strictModeChanged?: boolean;
allowingSloppyLetBinding?: boolean;
hasParenthesizedAncestor?: boolean;
},
): void {
Expand All @@ -604,12 +598,11 @@ export default abstract class LValParser extends NodeUtils {
return;
}

if (expression.type === "Identifier") {
if (type === "Identifier") {
this.checkIdentifier(
expression as Identifier,
binding,
strictModeChanged,
allowingSloppyLetBinding,
);

const { name } = expression as Identifier;
Expand All @@ -626,7 +619,7 @@ export default abstract class LValParser extends NodeUtils {
}

const validity = this.isValidLVal(
expression.type,
type,
!(hasParenthesizedAncestor || expression.extra?.parenthesized) &&
ancestor.type === "AssignmentExpression",
binding,
Expand All @@ -637,35 +630,27 @@ export default abstract class LValParser extends NodeUtils {
const ParseErrorClass =
binding === BIND_NONE ? Errors.InvalidLhs : Errors.InvalidLhsBinding;

this.raise(ParseErrorClass, {
at: expression,
ancestor:
ancestor.type === "UpdateExpression"
? { type: "UpdateExpression", prefix: ancestor.prefix }
: { type: ancestor.type },
});
this.raise(ParseErrorClass, { at: expression, ancestor });
return;
}

const [key, isParenthesizedExpression] = Array.isArray(validity)
? validity
: [validity, type === "ParenthesizedExpression"];
const nextAncestor =
expression.type === "ArrayPattern" ||
expression.type === "ObjectPattern" ||
expression.type === "ParenthesizedExpression"
? expression
type === "ArrayPattern" ||
type === "ObjectPattern" ||
type === "ParenthesizedExpression"
? ({ type } as const)
: ancestor;

// @ts-expect-error key may not index expression.
for (const child of [].concat(expression[key])) {
if (child) {
this.checkLVal(child, {
// @ts-expect-error: refine types
in: nextAncestor,
binding,
checkClashes,
allowingSloppyLetBinding,
strictModeChanged,
hasParenthesizedAncestor: isParenthesizedExpression,
});
Expand All @@ -677,7 +662,6 @@ export default abstract class LValParser extends NodeUtils {
at: Identifier,
bindingType: BindingTypes,
strictModeChanged: boolean = false,
allowLetBinding: boolean = !(bindingType & BIND_SCOPE_LEXICAL),
) {
if (
this.state.strict &&
Expand All @@ -695,7 +679,7 @@ export default abstract class LValParser extends NodeUtils {
}
}

if (!allowLetBinding && at.name === "let") {
if (bindingType & BIND_FLAGS_NO_LET_IN_LEXICAL && at.name === "let") {
this.raise(Errors.LetInLexicalBinding, { at });
}

Expand Down
14 changes: 9 additions & 5 deletions packages/babel-parser/src/parser/statement.ts
Expand Up @@ -29,6 +29,7 @@ import {
CLASS_ELEMENT_STATIC_GETTER,
CLASS_ELEMENT_STATIC_SETTER,
type BindingTypes,
BIND_CATCH_PARAM,
} from "../util/scopeflags";
import { ExpressionErrors } from "./util";
import { PARAM, functionFlags } from "../util/production-parameter";
Expand Down Expand Up @@ -470,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 @@ -1076,8 +1077,7 @@ export default abstract class StatementParser extends ExpressionParser {
this.scope.enter(simple ? SCOPE_SIMPLE_CATCH : 0);
this.checkLVal(param, {
in: { type: "CatchClause" },
binding: BIND_LEXICAL,
allowingSloppyLetBinding: true,
binding: BIND_CATCH_PARAM,
});

return param;
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 Expand Up @@ -2958,8 +2963,7 @@ export default abstract class StatementParser extends ExpressionParser {
| N.ImportNamespaceSpecifier,
>(specifier: Undone<T>, type: T["type"], bindingType = BIND_LEXICAL) {
this.checkLVal(specifier.local, {
// @ts-expect-error refine types
in: specifier,
in: { type },
binding: bindingType,
});
return this.finishNode(specifier, type);
Expand Down
43 changes: 23 additions & 20 deletions packages/babel-parser/src/util/scopeflags.ts
Expand Up @@ -26,42 +26,45 @@ export type ScopeFlags =

// These flags are meant to be _only_ used inside the Scope class (or subclasses).
// prettier-ignore
export const BIND_KIND_VALUE = 0b000000_0000_01,
BIND_KIND_TYPE = 0b000000_0000_10,
export const BIND_KIND_VALUE = 0b0000000_0000_01,
BIND_KIND_TYPE = 0b0000000_0000_10,
// Used in checkLVal and declareName to determine the type of a binding
BIND_SCOPE_VAR = 0b000000_0001_00, // Var-style binding
BIND_SCOPE_LEXICAL = 0b000000_0010_00, // Let- or const-style binding
BIND_SCOPE_FUNCTION = 0b000000_0100_00, // Function declaration
BIND_SCOPE_OUTSIDE = 0b000000_1000_00, // Special case for function names as
BIND_SCOPE_VAR = 0b0000000_0001_00, // Var-style binding
BIND_SCOPE_LEXICAL = 0b0000000_0010_00, // Let- or const-style binding
BIND_SCOPE_FUNCTION = 0b0000000_0100_00, // Function declaration
BIND_SCOPE_OUTSIDE = 0b0000000_1000_00, // Special case for function names as
// bound inside the function
// Misc flags
BIND_FLAGS_NONE = 0b0000001_0000_00,
BIND_FLAGS_CLASS = 0b0000010_0000_00,
BIND_FLAGS_TS_ENUM = 0b0000100_0000_00,
BIND_FLAGS_TS_CONST_ENUM = 0b0001000_0000_00,
BIND_FLAGS_TS_EXPORT_ONLY = 0b0010000_0000_00,
BIND_FLAGS_FLOW_DECLARE_FN = 0b0100000_0000_00,
BIND_FLAGS_TS_IMPORT = 0b1000000_0000_00;
BIND_FLAGS_NONE = 0b00000001_0000_00,
BIND_FLAGS_CLASS = 0b00000010_0000_00,
BIND_FLAGS_TS_ENUM = 0b00000100_0000_00,
BIND_FLAGS_TS_CONST_ENUM = 0b00001000_0000_00,
BIND_FLAGS_TS_EXPORT_ONLY = 0b00010000_0000_00,
BIND_FLAGS_FLOW_DECLARE_FN = 0b00100000_0000_00,
BIND_FLAGS_TS_IMPORT = 0b01000000_0000_00,
// Whether "let" should be allowed in bound names in sloppy mode
BIND_FLAGS_NO_LET_IN_LEXICAL = 0b10000000_0000_00;

// These flags are meant to be _only_ used by Scope consumers
// prettier-ignore
/* = is value? | is type? | scope | misc flags */
export const BIND_CLASS = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_CLASS ,
BIND_LEXICAL = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | 0 ,
export const BIND_CLASS = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_CLASS|BIND_FLAGS_NO_LET_IN_LEXICAL,
BIND_LEXICAL = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | BIND_FLAGS_NO_LET_IN_LEXICAL,
BIND_CATCH_PARAM = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | 0 ,
BIND_VAR = BIND_KIND_VALUE | 0 | BIND_SCOPE_VAR | 0 ,
BIND_FUNCTION = BIND_KIND_VALUE | 0 | BIND_SCOPE_FUNCTION | 0 ,
BIND_TS_INTERFACE = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_CLASS ,
BIND_TS_TYPE = 0 | BIND_KIND_TYPE | 0 | 0 ,
BIND_TS_ENUM = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_TS_ENUM,
BIND_TS_ENUM = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_TS_ENUM|BIND_FLAGS_NO_LET_IN_LEXICAL,
BIND_TS_AMBIENT = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
// These bindings don't introduce anything in the scope. They are used for assignments and
// function expressions IDs.
BIND_NONE = 0 | 0 | 0 | BIND_FLAGS_NONE ,
BIND_OUTSIDE = BIND_KIND_VALUE | 0 | 0 | BIND_FLAGS_NONE ,
BIND_NONE = 0 | 0 | 0 | BIND_FLAGS_NONE ,
BIND_OUTSIDE = BIND_KIND_VALUE | 0 | 0 | BIND_FLAGS_NONE ,

BIND_TS_CONST_ENUM = BIND_TS_ENUM | BIND_FLAGS_TS_CONST_ENUM ,
BIND_TS_CONST_ENUM = BIND_TS_ENUM | BIND_FLAGS_TS_CONST_ENUM ,
BIND_TS_NAMESPACE = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
BIND_TS_TYPE_IMPORT= 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_TS_IMPORT,
BIND_TS_TYPE_IMPORT= 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_TS_IMPORT ,

BIND_FLOW_DECLARE_FN = BIND_FLAGS_FLOW_DECLARE_FN;

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": "script"
Copy link
Member

Choose a reason for hiding this comment

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

Can you also add a test in modules? It throws that await is reserved in modules, right?

}