Revert "Parenthesized expressions (#8025)"
This reverts commit dd8b700a2c.
This commit is contained in:
committed by
Nicolò Ribaudo
parent
9f3457797f
commit
fc1ea7f496
@@ -19,7 +19,6 @@ export type Options = {
|
||||
strictMode: ?boolean,
|
||||
ranges: boolean,
|
||||
tokens: boolean,
|
||||
createParenthesizedExpressions: boolean,
|
||||
};
|
||||
|
||||
export const defaultOptions: Options = {
|
||||
@@ -56,9 +55,6 @@ export const defaultOptions: Options = {
|
||||
ranges: false,
|
||||
// Adds all parsed tokens to a `tokens` property on the `File` node
|
||||
tokens: false,
|
||||
// Whether to create ParenthesizedExpression AST nodes (if false
|
||||
// the parser sets extra.parenthesized on the expression nodes instead).
|
||||
createParenthesizedExpressions: false,
|
||||
};
|
||||
|
||||
// Interpret and default an options object
|
||||
|
||||
@@ -185,28 +185,17 @@ export default class ExpressionParser extends LValParser {
|
||||
|
||||
let patternErrorMsg;
|
||||
let elementName;
|
||||
|
||||
const unwrap = node => {
|
||||
return node.type === "ParenthesizedExpression"
|
||||
? unwrap(node.expression)
|
||||
: node;
|
||||
};
|
||||
const maybePattern = unwrap(left);
|
||||
if (maybePattern.type === "ObjectPattern") {
|
||||
if (left.type === "ObjectPattern") {
|
||||
patternErrorMsg = "`({a}) = 0` use `({a} = 0)`";
|
||||
elementName = "property";
|
||||
} else if (maybePattern.type === "ArrayPattern") {
|
||||
} else if (left.type === "ArrayPattern") {
|
||||
patternErrorMsg = "`([a]) = 0` use `([a] = 0)`";
|
||||
elementName = "element";
|
||||
}
|
||||
|
||||
if (
|
||||
patternErrorMsg &&
|
||||
((left.extra && left.extra.parenthesized) ||
|
||||
left.type === "ParenthesizedExpression")
|
||||
) {
|
||||
if (patternErrorMsg && left.extra && left.extra.parenthesized) {
|
||||
this.raise(
|
||||
maybePattern.start,
|
||||
left.start,
|
||||
`You're trying to assign to a parenthesized expression, eg. instead of ${patternErrorMsg}`,
|
||||
);
|
||||
}
|
||||
@@ -320,8 +309,7 @@ export default class ExpressionParser extends LValParser {
|
||||
if (
|
||||
operator === "**" &&
|
||||
left.type === "UnaryExpression" &&
|
||||
(this.options.createParenthesizedExpressions ||
|
||||
!(left.extra && left.extra.parenthesized))
|
||||
!(left.extra && left.extra.parenthesized)
|
||||
) {
|
||||
this.raise(
|
||||
left.argument.start,
|
||||
@@ -1167,6 +1155,13 @@ export default class ExpressionParser extends LValParser {
|
||||
return this.finishNode(node, type);
|
||||
}
|
||||
|
||||
parseParenExpression(): N.Expression {
|
||||
this.expect(tt.parenL);
|
||||
const val = this.parseExpression();
|
||||
this.expect(tt.parenR);
|
||||
return val;
|
||||
}
|
||||
|
||||
parseParenAndDistinguishExpression(canBeArrow: boolean): N.Expression {
|
||||
const startPos = this.state.start;
|
||||
const startLoc = this.state.startLoc;
|
||||
@@ -1277,16 +1272,10 @@ export default class ExpressionParser extends LValParser {
|
||||
val = exprList[0];
|
||||
}
|
||||
|
||||
if (!this.options.createParenthesizedExpressions) {
|
||||
this.addExtra(val, "parenthesized", true);
|
||||
this.addExtra(val, "parenStart", startPos);
|
||||
return val;
|
||||
}
|
||||
this.addExtra(val, "parenthesized", true);
|
||||
this.addExtra(val, "parenStart", startPos);
|
||||
|
||||
const parenExpression = this.startNodeAt(startPos, startLoc);
|
||||
parenExpression.expression = val;
|
||||
this.finishNode(parenExpression, "ParenthesizedExpression");
|
||||
return parenExpression;
|
||||
return val;
|
||||
}
|
||||
|
||||
shouldParseArrow(): boolean {
|
||||
|
||||
@@ -92,14 +92,6 @@ export default class LValParser extends NodeUtils {
|
||||
}
|
||||
break;
|
||||
|
||||
case "ParenthesizedExpression":
|
||||
node.expression = this.toAssignable(
|
||||
node.expression,
|
||||
isBinding,
|
||||
contextDescription,
|
||||
);
|
||||
break;
|
||||
|
||||
case "MemberExpression":
|
||||
if (!isBinding) break;
|
||||
|
||||
@@ -415,15 +407,6 @@ export default class LValParser extends NodeUtils {
|
||||
this.checkLVal(expr.argument, isBinding, checkClashes, "rest element");
|
||||
break;
|
||||
|
||||
case "ParenthesizedExpression":
|
||||
this.checkLVal(
|
||||
expr.expression,
|
||||
isBinding,
|
||||
checkClashes,
|
||||
"parenthesized expression",
|
||||
);
|
||||
break;
|
||||
|
||||
default: {
|
||||
const message =
|
||||
(isBinding
|
||||
|
||||
@@ -426,13 +426,6 @@ export default class StatementParser extends ExpressionParser {
|
||||
return this.finishNode(node, "DebuggerStatement");
|
||||
}
|
||||
|
||||
parseHeaderExpression(): N.Expression {
|
||||
this.expect(tt.parenL);
|
||||
const val = this.parseExpression();
|
||||
this.expect(tt.parenR);
|
||||
return val;
|
||||
}
|
||||
|
||||
parseDoStatement(node: N.DoWhileStatement): N.DoWhileStatement {
|
||||
this.next();
|
||||
this.state.labels.push(loopLabel);
|
||||
@@ -449,7 +442,7 @@ export default class StatementParser extends ExpressionParser {
|
||||
this.state.labels.pop();
|
||||
|
||||
this.expect(tt._while);
|
||||
node.test = this.parseHeaderExpression();
|
||||
node.test = this.parseParenExpression();
|
||||
this.eat(tt.semi);
|
||||
return this.finishNode(node, "DoWhileStatement");
|
||||
}
|
||||
@@ -538,7 +531,7 @@ export default class StatementParser extends ExpressionParser {
|
||||
|
||||
parseIfStatement(node: N.IfStatement): N.IfStatement {
|
||||
this.next();
|
||||
node.test = this.parseHeaderExpression();
|
||||
node.test = this.parseParenExpression();
|
||||
node.consequent = this.parseStatement("if");
|
||||
node.alternate = this.eat(tt._else) ? this.parseStatement("if") : null;
|
||||
return this.finishNode(node, "IfStatement");
|
||||
@@ -567,7 +560,7 @@ export default class StatementParser extends ExpressionParser {
|
||||
|
||||
parseSwitchStatement(node: N.SwitchStatement): N.SwitchStatement {
|
||||
this.next();
|
||||
node.discriminant = this.parseHeaderExpression();
|
||||
node.discriminant = this.parseParenExpression();
|
||||
const cases = (node.cases = []);
|
||||
this.expect(tt.braceL);
|
||||
this.state.labels.push(switchLabel);
|
||||
@@ -676,7 +669,7 @@ export default class StatementParser extends ExpressionParser {
|
||||
|
||||
parseWhileStatement(node: N.WhileStatement): N.WhileStatement {
|
||||
this.next();
|
||||
node.test = this.parseHeaderExpression();
|
||||
node.test = this.parseParenExpression();
|
||||
this.state.labels.push(loopLabel);
|
||||
|
||||
node.body =
|
||||
@@ -698,7 +691,7 @@ export default class StatementParser extends ExpressionParser {
|
||||
this.raise(this.state.start, "'with' in strict mode");
|
||||
}
|
||||
this.next();
|
||||
node.object = this.parseHeaderExpression();
|
||||
node.object = this.parseParenExpression();
|
||||
|
||||
node.body =
|
||||
// For the smartPipelines plugin:
|
||||
|
||||
@@ -566,11 +566,6 @@ export type SequenceExpression = NodeBase & {
|
||||
expressions: $ReadOnlyArray<Expression>,
|
||||
};
|
||||
|
||||
export type ParenthesizedExpression = NodeBase & {
|
||||
type: "ParenthesizedExpression",
|
||||
expression: Expression,
|
||||
};
|
||||
|
||||
// Pipelines
|
||||
|
||||
export type PipelineBody = NodeBase & {
|
||||
|
||||
Reference in New Issue
Block a user