parentheses.js 10.0 KB

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