UNPKG

10.8 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.ArrowFunctionExpression = ArrowFunctionExpression;
7exports.AssignmentExpression = AssignmentExpression;
8exports.Binary = Binary;
9exports.BinaryExpression = BinaryExpression;
10exports.ClassExpression = ClassExpression;
11exports.ConditionalExpression = ConditionalExpression;
12exports.DoExpression = DoExpression;
13exports.FunctionExpression = FunctionExpression;
14exports.FunctionTypeAnnotation = FunctionTypeAnnotation;
15exports.Identifier = Identifier;
16exports.LogicalExpression = LogicalExpression;
17exports.NullableTypeAnnotation = NullableTypeAnnotation;
18exports.ObjectExpression = ObjectExpression;
19exports.OptionalIndexedAccessType = OptionalIndexedAccessType;
20exports.OptionalCallExpression = exports.OptionalMemberExpression = OptionalMemberExpression;
21exports.SequenceExpression = SequenceExpression;
22exports.TSTypeAssertion = exports.TSSatisfiesExpression = exports.TSAsExpression = TSAsExpression;
23exports.TSInferType = TSInferType;
24exports.TSInstantiationExpression = TSInstantiationExpression;
25exports.TSIntersectionType = exports.TSUnionType = TSUnionType;
26exports.UnaryLike = UnaryLike;
27exports.IntersectionTypeAnnotation = exports.UnionTypeAnnotation = UnionTypeAnnotation;
28exports.UpdateExpression = UpdateExpression;
29exports.AwaitExpression = exports.YieldExpression = YieldExpression;
30var _t = require("@babel/types");
31const {
32 isArrayTypeAnnotation,
33 isArrowFunctionExpression,
34 isBinaryExpression,
35 isCallExpression,
36 isExportDeclaration,
37 isForOfStatement,
38 isIndexedAccessType,
39 isMemberExpression,
40 isObjectPattern,
41 isOptionalMemberExpression,
42 isYieldExpression
43} = _t;
44const 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]]);
45function isTSTypeExpression(nodeType) {
46 return nodeType === "TSAsExpression" || nodeType === "TSSatisfiesExpression" || nodeType === "TSTypeAssertion";
47}
48const isClassExtendsClause = (node, parent) => {
49 const parentType = parent.type;
50 return (parentType === "ClassDeclaration" || parentType === "ClassExpression") && parent.superClass === node;
51};
52const hasPostfixPart = (node, parent) => {
53 const parentType = parent.type;
54 return (parentType === "MemberExpression" || parentType === "OptionalMemberExpression") && parent.object === node || (parentType === "CallExpression" || parentType === "OptionalCallExpression" || parentType === "NewExpression") && parent.callee === node || parentType === "TaggedTemplateExpression" && parent.tag === node || parentType === "TSNonNullExpression";
55};
56function NullableTypeAnnotation(node, parent) {
57 return isArrayTypeAnnotation(parent);
58}
59function FunctionTypeAnnotation(node, parent, printStack) {
60 if (printStack.length < 3) return;
61 const parentType = parent.type;
62 return parentType === "UnionTypeAnnotation" || parentType === "IntersectionTypeAnnotation" || parentType === "ArrayTypeAnnotation" || parentType === "TypeAnnotation" && isArrowFunctionExpression(printStack[printStack.length - 3]);
63}
64function UpdateExpression(node, parent) {
65 return hasPostfixPart(node, parent) || isClassExtendsClause(node, parent);
66}
67function ObjectExpression(node, parent, printStack) {
68 return isFirstInContext(printStack, 1 | 2);
69}
70function DoExpression(node, parent, printStack) {
71 return !node.async && isFirstInContext(printStack, 1);
72}
73function Binary(node, parent) {
74 const parentType = parent.type;
75 if (node.operator === "**" && parentType === "BinaryExpression" && parent.operator === "**") {
76 return parent.left === node;
77 }
78 if (isClassExtendsClause(node, parent)) {
79 return true;
80 }
81 if (hasPostfixPart(node, parent) || parentType === "UnaryExpression" || parentType === "SpreadElement" || parentType === "AwaitExpression") {
82 return true;
83 }
84 if (parentType === "BinaryExpression" || parentType === "LogicalExpression") {
85 const parentPos = PRECEDENCE.get(parent.operator);
86 const nodePos = PRECEDENCE.get(node.operator);
87 if (parentPos === nodePos && parent.right === node && parentType !== "LogicalExpression" || parentPos > nodePos) {
88 return true;
89 }
90 }
91 return undefined;
92}
93function UnionTypeAnnotation(node, parent) {
94 const parentType = parent.type;
95 return parentType === "ArrayTypeAnnotation" || parentType === "NullableTypeAnnotation" || parentType === "IntersectionTypeAnnotation" || parentType === "UnionTypeAnnotation";
96}
97function OptionalIndexedAccessType(node, parent) {
98 return isIndexedAccessType(parent) && parent.objectType === node;
99}
100function TSAsExpression() {
101 return true;
102}
103function TSUnionType(node, parent) {
104 const parentType = parent.type;
105 return parentType === "TSArrayType" || parentType === "TSOptionalType" || parentType === "TSIntersectionType" || parentType === "TSUnionType" || parentType === "TSRestType";
106}
107function TSInferType(node, parent) {
108 const parentType = parent.type;
109 return parentType === "TSArrayType" || parentType === "TSOptionalType";
110}
111function TSInstantiationExpression(node, parent) {
112 const parentType = parent.type;
113 return (parentType === "CallExpression" || parentType === "OptionalCallExpression" || parentType === "NewExpression" || parentType === "TSInstantiationExpression") && !!parent.typeParameters;
114}
115function BinaryExpression(node, parent) {
116 if (node.operator === "in") {
117 const parentType = parent.type;
118 return parentType === "VariableDeclarator" || parentType === "ForStatement" || parentType === "ForInStatement" || parentType === "ForOfStatement";
119 }
120 return false;
121}
122function SequenceExpression(node, parent) {
123 const parentType = parent.type;
124 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) {
125 return false;
126 }
127 return true;
128}
129function YieldExpression(node, parent) {
130 const parentType = parent.type;
131 return parentType === "BinaryExpression" || parentType === "LogicalExpression" || parentType === "UnaryExpression" || parentType === "SpreadElement" || hasPostfixPart(node, parent) || parentType === "AwaitExpression" && isYieldExpression(node) || parentType === "ConditionalExpression" && node === parent.test || isClassExtendsClause(node, parent);
132}
133function ClassExpression(node, parent, printStack) {
134 return isFirstInContext(printStack, 1 | 4);
135}
136function UnaryLike(node, parent) {
137 return hasPostfixPart(node, parent) || isBinaryExpression(parent) && parent.operator === "**" && parent.left === node || isClassExtendsClause(node, parent);
138}
139function FunctionExpression(node, parent, printStack) {
140 return isFirstInContext(printStack, 1 | 4);
141}
142function ArrowFunctionExpression(node, parent) {
143 return isExportDeclaration(parent) || ConditionalExpression(node, parent);
144}
145function ConditionalExpression(node, parent) {
146 const parentType = parent.type;
147 if (parentType === "UnaryExpression" || parentType === "SpreadElement" || parentType === "BinaryExpression" || parentType === "LogicalExpression" || parentType === "ConditionalExpression" && parent.test === node || parentType === "AwaitExpression" || isTSTypeExpression(parentType)) {
148 return true;
149 }
150 return UnaryLike(node, parent);
151}
152function OptionalMemberExpression(node, parent) {
153 return isCallExpression(parent) && parent.callee === node || isMemberExpression(parent) && parent.object === node;
154}
155function AssignmentExpression(node, parent) {
156 if (isObjectPattern(node.left)) {
157 return true;
158 } else {
159 return ConditionalExpression(node, parent);
160 }
161}
162function LogicalExpression(node, parent) {
163 const parentType = parent.type;
164 if (isTSTypeExpression(parentType)) return true;
165 if (parentType !== "LogicalExpression") return false;
166 switch (node.operator) {
167 case "||":
168 return parent.operator === "??" || parent.operator === "&&";
169 case "&&":
170 return parent.operator === "??";
171 case "??":
172 return parent.operator !== "??";
173 }
174}
175function Identifier(node, parent, printStack) {
176 var _node$extra;
177 const parentType = parent.type;
178 if ((_node$extra = node.extra) != null && _node$extra.parenthesized && parentType === "AssignmentExpression" && parent.left === node) {
179 const rightType = parent.right.type;
180 if ((rightType === "FunctionExpression" || rightType === "ClassExpression") && parent.right.id == null) {
181 return true;
182 }
183 }
184 if (node.name === "let") {
185 const isFollowedByBracket = isMemberExpression(parent, {
186 object: node,
187 computed: true
188 }) || isOptionalMemberExpression(parent, {
189 object: node,
190 computed: true,
191 optional: false
192 });
193 return isFirstInContext(printStack, isFollowedByBracket ? 1 | 8 | 16 | 32 : 32);
194 }
195 return node.name === "async" && isForOfStatement(parent) && node === parent.left;
196}
197function isFirstInContext(printStack, checkParam) {
198 const expressionStatement = checkParam & 1;
199 const arrowBody = checkParam & 2;
200 const exportDefault = checkParam & 4;
201 const forHead = checkParam & 8;
202 const forInHead = checkParam & 16;
203 const forOfHead = checkParam & 32;
204 let i = printStack.length - 1;
205 if (i <= 0) return;
206 let node = printStack[i];
207 i--;
208 let parent = printStack[i];
209 while (i >= 0) {
210 const parentType = parent.type;
211 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) {
212 return true;
213 }
214 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)) {
215 node = parent;
216 i--;
217 parent = printStack[i];
218 } else {
219 return false;
220 }
221 }
222 return false;
223}
224
225//# sourceMappingURL=parentheses.js.map