1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.NullableTypeAnnotation = NullableTypeAnnotation;
|
---|
7 | exports.FunctionTypeAnnotation = FunctionTypeAnnotation;
|
---|
8 | exports.UpdateExpression = UpdateExpression;
|
---|
9 | exports.ObjectExpression = ObjectExpression;
|
---|
10 | exports.DoExpression = DoExpression;
|
---|
11 | exports.Binary = Binary;
|
---|
12 | exports.IntersectionTypeAnnotation = exports.UnionTypeAnnotation = UnionTypeAnnotation;
|
---|
13 | exports.OptionalIndexedAccessType = OptionalIndexedAccessType;
|
---|
14 | exports.TSAsExpression = TSAsExpression;
|
---|
15 | exports.TSTypeAssertion = TSTypeAssertion;
|
---|
16 | exports.TSIntersectionType = exports.TSUnionType = TSUnionType;
|
---|
17 | exports.TSInferType = TSInferType;
|
---|
18 | exports.BinaryExpression = BinaryExpression;
|
---|
19 | exports.SequenceExpression = SequenceExpression;
|
---|
20 | exports.AwaitExpression = exports.YieldExpression = YieldExpression;
|
---|
21 | exports.ClassExpression = ClassExpression;
|
---|
22 | exports.UnaryLike = UnaryLike;
|
---|
23 | exports.FunctionExpression = FunctionExpression;
|
---|
24 | exports.ArrowFunctionExpression = ArrowFunctionExpression;
|
---|
25 | exports.ConditionalExpression = ConditionalExpression;
|
---|
26 | exports.OptionalCallExpression = exports.OptionalMemberExpression = OptionalMemberExpression;
|
---|
27 | exports.AssignmentExpression = AssignmentExpression;
|
---|
28 | exports.LogicalExpression = LogicalExpression;
|
---|
29 | exports.Identifier = Identifier;
|
---|
30 |
|
---|
31 | var t = require("@babel/types");
|
---|
32 |
|
---|
33 | const 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 |
|
---|
61 | const isClassExtendsClause = (node, parent) => (t.isClassDeclaration(parent) || t.isClassExpression(parent)) && parent.superClass === node;
|
---|
62 |
|
---|
63 | const 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 |
|
---|
65 | function NullableTypeAnnotation(node, parent) {
|
---|
66 | return t.isArrayTypeAnnotation(parent);
|
---|
67 | }
|
---|
68 |
|
---|
69 | function 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 |
|
---|
73 | function UpdateExpression(node, parent) {
|
---|
74 | return hasPostfixPart(node, parent) || isClassExtendsClause(node, parent);
|
---|
75 | }
|
---|
76 |
|
---|
77 | function ObjectExpression(node, parent, printStack) {
|
---|
78 | return isFirstInContext(printStack, {
|
---|
79 | expressionStatement: true,
|
---|
80 | arrowBody: true
|
---|
81 | });
|
---|
82 | }
|
---|
83 |
|
---|
84 | function DoExpression(node, parent, printStack) {
|
---|
85 | return !node.async && isFirstInContext(printStack, {
|
---|
86 | expressionStatement: true
|
---|
87 | });
|
---|
88 | }
|
---|
89 |
|
---|
90 | function 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 |
|
---|
117 | function UnionTypeAnnotation(node, parent) {
|
---|
118 | return t.isArrayTypeAnnotation(parent) || t.isNullableTypeAnnotation(parent) || t.isIntersectionTypeAnnotation(parent) || t.isUnionTypeAnnotation(parent);
|
---|
119 | }
|
---|
120 |
|
---|
121 | function OptionalIndexedAccessType(node, parent) {
|
---|
122 | return t.isIndexedAccessType(parent, {
|
---|
123 | objectType: node
|
---|
124 | });
|
---|
125 | }
|
---|
126 |
|
---|
127 | function TSAsExpression() {
|
---|
128 | return true;
|
---|
129 | }
|
---|
130 |
|
---|
131 | function TSTypeAssertion() {
|
---|
132 | return true;
|
---|
133 | }
|
---|
134 |
|
---|
135 | function TSUnionType(node, parent) {
|
---|
136 | return t.isTSArrayType(parent) || t.isTSOptionalType(parent) || t.isTSIntersectionType(parent) || t.isTSUnionType(parent) || t.isTSRestType(parent);
|
---|
137 | }
|
---|
138 |
|
---|
139 | function TSInferType(node, parent) {
|
---|
140 | return t.isTSArrayType(parent) || t.isTSOptionalType(parent);
|
---|
141 | }
|
---|
142 |
|
---|
143 | function BinaryExpression(node, parent) {
|
---|
144 | return node.operator === "in" && (t.isVariableDeclarator(parent) || t.isFor(parent));
|
---|
145 | }
|
---|
146 |
|
---|
147 | function 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 |
|
---|
155 | function 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 |
|
---|
159 | function ClassExpression(node, parent, printStack) {
|
---|
160 | return isFirstInContext(printStack, {
|
---|
161 | expressionStatement: true,
|
---|
162 | exportDefault: true
|
---|
163 | });
|
---|
164 | }
|
---|
165 |
|
---|
166 | function UnaryLike(node, parent) {
|
---|
167 | return hasPostfixPart(node, parent) || t.isBinaryExpression(parent, {
|
---|
168 | operator: "**",
|
---|
169 | left: node
|
---|
170 | }) || isClassExtendsClause(node, parent);
|
---|
171 | }
|
---|
172 |
|
---|
173 | function FunctionExpression(node, parent, printStack) {
|
---|
174 | return isFirstInContext(printStack, {
|
---|
175 | expressionStatement: true,
|
---|
176 | exportDefault: true
|
---|
177 | });
|
---|
178 | }
|
---|
179 |
|
---|
180 | function ArrowFunctionExpression(node, parent) {
|
---|
181 | return t.isExportDeclaration(parent) || ConditionalExpression(node, parent);
|
---|
182 | }
|
---|
183 |
|
---|
184 | function 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 |
|
---|
194 | function OptionalMemberExpression(node, parent) {
|
---|
195 | return t.isCallExpression(parent, {
|
---|
196 | callee: node
|
---|
197 | }) || t.isMemberExpression(parent, {
|
---|
198 | object: node
|
---|
199 | });
|
---|
200 | }
|
---|
201 |
|
---|
202 | function AssignmentExpression(node, parent) {
|
---|
203 | if (t.isObjectPattern(node.left)) {
|
---|
204 | return true;
|
---|
205 | } else {
|
---|
206 | return ConditionalExpression(node, parent);
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | function 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 |
|
---|
226 | function 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 |
|
---|
247 | function 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 | } |
---|