You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

246 lines
8.0 KiB

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