Skip to content

Commit

Permalink
Fix missing parens in reprinted output
Browse files Browse the repository at this point in the history
  • Loading branch information
conartist6 committed Mar 3, 2022
1 parent cb7ff9a commit 2956890
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 294 deletions.
332 changes: 166 additions & 166 deletions lib/fast-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,6 @@ FPp.needsParens = function (assumeExpressionContext) {
}

const parent = this.getParentNode();
if (!parent) {
return false;
}

const name = this.getName();

Expand All @@ -347,197 +344,200 @@ FPp.needsParens = function (assumeExpressionContext) {
return false;
}

if (
parent.type === "ParenthesizedExpression" ||
(node.extra && node.extra.parenthesized)
) {
if (parent && parent.type === "ParenthesizedExpression") {
return false;
}

switch (node.type) {
case "UnaryExpression":
case "SpreadElement":
case "SpreadProperty":
return (
parent.type === "MemberExpression" &&
name === "object" &&
parent.object === node
);

case "BinaryExpression":
case "LogicalExpression":
switch (parent.type) {
case "CallExpression":
return name === "callee" && parent.callee === node;

case "UnaryExpression":
case "SpreadElement":
case "SpreadProperty":
return true;
if (node.extra && node.extra.parenthesized) {
return true;
}

case "MemberExpression":
return name === "object" && parent.object === node;
if (parent) {
switch (node.type) {
case "UnaryExpression":
case "SpreadElement":
case "SpreadProperty":
return (
parent.type === "MemberExpression" &&
name === "object" &&
parent.object === node
);

case "BinaryExpression":
case "LogicalExpression":
switch (parent.type) {
case "CallExpression":
return name === "callee" && parent.callee === node;

case "UnaryExpression":
case "SpreadElement":
case "SpreadProperty":
return true;

case "BinaryExpression":
case "LogicalExpression": {
const po = parent.operator;
const pp = PRECEDENCE[po];
const no = node.operator;
const np = PRECEDENCE[no];
case "MemberExpression":
return name === "object" && parent.object === node;

if (pp > np) {
return true;
}
case "BinaryExpression":
case "LogicalExpression": {
const po = parent.operator;
const pp = PRECEDENCE[po];
const no = node.operator;
const np = PRECEDENCE[no];

if (pp === np && name === "right") {
assert.strictEqual(parent.right, node);
return true;
if (pp > np) {
return true;
}

if (pp === np && name === "right") {
assert.strictEqual(parent.right, node);
return true;
}

break;
}

break;
default:
return false;
}

default:
return false;
}

break;
break;

case "SequenceExpression":
switch (parent.type) {
case "ReturnStatement":
return false;
case "SequenceExpression":
switch (parent.type) {
case "ReturnStatement":
return false;

case "ForStatement":
// Although parentheses wouldn't hurt around sequence expressions in
// the head of for loops, traditional style dictates that e.g. i++,
// j++ should not be wrapped with parentheses.
return false;
case "ForStatement":
// Although parentheses wouldn't hurt around sequence expressions in
// the head of for loops, traditional style dictates that e.g. i++,
// j++ should not be wrapped with parentheses.
return false;

case "ExpressionStatement":
return name !== "expression";
case "ExpressionStatement":
return name !== "expression";

default:
// Otherwise err on the side of overparenthesization, adding
// explicit exceptions above if this proves overzealous.
return true;
}
default:
// Otherwise err on the side of overparenthesization, adding
// explicit exceptions above if this proves overzealous.
return true;
}

case "OptionalIndexedAccessType":
return node.optional && parent.type === "IndexedAccessType";

case "IntersectionTypeAnnotation":
case "UnionTypeAnnotation":
return parent.type === "NullableTypeAnnotation";

case "Literal":
return (
parent.type === "MemberExpression" &&
isNumber.check(node.value) &&
name === "object" &&
parent.object === node
);

// Babel 6 Literal split
case "NumericLiteral":
return (
parent.type === "MemberExpression" &&
name === "object" &&
parent.object === node
);

case "YieldExpression":
case "AwaitExpression":
case "AssignmentExpression":
case "ConditionalExpression":
switch (parent.type) {
case "UnaryExpression":
case "SpreadElement":
case "SpreadProperty":
case "BinaryExpression":
case "LogicalExpression":
return true;
case "OptionalIndexedAccessType":
return node.optional && parent.type === "IndexedAccessType";

case "IntersectionTypeAnnotation":
case "UnionTypeAnnotation":
return parent.type === "NullableTypeAnnotation";

case "Literal":
return (
parent.type === "MemberExpression" &&
isNumber.check(node.value) &&
name === "object" &&
parent.object === node
);

// Babel 6 Literal split
case "NumericLiteral":
return (
parent.type === "MemberExpression" &&
name === "object" &&
parent.object === node
);

case "YieldExpression":
case "AwaitExpression":
case "AssignmentExpression":
case "ConditionalExpression":
switch (parent.type) {
case "UnaryExpression":
case "SpreadElement":
case "SpreadProperty":
case "BinaryExpression":
case "LogicalExpression":
return true;

case "CallExpression":
case "NewExpression":
return name === "callee" && parent.callee === node;
case "CallExpression":
case "NewExpression":
return name === "callee" && parent.callee === node;

case "ConditionalExpression":
return name === "test" && parent.test === node;
case "ConditionalExpression":
return name === "test" && parent.test === node;

case "MemberExpression":
return name === "object" && parent.object === node;
case "MemberExpression":
return name === "object" && parent.object === node;

default:
return false;
}
default:
return false;
}

case "ArrowFunctionExpression":
if (
n.CallExpression.check(parent) &&
name === "callee" &&
parent.callee === node
) {
return true;
}
case "ArrowFunctionExpression":
if (
n.CallExpression.check(parent) &&
name === "callee" &&
parent.callee === node
) {
return true;
}

if (
n.MemberExpression.check(parent) &&
name === "object" &&
parent.object === node
) {
return true;
}
if (
n.MemberExpression.check(parent) &&
name === "object" &&
parent.object === node
) {
return true;
}

if (
n.TSAsExpression &&
n.TSAsExpression.check(parent) &&
name === "expression" &&
parent.expression === node
) {
return true;
}
if (
n.TSAsExpression &&
n.TSAsExpression.check(parent) &&
name === "expression" &&
parent.expression === node
) {
return true;
}

return isBinary(parent);
return isBinary(parent);

case "ObjectExpression":
if (
parent.type === "ArrowFunctionExpression" &&
name === "body" &&
parent.body === node
) {
return true;
}
case "ObjectExpression":
if (
parent.type === "ArrowFunctionExpression" &&
name === "body" &&
parent.body === node
) {
return true;
}

break;
break;

case "TSAsExpression":
if (
parent.type === "ArrowFunctionExpression" &&
name === "body" &&
parent.body === node &&
node.expression.type === "ObjectExpression"
) {
return true;
}
break;

case "CallExpression":
if (
name === "declaration" &&
n.ExportDefaultDeclaration.check(parent) &&
n.FunctionExpression.check(node.callee)
) {
return true;
}
}
case "TSAsExpression":
if (
parent.type === "ArrowFunctionExpression" &&
name === "body" &&
parent.body === node &&
node.expression.type === "ObjectExpression"
) {
return true;
}
break;

case "CallExpression":
if (
name === "declaration" &&
n.ExportDefaultDeclaration.check(parent) &&
n.FunctionExpression.check(node.callee)
) {
return true;
}
}

if (
parent.type === "NewExpression" &&
name === "callee" &&
parent.callee === node
) {
return containsCallExpression(node);
if (
parent.type === "NewExpression" &&
name === "callee" &&
parent.callee === node
) {
return containsCallExpression(node);
}
}

if (
Expand Down
Loading

0 comments on commit 2956890

Please sign in to comment.