summaryrefslogtreecommitdiff
path: root/node_modules/@babel/generator/lib/node/parentheses.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/@babel/generator/lib/node/parentheses.js')
-rw-r--r--node_modules/@babel/generator/lib/node/parentheses.js196
1 files changed, 58 insertions, 138 deletions
diff --git a/node_modules/@babel/generator/lib/node/parentheses.js b/node_modules/@babel/generator/lib/node/parentheses.js
index 4656a67c..475e33ac 100644
--- a/node_modules/@babel/generator/lib/node/parentheses.js
+++ b/node_modules/@babel/generator/lib/node/parentheses.js
@@ -31,97 +31,35 @@ var _t = require("@babel/types");
const {
isArrayTypeAnnotation,
isArrowFunctionExpression,
- isAssignmentExpression,
- isAwaitExpression,
- isBinary,
isBinaryExpression,
- isUpdateExpression,
isCallExpression,
- isClass,
- isClassExpression,
- isConditional,
- isConditionalExpression,
isExportDeclaration,
- isExportDefaultDeclaration,
- isExpressionStatement,
- isFor,
- isForInStatement,
isForOfStatement,
- isForStatement,
- isFunctionExpression,
- isIfStatement,
isIndexedAccessType,
- isIntersectionTypeAnnotation,
- isLogicalExpression,
isMemberExpression,
- isNewExpression,
- isNullableTypeAnnotation,
isObjectPattern,
- isOptionalCallExpression,
isOptionalMemberExpression,
- isReturnStatement,
- isSequenceExpression,
- isSwitchStatement,
- isTSArrayType,
- isTSAsExpression,
- isTSInstantiationExpression,
- isTSIntersectionType,
- isTSNonNullExpression,
- isTSOptionalType,
- isTSRestType,
- isTSTypeAssertion,
- isTSUnionType,
- isTaggedTemplateExpression,
- isThrowStatement,
- isTypeAnnotation,
- isUnaryLike,
- isUnionTypeAnnotation,
- isVariableDeclarator,
- isWhileStatement,
- isYieldExpression,
- isTSSatisfiesExpression
+ isYieldExpression
} = _t;
-const PRECEDENCE = {
- "||": 0,
- "??": 0,
- "|>": 0,
- "&&": 1,
- "|": 2,
- "^": 3,
- "&": 4,
- "==": 5,
- "===": 5,
- "!=": 5,
- "!==": 5,
- "<": 6,
- ">": 6,
- "<=": 6,
- ">=": 6,
- in: 6,
- instanceof: 6,
- ">>": 7,
- "<<": 7,
- ">>>": 7,
- "+": 8,
- "-": 8,
- "*": 9,
- "/": 9,
- "%": 9,
- "**": 10
-};
-function isTSTypeExpression(node) {
- return isTSAsExpression(node) || isTSSatisfiesExpression(node) || isTSTypeAssertion(node);
+const PRECEDENCE = new Map([["||", 0], ["??", 0], ["|>", 0], ["&&", 1], ["|", 2], ["^", 3], ["&", 4], ["==", 5], ["===", 5], ["!=", 5], ["!==", 5], ["<", 6], [">", 6], ["<=", 6], [">=", 6], ["in", 6], ["instanceof", 6], [">>", 7], ["<<", 7], [">>>", 7], ["+", 8], ["-", 8], ["*", 9], ["/", 9], ["%", 9], ["**", 10]]);
+function isTSTypeExpression(nodeType) {
+ return nodeType === "TSAsExpression" || nodeType === "TSSatisfiesExpression" || nodeType === "TSTypeAssertion";
}
-const isClassExtendsClause = (node, parent) => isClass(parent, {
- superClass: node
-});
-const hasPostfixPart = (node, parent) => (isMemberExpression(parent) || isOptionalMemberExpression(parent)) && parent.object === node || (isCallExpression(parent) || isOptionalCallExpression(parent) || isNewExpression(parent)) && parent.callee === node || isTaggedTemplateExpression(parent) && parent.tag === node || isTSNonNullExpression(parent);
+const isClassExtendsClause = (node, parent) => {
+ const parentType = parent.type;
+ return (parentType === "ClassDeclaration" || parentType === "ClassExpression") && parent.superClass === node;
+};
+const hasPostfixPart = (node, parent) => {
+ const parentType = parent.type;
+ return (parentType === "MemberExpression" || parentType === "OptionalMemberExpression") && parent.object === node || (parentType === "CallExpression" || parentType === "OptionalCallExpression" || parentType === "NewExpression") && parent.callee === node || parentType === "TaggedTemplateExpression" && parent.tag === node || parentType === "TSNonNullExpression";
+};
function NullableTypeAnnotation(node, parent) {
return isArrayTypeAnnotation(parent);
}
function FunctionTypeAnnotation(node, parent, printStack) {
if (printStack.length < 3) return;
- return isUnionTypeAnnotation(parent) || isIntersectionTypeAnnotation(parent) || isArrayTypeAnnotation(parent) || isTypeAnnotation(parent) && isArrowFunctionExpression(printStack[printStack.length - 3]);
+ const parentType = parent.type;
+ return parentType === "UnionTypeAnnotation" || parentType === "IntersectionTypeAnnotation" || parentType === "ArrayTypeAnnotation" || parentType === "TypeAnnotation" && isArrowFunctionExpression(printStack[printStack.length - 3]);
}
function UpdateExpression(node, parent) {
return hasPostfixPart(node, parent) || isClassExtendsClause(node, parent);
@@ -133,67 +71,70 @@ function DoExpression(node, parent, printStack) {
return !node.async && isFirstInContext(printStack, 1);
}
function Binary(node, parent) {
- if (node.operator === "**" && isBinaryExpression(parent, {
- operator: "**"
- })) {
+ const parentType = parent.type;
+ if (node.operator === "**" && parentType === "BinaryExpression" && parent.operator === "**") {
return parent.left === node;
}
if (isClassExtendsClause(node, parent)) {
return true;
}
- if (hasPostfixPart(node, parent) || isUnaryLike(parent) || isAwaitExpression(parent)) {
+ if (hasPostfixPart(node, parent) || parentType === "UnaryExpression" || parentType === "SpreadElement" || parentType === "AwaitExpression") {
return true;
}
- if (isBinary(parent)) {
- const parentOp = parent.operator;
- const parentPos = PRECEDENCE[parentOp];
- const nodeOp = node.operator;
- const nodePos = PRECEDENCE[nodeOp];
- if (parentPos === nodePos && parent.right === node && !isLogicalExpression(parent) || parentPos > nodePos) {
+ if (parentType === "BinaryExpression" || parentType === "LogicalExpression") {
+ const parentPos = PRECEDENCE.get(parent.operator);
+ const nodePos = PRECEDENCE.get(node.operator);
+ if (parentPos === nodePos && parent.right === node && parentType !== "LogicalExpression" || parentPos > nodePos) {
return true;
}
}
+ return undefined;
}
function UnionTypeAnnotation(node, parent) {
- return isArrayTypeAnnotation(parent) || isNullableTypeAnnotation(parent) || isIntersectionTypeAnnotation(parent) || isUnionTypeAnnotation(parent);
+ const parentType = parent.type;
+ return parentType === "ArrayTypeAnnotation" || parentType === "NullableTypeAnnotation" || parentType === "IntersectionTypeAnnotation" || parentType === "UnionTypeAnnotation";
}
function OptionalIndexedAccessType(node, parent) {
- return isIndexedAccessType(parent, {
- objectType: node
- });
+ return isIndexedAccessType(parent) && parent.objectType === node;
}
function TSAsExpression() {
return true;
}
function TSUnionType(node, parent) {
- return isTSArrayType(parent) || isTSOptionalType(parent) || isTSIntersectionType(parent) || isTSUnionType(parent) || isTSRestType(parent);
+ const parentType = parent.type;
+ return parentType === "TSArrayType" || parentType === "TSOptionalType" || parentType === "TSIntersectionType" || parentType === "TSUnionType" || parentType === "TSRestType";
}
function TSInferType(node, parent) {
- return isTSArrayType(parent) || isTSOptionalType(parent);
+ const parentType = parent.type;
+ return parentType === "TSArrayType" || parentType === "TSOptionalType";
}
function TSInstantiationExpression(node, parent) {
- return (isCallExpression(parent) || isOptionalCallExpression(parent) || isNewExpression(parent) || isTSInstantiationExpression(parent)) && !!parent.typeParameters;
+ const parentType = parent.type;
+ return (parentType === "CallExpression" || parentType === "OptionalCallExpression" || parentType === "NewExpression" || parentType === "TSInstantiationExpression") && !!parent.typeParameters;
}
function BinaryExpression(node, parent) {
- return node.operator === "in" && (isVariableDeclarator(parent) || isFor(parent));
+ if (node.operator === "in") {
+ const parentType = parent.type;
+ return parentType === "VariableDeclarator" || parentType === "ForStatement" || parentType === "ForInStatement" || parentType === "ForOfStatement";
+ }
+ return false;
}
function SequenceExpression(node, parent) {
- if (isForStatement(parent) || isThrowStatement(parent) || isReturnStatement(parent) || isIfStatement(parent) && parent.test === node || isWhileStatement(parent) && parent.test === node || isForInStatement(parent) && parent.right === node || isSwitchStatement(parent) && parent.discriminant === node || isExpressionStatement(parent) && parent.expression === node) {
+ const parentType = parent.type;
+ if (parentType === "ForStatement" || parentType === "ThrowStatement" || parentType === "ReturnStatement" || parentType === "IfStatement" && parent.test === node || parentType === "WhileStatement" && parent.test === node || parentType === "ForInStatement" && parent.right === node || parentType === "SwitchStatement" && parent.discriminant === node || parentType === "ExpressionStatement" && parent.expression === node) {
return false;
}
return true;
}
function YieldExpression(node, parent) {
- return isBinary(parent) || isUnaryLike(parent) || hasPostfixPart(node, parent) || isAwaitExpression(parent) && isYieldExpression(node) || isConditionalExpression(parent) && node === parent.test || isClassExtendsClause(node, parent);
+ const parentType = parent.type;
+ return parentType === "BinaryExpression" || parentType === "LogicalExpression" || parentType === "UnaryExpression" || parentType === "SpreadElement" || hasPostfixPart(node, parent) || parentType === "AwaitExpression" && isYieldExpression(node) || parentType === "ConditionalExpression" && node === parent.test || isClassExtendsClause(node, parent);
}
function ClassExpression(node, parent, printStack) {
return isFirstInContext(printStack, 1 | 4);
}
function UnaryLike(node, parent) {
- return hasPostfixPart(node, parent) || isBinaryExpression(parent, {
- operator: "**",
- left: node
- }) || isClassExtendsClause(node, parent);
+ return hasPostfixPart(node, parent) || isBinaryExpression(parent) && parent.operator === "**" && parent.left === node || isClassExtendsClause(node, parent);
}
function FunctionExpression(node, parent, printStack) {
return isFirstInContext(printStack, 1 | 4);
@@ -202,19 +143,14 @@ function ArrowFunctionExpression(node, parent) {
return isExportDeclaration(parent) || ConditionalExpression(node, parent);
}
function ConditionalExpression(node, parent) {
- if (isUnaryLike(parent) || isBinary(parent) || isConditionalExpression(parent, {
- test: node
- }) || isAwaitExpression(parent) || isTSTypeExpression(parent)) {
+ const parentType = parent.type;
+ if (parentType === "UnaryExpression" || parentType === "SpreadElement" || parentType === "BinaryExpression" || parentType === "LogicalExpression" || parentType === "ConditionalExpression" && parent.test === node || parentType === "AwaitExpression" || isTSTypeExpression(parentType)) {
return true;
}
return UnaryLike(node, parent);
}
function OptionalMemberExpression(node, parent) {
- return isCallExpression(parent, {
- callee: node
- }) || isMemberExpression(parent, {
- object: node
- });
+ return isCallExpression(parent) && parent.callee === node || isMemberExpression(parent) && parent.object === node;
}
function AssignmentExpression(node, parent) {
if (isObjectPattern(node.left)) {
@@ -224,25 +160,26 @@ function AssignmentExpression(node, parent) {
}
}
function LogicalExpression(node, parent) {
- if (isTSTypeExpression(parent)) return true;
+ const parentType = parent.type;
+ if (isTSTypeExpression(parentType)) return true;
+ if (parentType !== "LogicalExpression") return false;
switch (node.operator) {
case "||":
- if (!isLogicalExpression(parent)) return false;
return parent.operator === "??" || parent.operator === "&&";
case "&&":
- return isLogicalExpression(parent, {
- operator: "??"
- });
+ return parent.operator === "??";
case "??":
- return isLogicalExpression(parent) && parent.operator !== "??";
+ return parent.operator !== "??";
}
}
function Identifier(node, parent, printStack) {
var _node$extra;
- if ((_node$extra = node.extra) != null && _node$extra.parenthesized && isAssignmentExpression(parent, {
- left: node
- }) && (isFunctionExpression(parent.right) || isClassExpression(parent.right)) && parent.right.id == null) {
- return true;
+ const parentType = parent.type;
+ if ((_node$extra = node.extra) != null && _node$extra.parenthesized && parentType === "AssignmentExpression" && parent.left === node) {
+ const rightType = parent.right.type;
+ if ((rightType === "FunctionExpression" || rightType === "ClassExpression") && parent.right.id == null) {
+ return true;
+ }
}
if (node.name === "let") {
const isFollowedByBracket = isMemberExpression(parent, {
@@ -270,28 +207,11 @@ function isFirstInContext(printStack, checkParam) {
i--;
let parent = printStack[i];
while (i >= 0) {
- if (expressionStatement && isExpressionStatement(parent, {
- expression: node
- }) || exportDefault && isExportDefaultDeclaration(parent, {
- declaration: node
- }) || arrowBody && isArrowFunctionExpression(parent, {
- body: node
- }) || forHead && isForStatement(parent, {
- init: node
- }) || forInHead && isForInStatement(parent, {
- left: node
- }) || forOfHead && isForOfStatement(parent, {
- left: node
- })) {
+ const parentType = parent.type;
+ if (expressionStatement && parentType === "ExpressionStatement" && parent.expression === node || exportDefault && parentType === "ExportDefaultDeclaration" && node === parent.declaration || arrowBody && parentType === "ArrowFunctionExpression" && parent.body === node || forHead && parentType === "ForStatement" && parent.init === node || forInHead && parentType === "ForInStatement" && parent.left === node || forOfHead && parentType === "ForOfStatement" && parent.left === node) {
return true;
}
- if (i > 0 && (hasPostfixPart(node, parent) && !isNewExpression(parent) || isSequenceExpression(parent) && parent.expressions[0] === node || isUpdateExpression(parent) && !parent.prefix || isConditional(parent, {
- test: node
- }) || isBinary(parent, {
- left: node
- }) || isAssignmentExpression(parent, {
- left: node
- }))) {
+ if (i > 0 && (hasPostfixPart(node, parent) && parentType !== "NewExpression" || parentType === "SequenceExpression" && parent.expressions[0] === node || parentType === "UpdateExpression" && !parent.prefix || parentType === "ConditionalExpression" && parent.test === node || (parentType === "BinaryExpression" || parentType === "LogicalExpression") && parent.left === node || parentType === "AssignmentExpression" && parent.left === node)) {
node = parent;
i--;
parent = printStack[i];