parentheses.js 7.6 KB

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