parentheses.js 7.2 KB

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