cleanup needsParens (#935)
parent
1b6ddf9a7f
commit
06987e9948
|
@ -264,26 +264,12 @@ FPp.needsParens = function(assumeExpressionContext) {
|
||||||
parent.object === node;
|
parent.object === node;
|
||||||
|
|
||||||
case "UpdateExpression":
|
case "UpdateExpression":
|
||||||
switch (parent.type) {
|
if (parent.type === "UnaryExpression") {
|
||||||
case "MemberExpression":
|
return node.prefix &&
|
||||||
return name === "object" && parent.object === node;
|
|
||||||
|
|
||||||
case "TaggedTemplateExpression":
|
|
||||||
case "NewExpression":
|
|
||||||
return true;
|
|
||||||
|
|
||||||
case "UnaryExpression":
|
|
||||||
if (
|
|
||||||
node.prefix &&
|
|
||||||
((node.operator === "++" && parent.operator === "+") ||
|
((node.operator === "++" && parent.operator === "+") ||
|
||||||
(node.operator === "--" && parent.operator === "-"))
|
(node.operator === "--" && parent.operator === "-"));
|
||||||
) {
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
// else fall through
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
case "UnaryExpression":
|
case "UnaryExpression":
|
||||||
switch (parent.type) {
|
switch (parent.type) {
|
||||||
case "UnaryExpression":
|
case "UnaryExpression":
|
||||||
|
@ -292,9 +278,20 @@ FPp.needsParens = function(assumeExpressionContext) {
|
||||||
|
|
||||||
case "MemberExpression":
|
case "MemberExpression":
|
||||||
return name === "object" && parent.object === node;
|
return name === "object" && parent.object === node;
|
||||||
|
|
||||||
|
case "TaggedTemplateExpression":
|
||||||
|
return true;
|
||||||
|
|
||||||
|
case "NewExpression":
|
||||||
|
case "CallExpression":
|
||||||
|
return name === "callee" && parent.callee == node;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
case "BinaryExpression":
|
case "BinaryExpression":
|
||||||
|
// TODO https://github.com/prettier/prettier/issues/907 this is not complete
|
||||||
if (
|
if (
|
||||||
node.operator === "in" &&
|
node.operator === "in" &&
|
||||||
parent.type === "ForStatement" &&
|
parent.type === "ForStatement" &&
|
||||||
|
@ -302,11 +299,10 @@ FPp.needsParens = function(assumeExpressionContext) {
|
||||||
) {
|
) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (node.operator === "in" && parent.type === "AssignmentExpression") {
|
if (node.operator === "in" && parent.type === "AssignmentExpression") {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
// else fall through
|
||||||
case "LogicalExpression":
|
case "LogicalExpression":
|
||||||
switch (parent.type) {
|
switch (parent.type) {
|
||||||
case "CallExpression":
|
case "CallExpression":
|
||||||
|
@ -371,6 +367,7 @@ FPp.needsParens = function(assumeExpressionContext) {
|
||||||
if (parent.type === "UnaryExpression") {
|
if (parent.type === "UnaryExpression") {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
// else fall through
|
||||||
case "AwaitExpression":
|
case "AwaitExpression":
|
||||||
switch (parent.type) {
|
switch (parent.type) {
|
||||||
case "TaggedTemplateExpression":
|
case "TaggedTemplateExpression":
|
||||||
|
@ -419,18 +416,13 @@ FPp.needsParens = function(assumeExpressionContext) {
|
||||||
case "AssignmentExpression":
|
case "AssignmentExpression":
|
||||||
if (parent.type === "ArrowFunctionExpression" && parent.body === node) {
|
if (parent.type === "ArrowFunctionExpression" && parent.body === node) {
|
||||||
return node.left.type === "ObjectPattern";
|
return node.left.type === "ObjectPattern";
|
||||||
}
|
} else if (
|
||||||
if (
|
|
||||||
parent.type === "ForStatement" &&
|
parent.type === "ForStatement" &&
|
||||||
(parent.init === node || parent.update === node)
|
(parent.init === node || parent.update === node)
|
||||||
) {
|
) {
|
||||||
return false;
|
return false;
|
||||||
}
|
} else if (parent.type === "ExpressionStatement") {
|
||||||
if (parent.type === "ExpressionStatement") {
|
return node.left.type === "ObjectPattern";
|
||||||
if (node.left.type === "ObjectPattern") {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
@ -458,7 +450,7 @@ FPp.needsParens = function(assumeExpressionContext) {
|
||||||
return name === "object" && parent.object === node;
|
return name === "object" && parent.object === node;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return n.ObjectPattern.check(node.left) && this.firstInStatement();
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
case "FunctionExpression":
|
case "FunctionExpression":
|
||||||
|
@ -516,16 +508,14 @@ FPp.needsParens = function(assumeExpressionContext) {
|
||||||
if (parent.test === node) {
|
if (parent.test === node) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
default:
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
case "StringLiteral":
|
case "StringLiteral":
|
||||||
if (parent.type === "ExpressionStatement") {
|
return parent.type === "ExpressionStatement"; // to avoid becoming a directive
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
|
||||||
if (
|
if (
|
||||||
parent.type === "NewExpression" &&
|
parent.type === "NewExpression" &&
|
||||||
name === "callee" &&
|
name === "callee" &&
|
||||||
|
@ -533,14 +523,14 @@ FPp.needsParens = function(assumeExpressionContext) {
|
||||||
) {
|
) {
|
||||||
return containsCallExpression(node);
|
return containsCallExpression(node);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
assumeExpressionContext !== true &&
|
assumeExpressionContext !== true &&
|
||||||
!this.canBeFirstInStatement() &&
|
!this.canBeFirstInStatement() &&
|
||||||
this.firstInStatement()
|
this.firstInStatement()
|
||||||
)
|
) {
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
|
@ -7,12 +7,16 @@ x--;
|
||||||
-+1;
|
-+1;
|
||||||
x + + + + 1;
|
x + + + + 1;
|
||||||
x + (+ (+ (+ 1)))
|
x + (+ (+ (+ 1)))
|
||||||
|
x * +y;
|
||||||
|
+x * y;
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
!!x;
|
!!x;
|
||||||
x++;
|
x++;
|
||||||
x--;
|
x--;
|
||||||
-+1;
|
-+1;
|
||||||
x + (+(+(+1)));
|
x + +(+(+1));
|
||||||
x + (+(+(+1)));
|
x + +(+(+1));
|
||||||
|
x * +y;
|
||||||
|
+x * y;
|
||||||
"
|
"
|
||||||
`;
|
`;
|
||||||
|
|
|
@ -4,3 +4,5 @@ x--;
|
||||||
-+1;
|
-+1;
|
||||||
x + + + + 1;
|
x + + + + 1;
|
||||||
x + (+ (+ (+ 1)))
|
x + (+ (+ (+ 1)))
|
||||||
|
x * +y;
|
||||||
|
+x * y;
|
||||||
|
|
Loading…
Reference in New Issue