parentheses.js 9.2 KB

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