validate parentheses in the left-hand side of assignment expressions (#10576)

* invalid left-hand assignments based on parenthesis

* validate against nested ParenthesizedExpressions
This commit is contained in:
Bowei Han
2019-12-09 20:29:02 -05:00
committed by Huáng Jùnliàng
parent de1fa902f0
commit 20e43ad103
48 changed files with 2811 additions and 27 deletions

View File

@@ -41,12 +41,6 @@ import {
SCOPE_PROGRAM,
} from "../util/scopeflags";
const unwrapParenthesizedExpression = node => {
return node.type === "ParenthesizedExpression"
? unwrapParenthesizedExpression(node.expression)
: node;
};
export default class ExpressionParser extends LValParser {
// Forward-declaration: defined in statement.js
+parseBlock: (
@@ -213,26 +207,6 @@ export default class ExpressionParser extends LValParser {
this.checkLVal(left, undefined, undefined, "assignment expression");
const maybePattern = unwrapParenthesizedExpression(left);
let patternErrorMsg;
if (maybePattern.type === "ObjectPattern") {
patternErrorMsg = "`({a}) = 0` use `({a} = 0)`";
} else if (maybePattern.type === "ArrayPattern") {
patternErrorMsg = "`([a]) = 0` use `([a] = 0)`";
}
if (
patternErrorMsg &&
((left.extra && left.extra.parenthesized) ||
left.type === "ParenthesizedExpression")
) {
this.raise(
maybePattern.start,
`You're trying to assign to a parenthesized expression, eg. instead of ${patternErrorMsg}`,
);
}
this.next();
node.right = this.parseMaybeAssign(noIn);
return this.finishNode(node, "AssignmentExpression");

View File

@@ -22,6 +22,12 @@ import {
import { NodeUtils } from "./node";
import { type BindingTypes, BIND_NONE } from "../util/scopeflags";
const unwrapParenthesizedExpression = (node: Node) => {
return node.type === "ParenthesizedExpression"
? unwrapParenthesizedExpression(node.expression)
: node;
};
export default class LValParser extends NodeUtils {
// Forward-declaration: defined in expression.js
+parseIdentifier: (liberal?: boolean) => Identifier;
@@ -49,6 +55,20 @@ export default class LValParser extends NodeUtils {
contextDescription: string,
): Node {
if (node) {
if (
(this.options.createParenthesizedExpressions &&
node.type === "ParenthesizedExpression") ||
node.extra?.parenthesized
) {
const parenthesized = unwrapParenthesizedExpression(node);
if (
parenthesized.type !== "Identifier" &&
parenthesized.type !== "MemberExpression"
) {
this.raise(node.start, "Invalid parenthesized assignment pattern");
}
}
switch (node.type) {
case "Identifier":
case "ObjectPattern":