UNPKG

5.78 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.AwaitExpression = exports.FunctionTypeAnnotation = undefined;
5exports.NullableTypeAnnotation = NullableTypeAnnotation;
6exports.UpdateExpression = UpdateExpression;
7exports.ObjectExpression = ObjectExpression;
8exports.DoExpression = DoExpression;
9exports.Binary = Binary;
10exports.BinaryExpression = BinaryExpression;
11exports.SequenceExpression = SequenceExpression;
12exports.YieldExpression = YieldExpression;
13exports.ClassExpression = ClassExpression;
14exports.UnaryLike = UnaryLike;
15exports.FunctionExpression = FunctionExpression;
16exports.ArrowFunctionExpression = ArrowFunctionExpression;
17exports.ConditionalExpression = ConditionalExpression;
18exports.AssignmentExpression = AssignmentExpression;
19
20var _babelTypes = require("babel-types");
21
22var t = _interopRequireWildcard(_babelTypes);
23
24function _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
26var 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
53function NullableTypeAnnotation(node, parent) {
54 return t.isArrayTypeAnnotation(parent);
55}
56
57exports.FunctionTypeAnnotation = NullableTypeAnnotation;
58function UpdateExpression(node, parent) {
59 return t.isMemberExpression(parent) && parent.object === node;
60}
61
62function ObjectExpression(node, parent, printStack) {
63 return isFirstInStatement(printStack, { considerArrow: true });
64}
65
66function DoExpression(node, parent, printStack) {
67 return isFirstInStatement(printStack);
68}
69
70function 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
90function BinaryExpression(node, parent) {
91 return node.operator === "in" && (t.isVariableDeclarator(parent) || t.isFor(parent));
92}
93
94function 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
103function 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
107exports.AwaitExpression = YieldExpression;
108function ClassExpression(node, parent, printStack) {
109 return isFirstInStatement(printStack, { considerDefaultExports: true });
110}
111
112function UnaryLike(node, parent) {
113 return t.isMemberExpression(parent, { object: node }) || t.isCallExpression(parent, { callee: node }) || t.isNewExpression(parent, { callee: node });
114}
115
116function FunctionExpression(node, parent, printStack) {
117 return isFirstInStatement(printStack, { considerDefaultExports: true });
118}
119
120function 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
128function 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
136function AssignmentExpression(node) {
137 if (t.isObjectPattern(node.left)) {
138 return true;
139 } else {
140 return ConditionalExpression.apply(undefined, arguments);
141 }
142}
143
144function 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