parentheses.js 5.6 KB

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