Skip to content

Commit

Permalink
chore
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed May 7, 2024
1 parent c12f0ec commit 7731bf1
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 62 deletions.
5 changes: 4 additions & 1 deletion Gulpfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,10 @@ gulp.task(
);

function watch() {
gulp.watch(defaultSourcesGlob, gulp.task("build-no-bundle-watch"));
gulp.watch(
defaultSourcesGlob,
gulp.series("build-no-bundle-watch", "build-cjs-bundles")
);
gulp.watch(babelStandalonePluginConfigGlob, gulp.task("generate-standalone"));
gulp.watch(buildTypingsWatchGlob, gulp.task("generate-type-helpers"));
gulp.watch(
Expand Down
24 changes: 11 additions & 13 deletions packages/babel-parser/src/parser/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,7 @@ export default abstract class ExpressionParser extends LValParser {

this.next();
node.right = this.parseMaybeAssign();
this.checkLVal(left, {
in: this.finishNode(node, "AssignmentExpression"),
});
this.checkLVal(left, this.finishNode(node, "AssignmentExpression"));
// @ts-expect-error todo(flow->ts) improve node types
return node;
} else if (ownExpressionErrors) {
Expand Down Expand Up @@ -676,9 +674,10 @@ export default abstract class ExpressionParser extends LValParser {
): N.Expression {
if (update) {
const updateExpressionNode = node as Undone<N.UpdateExpression>;
this.checkLVal(updateExpressionNode.argument, {
in: this.finishNode(updateExpressionNode, "UpdateExpression"),
});
this.checkLVal(
updateExpressionNode.argument,
this.finishNode(updateExpressionNode, "UpdateExpression"),
);
return node;
}

Expand All @@ -691,9 +690,7 @@ export default abstract class ExpressionParser extends LValParser {
node.prefix = false;
node.argument = expr;
this.next();
this.checkLVal(expr, {
in: (expr = this.finishNode(node, "UpdateExpression")),
});
this.checkLVal(expr, (expr = this.finishNode(node, "UpdateExpression")));
}
return expr;
}
Expand Down Expand Up @@ -2665,12 +2662,13 @@ export default abstract class ExpressionParser extends LValParser {
// 1. https://tc39.es/ecma262/#prod-FormalParameters
const formalParameters = { type: "FormalParameters" } as const;
for (const param of node.params) {
this.checkLVal(param, {
in: formalParameters,
binding: BindingFlag.TYPE_VAR,
this.checkLVal(
param,
formalParameters,
BindingFlag.TYPE_VAR,
checkClashes,
strictModeChanged,
});
);
}
}

Expand Down
73 changes: 41 additions & 32 deletions packages/babel-parser/src/parser/lval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ export default abstract class LValParser extends NodeUtils {
isUnparenthesizedInAssign: boolean,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
binding: BindingFlag,
): string | boolean {
): string | boolean | [string, boolean] {
switch (type) {
case "AssignmentPattern":
return "left";
Expand All @@ -567,23 +567,22 @@ export default abstract class LValParser extends NodeUtils {
* Verify that a target expression is an lval (something that can be assigned to).
*
* @param expression The expression in question to check.
* @param options A set of options described below.
* @param options.in
* @param ancestor
* The relevant ancestor to provide context information for the error
* if the check fails.
* @param options.binding
* @param binding
* The desired binding type. If the given expression is an identifier
* and `binding` is not `BindingFlag.TYPE_NONE`, `checkLVal` will register binding
* to the parser scope See also `src/util/scopeflags.js`
* @param options.checkClashes
* @param checkClashes
* An optional string set to check if an identifier name is included.
* `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.strictModeChanged
* @param strictModeChanged
* Whether an identifier has been parsed in a sloppy context but should
* be reinterpreted as strict-mode. e.g. `(arguments) => { "use strict "}`
* @param options.hasParenthesizedAncestor
* @param hasParenthesizedAncestor
* This is only used internally during recursive calls, and you should
* not have to set it yourself.
*/
Expand All @@ -595,19 +594,11 @@ export default abstract class LValParser extends NodeUtils {
| RestElement
| Pattern
| TSParameterProperty,
{
in: ancestor,
binding = BindingFlag.TYPE_NONE,
checkClashes = false,
strictModeChanged = false,
hasParenthesizedAncestor = false,
}: {
in: LValAncestor;
binding?: BindingFlag;
checkClashes?: Set<string> | false;
strictModeChanged?: boolean;
hasParenthesizedAncestor?: boolean;
},
ancestor: LValAncestor,
binding: BindingFlag = BindingFlag.TYPE_NONE,
checkClashes: Set<string> | false = false,
strictModeChanged: boolean = false,
hasParenthesizedAncestor: boolean = false,
): void {
const type = expression.type;

Expand Down Expand Up @@ -669,25 +660,43 @@ export default abstract class LValParser extends NodeUtils {
return;
}

const [key, isParenthesizedExpression] = Array.isArray(validity)
? validity
: [validity, type === "ParenthesizedExpression"];
let key: string, isParenthesizedExpression: boolean;
if (typeof validity === "string") {
key = validity;
isParenthesizedExpression = type === "ParenthesizedExpression";
} else {
[key, isParenthesizedExpression] = validity;
}

const nextAncestor =
type === "ArrayPattern" || type === "ObjectPattern"
? ({ type } as const)
: ancestor;

// @ts-expect-error key may not index expression.
for (const child of [].concat(expression[key])) {
if (child) {
this.checkLVal(child, {
in: nextAncestor,
binding,
checkClashes,
strictModeChanged,
hasParenthesizedAncestor: isParenthesizedExpression,
});
const val = expression[key];
if (Array.isArray(val)) {
for (const child of val) {
if (child) {
this.checkLVal(
child,
nextAncestor,
binding,
checkClashes,
strictModeChanged,
isParenthesizedExpression,
);
}
}
} else if (val) {
this.checkLVal(
val,
nextAncestor,
binding,
checkClashes,
strictModeChanged,
isParenthesizedExpression,
);
}
}

Expand Down
25 changes: 12 additions & 13 deletions packages/babel-parser/src/parser/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ export default abstract class StatementParser extends ExpressionParser {
this.checkDestructuringPrivate(refExpressionErrors);
this.toAssignable(init, /* isLHS */ true);
const type = isForOf ? "ForOfStatement" : "ForInStatement";
this.checkLVal(init, { in: { type } });
this.checkLVal(init, { type });
return this.parseForIn(
node as Undone<N.ForInStatement | N.ForOfStatement>,
// @ts-expect-error init has been transformed to an assignable
Expand Down Expand Up @@ -1133,10 +1133,11 @@ export default abstract class StatementParser extends ExpressionParser {
? ScopeFlag.SIMPLE_CATCH
: 0,
);
this.checkLVal(param, {
in: { type: "CatchClause" },
binding: BindingFlag.TYPE_CATCH_PARAM,
});
this.checkLVal(
param,
{ type: "CatchClause" },
BindingFlag.TYPE_CATCH_PARAM,
);

return param;
}
Expand Down Expand Up @@ -1566,10 +1567,11 @@ export default abstract class StatementParser extends ExpressionParser {
kind: "var" | "let" | "const" | "using" | "await using",
): void {
const id = this.parseBindingAtom();
this.checkLVal(id, {
in: { type: "VariableDeclarator" },
binding: kind === "var" ? BindingFlag.TYPE_VAR : BindingFlag.TYPE_LEXICAL,
});
this.checkLVal(
id,
{ type: "VariableDeclarator" },
kind === "var" ? BindingFlag.TYPE_VAR : BindingFlag.TYPE_LEXICAL,
);
decl.id = id;
}

Expand Down Expand Up @@ -3146,10 +3148,7 @@ export default abstract class StatementParser extends ExpressionParser {
type: T["type"],
bindingType: BindingFlag = BindingFlag.TYPE_LEXICAL,
) {
this.checkLVal(specifier.local, {
in: { type },
binding: bindingType,
});
this.checkLVal(specifier.local, { type }, bindingType);
return this.finishNode(specifier, type);
}

Expand Down
5 changes: 2 additions & 3 deletions packages/babel-parser/src/plugins/typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3681,7 +3681,6 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
}
}

// @ts-expect-error plugin overrides interfaces
isValidLVal(
type:
| "TSTypeCastExpression"
Expand Down Expand Up @@ -3709,8 +3708,8 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
case "TSSatisfiesExpression":
case "TSTypeAssertion":
return (
(binding !== BindingFlag.TYPE_NONE ||
!isUnparenthesizedInAssign) && ["expression", true]
(binding !== BindingFlag.TYPE_NONE || !isUnparenthesizedInAssign) &&
(["expression", true] as [string, boolean])
);
default:
return super.isValidLVal(type, isUnparenthesizedInAssign, binding);
Expand Down

0 comments on commit 7731bf1

Please sign in to comment.