parentheses.js 6.0 KB

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