1 | "use strict";
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports.AwaitExpression = exports.FunctionTypeAnnotation = undefined;
|
5 | exports.NullableTypeAnnotation = NullableTypeAnnotation;
|
6 | exports.UpdateExpression = UpdateExpression;
|
7 | exports.ObjectExpression = ObjectExpression;
|
8 | exports.DoExpression = DoExpression;
|
9 | exports.Binary = Binary;
|
10 | exports.BinaryExpression = BinaryExpression;
|
11 | exports.SequenceExpression = SequenceExpression;
|
12 | exports.YieldExpression = YieldExpression;
|
13 | exports.ClassExpression = ClassExpression;
|
14 | exports.UnaryLike = UnaryLike;
|
15 | exports.FunctionExpression = FunctionExpression;
|
16 | exports.ArrowFunctionExpression = ArrowFunctionExpression;
|
17 | exports.ConditionalExpression = ConditionalExpression;
|
18 | exports.AssignmentExpression = AssignmentExpression;
|
19 |
|
20 | var _babelTypes = require("babel-types");
|
21 |
|
22 | var t = _interopRequireWildcard(_babelTypes);
|
23 |
|
24 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
|
25 |
|
26 | var PRECEDENCE = {
|
27 | "||": 0,
|
28 | "&&": 1,
|
29 | "|": 2,
|
30 | "^": 3,
|
31 | "&": 4,
|
32 | "==": 5,
|
33 | "===": 5,
|
34 | "!=": 5,
|
35 | "!==": 5,
|
36 | "<": 6,
|
37 | ">": 6,
|
38 | "<=": 6,
|
39 | ">=": 6,
|
40 | in: 6,
|
41 | instanceof: 6,
|
42 | ">>": 7,
|
43 | "<<": 7,
|
44 | ">>>": 7,
|
45 | "+": 8,
|
46 | "-": 8,
|
47 | "*": 9,
|
48 | "/": 9,
|
49 | "%": 9,
|
50 | "**": 10
|
51 | };
|
52 |
|
53 | function NullableTypeAnnotation(node, parent) {
|
54 | return t.isArrayTypeAnnotation(parent);
|
55 | }
|
56 |
|
57 | exports.FunctionTypeAnnotation = NullableTypeAnnotation;
|
58 | function UpdateExpression(node, parent) {
|
59 | return t.isMemberExpression(parent) && parent.object === node;
|
60 | }
|
61 |
|
62 | function ObjectExpression(node, parent, printStack) {
|
63 | return isFirstInStatement(printStack, { considerArrow: true });
|
64 | }
|
65 |
|
66 | function DoExpression(node, parent, printStack) {
|
67 | return isFirstInStatement(printStack);
|
68 | }
|
69 |
|
70 | function Binary(node, parent) {
|
71 | if ((t.isCallExpression(parent) || t.isNewExpression(parent)) && parent.callee === node || t.isUnaryLike(parent) || t.isMemberExpression(parent) && parent.object === node || t.isAwaitExpression(parent)) {
|
72 | return true;
|
73 | }
|
74 |
|
75 | if (t.isBinary(parent)) {
|
76 | var parentOp = parent.operator;
|
77 | var parentPos = PRECEDENCE[parentOp];
|
78 |
|
79 | var nodeOp = node.operator;
|
80 | var nodePos = PRECEDENCE[nodeOp];
|
81 |
|
82 | if (parentPos === nodePos && parent.right === node && !t.isLogicalExpression(parent) || parentPos > nodePos) {
|
83 | return true;
|
84 | }
|
85 | }
|
86 |
|
87 | return false;
|
88 | }
|
89 |
|
90 | function BinaryExpression(node, parent) {
|
91 | return node.operator === "in" && (t.isVariableDeclarator(parent) || t.isFor(parent));
|
92 | }
|
93 |
|
94 | function SequenceExpression(node, parent) {
|
95 |
|
96 | 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) {
|
97 | return false;
|
98 | }
|
99 |
|
100 | return true;
|
101 | }
|
102 |
|
103 | function YieldExpression(node, parent) {
|
104 | return t.isBinary(parent) || t.isUnaryLike(parent) || t.isCallExpression(parent) || t.isMemberExpression(parent) || t.isNewExpression(parent) || t.isConditionalExpression(parent) && node === parent.test;
|
105 | }
|
106 |
|
107 | exports.AwaitExpression = YieldExpression;
|
108 | function ClassExpression(node, parent, printStack) {
|
109 | return isFirstInStatement(printStack, { considerDefaultExports: true });
|
110 | }
|
111 |
|
112 | function UnaryLike(node, parent) {
|
113 | return t.isMemberExpression(parent, { object: node }) || t.isCallExpression(parent, { callee: node }) || t.isNewExpression(parent, { callee: node });
|
114 | }
|
115 |
|
116 | function FunctionExpression(node, parent, printStack) {
|
117 | return isFirstInStatement(printStack, { considerDefaultExports: true });
|
118 | }
|
119 |
|
120 | function ArrowFunctionExpression(node, parent) {
|
121 | if (t.isExportDeclaration(parent) || t.isBinaryExpression(parent) || t.isLogicalExpression(parent) || t.isUnaryExpression(parent) || t.isTaggedTemplateExpression(parent)) {
|
122 | return true;
|
123 | }
|
124 |
|
125 | return UnaryLike(node, parent);
|
126 | }
|
127 |
|
128 | function ConditionalExpression(node, parent) {
|
129 | if (t.isUnaryLike(parent) || t.isBinary(parent) || t.isConditionalExpression(parent, { test: node }) || t.isAwaitExpression(parent)) {
|
130 | return true;
|
131 | }
|
132 |
|
133 | return UnaryLike(node, parent);
|
134 | }
|
135 |
|
136 | function AssignmentExpression(node) {
|
137 | if (t.isObjectPattern(node.left)) {
|
138 | return true;
|
139 | } else {
|
140 | return ConditionalExpression.apply(undefined, arguments);
|
141 | }
|
142 | }
|
143 |
|
144 | function isFirstInStatement(printStack) {
|
145 | var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
146 | _ref$considerArrow = _ref.considerArrow,
|
147 | considerArrow = _ref$considerArrow === undefined ? false : _ref$considerArrow,
|
148 | _ref$considerDefaultE = _ref.considerDefaultExports,
|
149 | considerDefaultExports = _ref$considerDefaultE === undefined ? false : _ref$considerDefaultE;
|
150 |
|
151 | var i = printStack.length - 1;
|
152 | var node = printStack[i];
|
153 | i--;
|
154 | var parent = printStack[i];
|
155 | while (i > 0) {
|
156 | if (t.isExpressionStatement(parent, { expression: node }) || t.isTaggedTemplateExpression(parent) || considerDefaultExports && t.isExportDefaultDeclaration(parent, { declaration: node }) || considerArrow && t.isArrowFunctionExpression(parent, { body: node })) {
|
157 | return true;
|
158 | }
|
159 |
|
160 | if (t.isCallExpression(parent, { callee: node }) || t.isSequenceExpression(parent) && parent.expressions[0] === node || t.isMemberExpression(parent, { object: node }) || t.isConditional(parent, { test: node }) || t.isBinary(parent, { left: node }) || t.isAssignmentExpression(parent, { left: node })) {
|
161 | node = parent;
|
162 | i--;
|
163 | parent = printStack[i];
|
164 | } else {
|
165 | return false;
|
166 | }
|
167 | }
|
168 |
|
169 | return false;
|
170 | } |
\ | No newline at end of file |