source: trip-planner-front/node_modules/@babel/generator/lib/node/parentheses.js@ eed0bf8

Last change on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 8.5 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.NullableTypeAnnotation = NullableTypeAnnotation;
7exports.FunctionTypeAnnotation = FunctionTypeAnnotation;
8exports.UpdateExpression = UpdateExpression;
9exports.ObjectExpression = ObjectExpression;
10exports.DoExpression = DoExpression;
11exports.Binary = Binary;
12exports.IntersectionTypeAnnotation = exports.UnionTypeAnnotation = UnionTypeAnnotation;
13exports.OptionalIndexedAccessType = OptionalIndexedAccessType;
14exports.TSAsExpression = TSAsExpression;
15exports.TSTypeAssertion = TSTypeAssertion;
16exports.TSIntersectionType = exports.TSUnionType = TSUnionType;
17exports.TSInferType = TSInferType;
18exports.BinaryExpression = BinaryExpression;
19exports.SequenceExpression = SequenceExpression;
20exports.AwaitExpression = exports.YieldExpression = YieldExpression;
21exports.ClassExpression = ClassExpression;
22exports.UnaryLike = UnaryLike;
23exports.FunctionExpression = FunctionExpression;
24exports.ArrowFunctionExpression = ArrowFunctionExpression;
25exports.ConditionalExpression = ConditionalExpression;
26exports.OptionalCallExpression = exports.OptionalMemberExpression = OptionalMemberExpression;
27exports.AssignmentExpression = AssignmentExpression;
28exports.LogicalExpression = LogicalExpression;
29exports.Identifier = Identifier;
30
31var t = require("@babel/types");
32
33const PRECEDENCE = {
34 "||": 0,
35 "??": 0,
36 "&&": 1,
37 "|": 2,
38 "^": 3,
39 "&": 4,
40 "==": 5,
41 "===": 5,
42 "!=": 5,
43 "!==": 5,
44 "<": 6,
45 ">": 6,
46 "<=": 6,
47 ">=": 6,
48 in: 6,
49 instanceof: 6,
50 ">>": 7,
51 "<<": 7,
52 ">>>": 7,
53 "+": 8,
54 "-": 8,
55 "*": 9,
56 "/": 9,
57 "%": 9,
58 "**": 10
59};
60
61const isClassExtendsClause = (node, parent) => (t.isClassDeclaration(parent) || t.isClassExpression(parent)) && parent.superClass === node;
62
63const hasPostfixPart = (node, parent) => (t.isMemberExpression(parent) || t.isOptionalMemberExpression(parent)) && parent.object === node || (t.isCallExpression(parent) || t.isOptionalCallExpression(parent) || t.isNewExpression(parent)) && parent.callee === node || t.isTaggedTemplateExpression(parent) && parent.tag === node || t.isTSNonNullExpression(parent);
64
65function NullableTypeAnnotation(node, parent) {
66 return t.isArrayTypeAnnotation(parent);
67}
68
69function FunctionTypeAnnotation(node, parent, printStack) {
70 return t.isUnionTypeAnnotation(parent) || t.isIntersectionTypeAnnotation(parent) || t.isArrayTypeAnnotation(parent) || t.isTypeAnnotation(parent) && t.isArrowFunctionExpression(printStack[printStack.length - 3]);
71}
72
73function UpdateExpression(node, parent) {
74 return hasPostfixPart(node, parent) || isClassExtendsClause(node, parent);
75}
76
77function ObjectExpression(node, parent, printStack) {
78 return isFirstInContext(printStack, {
79 expressionStatement: true,
80 arrowBody: true
81 });
82}
83
84function DoExpression(node, parent, printStack) {
85 return !node.async && isFirstInContext(printStack, {
86 expressionStatement: true
87 });
88}
89
90function Binary(node, parent) {
91 if (node.operator === "**" && t.isBinaryExpression(parent, {
92 operator: "**"
93 })) {
94 return parent.left === node;
95 }
96
97 if (isClassExtendsClause(node, parent)) {
98 return true;
99 }
100
101 if (hasPostfixPart(node, parent) || t.isUnaryLike(parent) || t.isAwaitExpression(parent)) {
102 return true;
103 }
104
105 if (t.isBinary(parent)) {
106 const parentOp = parent.operator;
107 const parentPos = PRECEDENCE[parentOp];
108 const nodeOp = node.operator;
109 const nodePos = PRECEDENCE[nodeOp];
110
111 if (parentPos === nodePos && parent.right === node && !t.isLogicalExpression(parent) || parentPos > nodePos) {
112 return true;
113 }
114 }
115}
116
117function UnionTypeAnnotation(node, parent) {
118 return t.isArrayTypeAnnotation(parent) || t.isNullableTypeAnnotation(parent) || t.isIntersectionTypeAnnotation(parent) || t.isUnionTypeAnnotation(parent);
119}
120
121function OptionalIndexedAccessType(node, parent) {
122 return t.isIndexedAccessType(parent, {
123 objectType: node
124 });
125}
126
127function TSAsExpression() {
128 return true;
129}
130
131function TSTypeAssertion() {
132 return true;
133}
134
135function TSUnionType(node, parent) {
136 return t.isTSArrayType(parent) || t.isTSOptionalType(parent) || t.isTSIntersectionType(parent) || t.isTSUnionType(parent) || t.isTSRestType(parent);
137}
138
139function TSInferType(node, parent) {
140 return t.isTSArrayType(parent) || t.isTSOptionalType(parent);
141}
142
143function BinaryExpression(node, parent) {
144 return node.operator === "in" && (t.isVariableDeclarator(parent) || t.isFor(parent));
145}
146
147function SequenceExpression(node, parent) {
148 if (t.isForStatement(parent) || t.isThrowStatement(parent) || t.isReturnStatement(parent) || t.isIfStatement(parent) && parent.test === node || t.isWhileStatement(parent) && parent.test === node || t.isForInStatement(parent) && parent.right === node || t.isSwitchStatement(parent) && parent.discriminant === node || t.isExpressionStatement(parent) && parent.expression === node) {
149 return false;
150 }
151
152 return true;
153}
154
155function YieldExpression(node, parent) {
156 return t.isBinary(parent) || t.isUnaryLike(parent) || hasPostfixPart(node, parent) || t.isAwaitExpression(parent) && t.isYieldExpression(node) || t.isConditionalExpression(parent) && node === parent.test || isClassExtendsClause(node, parent);
157}
158
159function ClassExpression(node, parent, printStack) {
160 return isFirstInContext(printStack, {
161 expressionStatement: true,
162 exportDefault: true
163 });
164}
165
166function UnaryLike(node, parent) {
167 return hasPostfixPart(node, parent) || t.isBinaryExpression(parent, {
168 operator: "**",
169 left: node
170 }) || isClassExtendsClause(node, parent);
171}
172
173function FunctionExpression(node, parent, printStack) {
174 return isFirstInContext(printStack, {
175 expressionStatement: true,
176 exportDefault: true
177 });
178}
179
180function ArrowFunctionExpression(node, parent) {
181 return t.isExportDeclaration(parent) || ConditionalExpression(node, parent);
182}
183
184function ConditionalExpression(node, parent) {
185 if (t.isUnaryLike(parent) || t.isBinary(parent) || t.isConditionalExpression(parent, {
186 test: node
187 }) || t.isAwaitExpression(parent) || t.isTSTypeAssertion(parent) || t.isTSAsExpression(parent)) {
188 return true;
189 }
190
191 return UnaryLike(node, parent);
192}
193
194function OptionalMemberExpression(node, parent) {
195 return t.isCallExpression(parent, {
196 callee: node
197 }) || t.isMemberExpression(parent, {
198 object: node
199 });
200}
201
202function AssignmentExpression(node, parent) {
203 if (t.isObjectPattern(node.left)) {
204 return true;
205 } else {
206 return ConditionalExpression(node, parent);
207 }
208}
209
210function LogicalExpression(node, parent) {
211 switch (node.operator) {
212 case "||":
213 if (!t.isLogicalExpression(parent)) return false;
214 return parent.operator === "??" || parent.operator === "&&";
215
216 case "&&":
217 return t.isLogicalExpression(parent, {
218 operator: "??"
219 });
220
221 case "??":
222 return t.isLogicalExpression(parent) && parent.operator !== "??";
223 }
224}
225
226function Identifier(node, parent, printStack) {
227 if (node.name === "let") {
228 const isFollowedByBracket = t.isMemberExpression(parent, {
229 object: node,
230 computed: true
231 }) || t.isOptionalMemberExpression(parent, {
232 object: node,
233 computed: true,
234 optional: false
235 });
236 return isFirstInContext(printStack, {
237 expressionStatement: isFollowedByBracket,
238 forHead: isFollowedByBracket,
239 forInHead: isFollowedByBracket,
240 forOfHead: true
241 });
242 }
243
244 return node.name === "async" && t.isForOfStatement(parent) && node === parent.left;
245}
246
247function isFirstInContext(printStack, {
248 expressionStatement = false,
249 arrowBody = false,
250 exportDefault = false,
251 forHead = false,
252 forInHead = false,
253 forOfHead = false
254}) {
255 let i = printStack.length - 1;
256 let node = printStack[i];
257 i--;
258 let parent = printStack[i];
259
260 while (i >= 0) {
261 if (expressionStatement && t.isExpressionStatement(parent, {
262 expression: node
263 }) || exportDefault && t.isExportDefaultDeclaration(parent, {
264 declaration: node
265 }) || arrowBody && t.isArrowFunctionExpression(parent, {
266 body: node
267 }) || forHead && t.isForStatement(parent, {
268 init: node
269 }) || forInHead && t.isForInStatement(parent, {
270 left: node
271 }) || forOfHead && t.isForOfStatement(parent, {
272 left: node
273 })) {
274 return true;
275 }
276
277 if (hasPostfixPart(node, parent) && !t.isNewExpression(parent) || t.isSequenceExpression(parent) && parent.expressions[0] === node || t.isConditional(parent, {
278 test: node
279 }) || t.isBinary(parent, {
280 left: node
281 }) || t.isAssignmentExpression(parent, {
282 left: node
283 })) {
284 node = parent;
285 i--;
286 parent = printStack[i];
287 } else {
288 return false;
289 }
290 }
291
292 return false;
293}
Note: See TracBrowser for help on using the repository browser.