Parenthesized expressions (#8025)
* Add parser createParenthesizedExpressions option … When set to `true` we create `ParenthesizedExpression` nodes instead of setting `extra.parenthesized`. * Also update babel-parser.d.ts
This commit is contained in:
committed by
Justin Ridgewell
parent
417e72ebfd
commit
dd8b700a2c
@@ -185,17 +185,28 @@ export default class ExpressionParser extends LValParser {
|
||||
|
||||
let patternErrorMsg;
|
||||
let elementName;
|
||||
if (left.type === "ObjectPattern") {
|
||||
|
||||
const unwrap = node => {
|
||||
return node.type === "ParenthesizedExpression"
|
||||
? unwrap(node.expression)
|
||||
: node;
|
||||
};
|
||||
const maybePattern = unwrap(left);
|
||||
if (maybePattern.type === "ObjectPattern") {
|
||||
patternErrorMsg = "`({a}) = 0` use `({a} = 0)`";
|
||||
elementName = "property";
|
||||
} else if (left.type === "ArrayPattern") {
|
||||
} else if (maybePattern.type === "ArrayPattern") {
|
||||
patternErrorMsg = "`([a]) = 0` use `([a] = 0)`";
|
||||
elementName = "element";
|
||||
}
|
||||
|
||||
if (patternErrorMsg && left.extra && left.extra.parenthesized) {
|
||||
if (
|
||||
patternErrorMsg &&
|
||||
((left.extra && left.extra.parenthesized) ||
|
||||
left.type === "ParenthesizedExpression")
|
||||
) {
|
||||
this.raise(
|
||||
left.start,
|
||||
maybePattern.start,
|
||||
`You're trying to assign to a parenthesized expression, eg. instead of ${patternErrorMsg}`,
|
||||
);
|
||||
}
|
||||
@@ -309,7 +320,8 @@ export default class ExpressionParser extends LValParser {
|
||||
if (
|
||||
operator === "**" &&
|
||||
left.type === "UnaryExpression" &&
|
||||
!(left.extra && left.extra.parenthesized)
|
||||
(this.options.createParenthesizedExpressions ||
|
||||
!(left.extra && left.extra.parenthesized))
|
||||
) {
|
||||
this.raise(
|
||||
left.argument.start,
|
||||
@@ -1155,13 +1167,6 @@ 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;
|
||||
@@ -1272,10 +1277,16 @@ export default class ExpressionParser extends LValParser {
|
||||
val = exprList[0];
|
||||
}
|
||||
|
||||
this.addExtra(val, "parenthesized", true);
|
||||
this.addExtra(val, "parenStart", startPos);
|
||||
if (!this.options.createParenthesizedExpressions) {
|
||||
this.addExtra(val, "parenthesized", true);
|
||||
this.addExtra(val, "parenStart", startPos);
|
||||
return val;
|
||||
}
|
||||
|
||||
return val;
|
||||
const parenExpression = this.startNodeAt(startPos, startLoc);
|
||||
parenExpression.expression = val;
|
||||
this.finishNode(parenExpression, "ParenthesizedExpression");
|
||||
return parenExpression;
|
||||
}
|
||||
|
||||
shouldParseArrow(): boolean {
|
||||
|
||||
Reference in New Issue
Block a user