parentheses.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.ArrowFunctionExpression = ArrowFunctionExpression;
  6. exports.AssignmentExpression = AssignmentExpression;
  7. exports.Binary = Binary;
  8. exports.BinaryExpression = BinaryExpression;
  9. exports.ClassExpression = ClassExpression;
  10. exports.ConditionalExpression = ConditionalExpression;
  11. exports.DoExpression = DoExpression;
  12. exports.FunctionExpression = FunctionExpression;
  13. exports.FunctionTypeAnnotation = FunctionTypeAnnotation;
  14. exports.Identifier = Identifier;
  15. exports.LogicalExpression = LogicalExpression;
  16. exports.NullableTypeAnnotation = NullableTypeAnnotation;
  17. exports.ObjectExpression = ObjectExpression;
  18. exports.OptionalIndexedAccessType = OptionalIndexedAccessType;
  19. exports.OptionalCallExpression = exports.OptionalMemberExpression = OptionalMemberExpression;
  20. exports.SequenceExpression = SequenceExpression;
  21. exports.TSTypeAssertion = exports.TSSatisfiesExpression = exports.TSAsExpression = TSAsExpression;
  22. exports.TSInferType = TSInferType;
  23. exports.TSInstantiationExpression = TSInstantiationExpression;
  24. exports.TSIntersectionType = exports.TSUnionType = TSUnionType;
  25. exports.UnaryLike = UnaryLike;
  26. exports.IntersectionTypeAnnotation = exports.UnionTypeAnnotation = UnionTypeAnnotation;
  27. exports.UpdateExpression = UpdateExpression;
  28. exports.AwaitExpression = exports.YieldExpression = YieldExpression;
  29. var _t = require("@babel/types");
  30. const {
  31. isArrayTypeAnnotation,
  32. isArrowFunctionExpression,
  33. isBinaryExpression,
  34. isCallExpression,
  35. isExportDeclaration,
  36. isForOfStatement,
  37. isIndexedAccessType,
  38. isMemberExpression,
  39. isObjectPattern,
  40. isOptionalMemberExpression,
  41. isYieldExpression
  42. } = _t;
  43. const PRECEDENCE = new Map([["||", 0], ["??", 0], ["|>", 0], ["&&", 1], ["|", 2], ["^", 3], ["&", 4], ["==", 5], ["===", 5], ["!=", 5], ["!==", 5], ["<", 6], [">", 6], ["<=", 6], [">=", 6], ["in", 6], ["instanceof", 6], [">>", 7], ["<<", 7], [">>>", 7], ["+", 8], ["-", 8], ["*", 9], ["/", 9], ["%", 9], ["**", 10]]);
  44. function isTSTypeExpression(nodeType) {
  45. return nodeType === "TSAsExpression" || nodeType === "TSSatisfiesExpression" || nodeType === "TSTypeAssertion";
  46. }
  47. const isClassExtendsClause = (node, parent) => {
  48. const parentType = parent.type;
  49. return (parentType === "ClassDeclaration" || parentType === "ClassExpression") && parent.superClass === node;
  50. };
  51. const hasPostfixPart = (node, parent) => {
  52. const parentType = parent.type;
  53. return (parentType === "MemberExpression" || parentType === "OptionalMemberExpression") && parent.object === node || (parentType === "CallExpression" || parentType === "OptionalCallExpression" || parentType === "NewExpression") && parent.callee === node || parentType === "TaggedTemplateExpression" && parent.tag === node || parentType === "TSNonNullExpression";
  54. };
  55. function NullableTypeAnnotation(node, parent) {
  56. return isArrayTypeAnnotation(parent);
  57. }
  58. function FunctionTypeAnnotation(node, parent, printStack) {
  59. if (printStack.length < 3) return;
  60. const parentType = parent.type;
  61. return parentType === "UnionTypeAnnotation" || parentType === "IntersectionTypeAnnotation" || parentType === "ArrayTypeAnnotation" || parentType === "TypeAnnotation" && isArrowFunctionExpression(printStack[printStack.length - 3]);
  62. }
  63. function UpdateExpression(node, parent) {
  64. return hasPostfixPart(node, parent) || isClassExtendsClause(node, parent);
  65. }
  66. function ObjectExpression(node, parent, printStack) {
  67. return isFirstInContext(printStack, 1 | 2);
  68. }
  69. function DoExpression(node, parent, printStack) {
  70. return !node.async && isFirstInContext(printStack, 1);
  71. }
  72. function Binary(node, parent) {
  73. const parentType = parent.type;
  74. if (node.operator === "**" && parentType === "BinaryExpression" && parent.operator === "**") {
  75. return parent.left === node;
  76. }
  77. if (isClassExtendsClause(node, parent)) {
  78. return true;
  79. }
  80. if (hasPostfixPart(node, parent) || parentType === "UnaryExpression" || parentType === "SpreadElement" || parentType === "AwaitExpression") {
  81. return true;
  82. }
  83. if (parentType === "BinaryExpression" || parentType === "LogicalExpression") {
  84. const parentPos = PRECEDENCE.get(parent.operator);
  85. const nodePos = PRECEDENCE.get(node.operator);
  86. if (parentPos === nodePos && parent.right === node && parentType !== "LogicalExpression" || parentPos > nodePos) {
  87. return true;
  88. }
  89. }
  90. return undefined;
  91. }
  92. function UnionTypeAnnotation(node, parent) {
  93. const parentType = parent.type;
  94. return parentType === "ArrayTypeAnnotation" || parentType === "NullableTypeAnnotation" || parentType === "IntersectionTypeAnnotation" || parentType === "UnionTypeAnnotation";
  95. }
  96. function OptionalIndexedAccessType(node, parent) {
  97. return isIndexedAccessType(parent) && parent.objectType === node;
  98. }
  99. function TSAsExpression() {
  100. return true;
  101. }
  102. function TSUnionType(node, parent) {
  103. const parentType = parent.type;
  104. return parentType === "TSArrayType" || parentType === "TSOptionalType" || parentType === "TSIntersectionType" || parentType === "TSUnionType" || parentType === "TSRestType";
  105. }
  106. function TSInferType(node, parent) {
  107. const parentType = parent.type;
  108. return parentType === "TSArrayType" || parentType === "TSOptionalType";
  109. }
  110. function TSInstantiationExpression(node, parent) {
  111. const parentType = parent.type;
  112. return (parentType === "CallExpression" || parentType === "OptionalCallExpression" || parentType === "NewExpression" || parentType === "TSInstantiationExpression") && !!parent.typeParameters;
  113. }
  114. function BinaryExpression(node, parent) {
  115. if (node.operator === "in") {
  116. const parentType = parent.type;
  117. return parentType === "VariableDeclarator" || parentType === "ForStatement" || parentType === "ForInStatement" || parentType === "ForOfStatement";
  118. }
  119. return false;
  120. }
  121. function SequenceExpression(node, parent) {
  122. const parentType = parent.type;
  123. if (parentType === "ForStatement" || parentType === "ThrowStatement" || parentType === "ReturnStatement" || parentType === "IfStatement" && parent.test === node || parentType === "WhileStatement" && parent.test === node || parentType === "ForInStatement" && parent.right === node || parentType === "SwitchStatement" && parent.discriminant === node || parentType === "ExpressionStatement" && parent.expression === node) {
  124. return false;
  125. }
  126. return true;
  127. }
  128. function YieldExpression(node, parent) {
  129. const parentType = parent.type;
  130. return parentType === "BinaryExpression" || parentType === "LogicalExpression" || parentType === "UnaryExpression" || parentType === "SpreadElement" || hasPostfixPart(node, parent) || parentType === "AwaitExpression" && isYieldExpression(node) || parentType === "ConditionalExpression" && node === parent.test || isClassExtendsClause(node, parent);
  131. }
  132. function ClassExpression(node, parent, printStack) {
  133. return isFirstInContext(printStack, 1 | 4);
  134. }
  135. function UnaryLike(node, parent) {
  136. return hasPostfixPart(node, parent) || isBinaryExpression(parent) && parent.operator === "**" && parent.left === node || isClassExtendsClause(node, parent);
  137. }
  138. function FunctionExpression(node, parent, printStack) {
  139. return isFirstInContext(printStack, 1 | 4);
  140. }
  141. function ArrowFunctionExpression(node, parent) {
  142. return isExportDeclaration(parent) || ConditionalExpression(node, parent);
  143. }
  144. function ConditionalExpression(node, parent) {
  145. const parentType = parent.type;
  146. if (parentType === "UnaryExpression" || parentType === "SpreadElement" || parentType === "BinaryExpression" || parentType === "LogicalExpression" || parentType === "ConditionalExpression" && parent.test === node || parentType === "AwaitExpression" || isTSTypeExpression(parentType)) {
  147. return true;
  148. }
  149. return UnaryLike(node, parent);
  150. }
  151. function OptionalMemberExpression(node, parent) {
  152. return isCallExpression(parent) && parent.callee === node || isMemberExpression(parent) && parent.object === node;
  153. }
  154. function AssignmentExpression(node, parent) {
  155. if (isObjectPattern(node.left)) {
  156. return true;
  157. } else {
  158. return ConditionalExpression(node, parent);
  159. }
  160. }
  161. function LogicalExpression(node, parent) {
  162. const parentType = parent.type;
  163. if (isTSTypeExpression(parentType)) return true;
  164. if (parentType !== "LogicalExpression") return false;
  165. switch (node.operator) {
  166. case "||":
  167. return parent.operator === "??" || parent.operator === "&&";
  168. case "&&":
  169. return parent.operator === "??";
  170. case "??":
  171. return parent.operator !== "??";
  172. }
  173. }
  174. function Identifier(node, parent, printStack) {
  175. var _node$extra;
  176. const parentType = parent.type;
  177. if ((_node$extra = node.extra) != null && _node$extra.parenthesized && parentType === "AssignmentExpression" && parent.left === node) {
  178. const rightType = parent.right.type;
  179. if ((rightType === "FunctionExpression" || rightType === "ClassExpression") && parent.right.id == null) {
  180. return true;
  181. }
  182. }
  183. if (node.name === "let") {
  184. const isFollowedByBracket = isMemberExpression(parent, {
  185. object: node,
  186. computed: true
  187. }) || isOptionalMemberExpression(parent, {
  188. object: node,
  189. computed: true,
  190. optional: false
  191. });
  192. return isFirstInContext(printStack, isFollowedByBracket ? 1 | 8 | 16 | 32 : 32);
  193. }
  194. return node.name === "async" && isForOfStatement(parent) && node === parent.left;
  195. }
  196. function isFirstInContext(printStack, checkParam) {
  197. const expressionStatement = checkParam & 1;
  198. const arrowBody = checkParam & 2;
  199. const exportDefault = checkParam & 4;
  200. const forHead = checkParam & 8;
  201. const forInHead = checkParam & 16;
  202. const forOfHead = checkParam & 32;
  203. let i = printStack.length - 1;
  204. if (i <= 0) return;
  205. let node = printStack[i];
  206. i--;
  207. let parent = printStack[i];
  208. while (i >= 0) {
  209. const parentType = parent.type;
  210. if (expressionStatement && parentType === "ExpressionStatement" && parent.expression === node || exportDefault && parentType === "ExportDefaultDeclaration" && node === parent.declaration || arrowBody && parentType === "ArrowFunctionExpression" && parent.body === node || forHead && parentType === "ForStatement" && parent.init === node || forInHead && parentType === "ForInStatement" && parent.left === node || forOfHead && parentType === "ForOfStatement" && parent.left === node) {
  211. return true;
  212. }
  213. if (i > 0 && (hasPostfixPart(node, parent) && parentType !== "NewExpression" || parentType === "SequenceExpression" && parent.expressions[0] === node || parentType === "UpdateExpression" && !parent.prefix || parentType === "ConditionalExpression" && parent.test === node || (parentType === "BinaryExpression" || parentType === "LogicalExpression") && parent.left === node || parentType === "AssignmentExpression" && parent.left === node)) {
  214. node = parent;
  215. i--;
  216. parent = printStack[i];
  217. } else {
  218. return false;
  219. }
  220. }
  221. return false;
  222. }
  223. //# sourceMappingURL=parentheses.js.map