parentheses.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. var t = _interopRequireWildcard(require("@babel/types"));
  27. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  28. 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; }
  29. const PRECEDENCE = {
  30. "||": 0,
  31. "??": 0,
  32. "&&": 1,
  33. "|": 2,
  34. "^": 3,
  35. "&": 4,
  36. "==": 5,
  37. "===": 5,
  38. "!=": 5,
  39. "!==": 5,
  40. "<": 6,
  41. ">": 6,
  42. "<=": 6,
  43. ">=": 6,
  44. in: 6,
  45. instanceof: 6,
  46. ">>": 7,
  47. "<<": 7,
  48. ">>>": 7,
  49. "+": 8,
  50. "-": 8,
  51. "*": 9,
  52. "/": 9,
  53. "%": 9,
  54. "**": 10
  55. };
  56. const isClassExtendsClause = (node, parent) => (t.isClassDeclaration(parent) || t.isClassExpression(parent)) && parent.superClass === node;
  57. function NullableTypeAnnotation(node, parent) {
  58. return t.isArrayTypeAnnotation(parent);
  59. }
  60. function FunctionTypeAnnotation(node, parent, printStack) {
  61. return t.isUnionTypeAnnotation(parent) || t.isIntersectionTypeAnnotation(parent) || t.isArrayTypeAnnotation(parent) || t.isTypeAnnotation(parent) && t.isArrowFunctionExpression(printStack[printStack.length - 3]);
  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. const parentOp = parent.operator;
  94. const parentPos = PRECEDENCE[parentOp];
  95. const nodeOp = node.operator;
  96. const 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 TSUnionType(node, parent) {
  113. return t.isTSArrayType(parent) || t.isTSOptionalType(parent) || t.isTSIntersectionType(parent) || t.isTSUnionType(parent) || t.isTSRestType(parent);
  114. }
  115. function BinaryExpression(node, parent) {
  116. return node.operator === "in" && (t.isVariableDeclarator(parent) || t.isFor(parent));
  117. }
  118. function SequenceExpression(node, parent) {
  119. 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) {
  120. return false;
  121. }
  122. return true;
  123. }
  124. function YieldExpression(node, parent) {
  125. 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);
  126. }
  127. function ClassExpression(node, parent, printStack) {
  128. return isFirstInStatement(printStack, {
  129. considerDefaultExports: true
  130. });
  131. }
  132. function UnaryLike(node, parent) {
  133. return t.isMemberExpression(parent, {
  134. object: node
  135. }) || t.isCallExpression(parent, {
  136. callee: node
  137. }) || t.isNewExpression(parent, {
  138. callee: node
  139. }) || t.isBinaryExpression(parent, {
  140. operator: "**",
  141. left: node
  142. }) || isClassExtendsClause(node, parent);
  143. }
  144. function FunctionExpression(node, parent, printStack) {
  145. return isFirstInStatement(printStack, {
  146. considerDefaultExports: true
  147. });
  148. }
  149. function ArrowFunctionExpression(node, parent) {
  150. return t.isExportDeclaration(parent) || ConditionalExpression(node, parent);
  151. }
  152. function ConditionalExpression(node, parent) {
  153. if (t.isUnaryLike(parent) || t.isBinary(parent) || t.isConditionalExpression(parent, {
  154. test: node
  155. }) || t.isAwaitExpression(parent) || t.isOptionalMemberExpression(parent) || t.isTaggedTemplateExpression(parent) || t.isTSTypeAssertion(parent) || t.isTSAsExpression(parent)) {
  156. return true;
  157. }
  158. return UnaryLike(node, parent);
  159. }
  160. function OptionalMemberExpression(node, parent) {
  161. return t.isCallExpression(parent) || t.isMemberExpression(parent);
  162. }
  163. function AssignmentExpression(node) {
  164. if (t.isObjectPattern(node.left)) {
  165. return true;
  166. } else {
  167. return ConditionalExpression(...arguments);
  168. }
  169. }
  170. function NewExpression(node, parent) {
  171. return isClassExtendsClause(node, parent);
  172. }
  173. function isFirstInStatement(printStack, {
  174. considerArrow = false,
  175. considerDefaultExports = false
  176. } = {}) {
  177. let i = printStack.length - 1;
  178. let node = printStack[i];
  179. i--;
  180. let parent = printStack[i];
  181. while (i > 0) {
  182. if (t.isExpressionStatement(parent, {
  183. expression: node
  184. }) || t.isTaggedTemplateExpression(parent) || considerDefaultExports && t.isExportDefaultDeclaration(parent, {
  185. declaration: node
  186. }) || considerArrow && t.isArrowFunctionExpression(parent, {
  187. body: node
  188. })) {
  189. return true;
  190. }
  191. if (t.isCallExpression(parent, {
  192. callee: node
  193. }) || t.isSequenceExpression(parent) && parent.expressions[0] === node || t.isMemberExpression(parent, {
  194. object: node
  195. }) || t.isConditional(parent, {
  196. test: node
  197. }) || t.isBinary(parent, {
  198. left: node
  199. }) || t.isAssignmentExpression(parent, {
  200. left: node
  201. })) {
  202. node = parent;
  203. i--;
  204. parent = printStack[i];
  205. } else {
  206. return false;
  207. }
  208. }
  209. return false;
  210. }