parentheses.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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.TSAsExpression = TSAsExpression;
  22. exports.TSInferType = TSInferType;
  23. exports.TSTypeAssertion = TSTypeAssertion;
  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. isAssignmentExpression,
  34. isAwaitExpression,
  35. isBinary,
  36. isBinaryExpression,
  37. isCallExpression,
  38. isClassDeclaration,
  39. isClassExpression,
  40. isConditional,
  41. isConditionalExpression,
  42. isExportDeclaration,
  43. isExportDefaultDeclaration,
  44. isExpressionStatement,
  45. isFor,
  46. isForInStatement,
  47. isForOfStatement,
  48. isForStatement,
  49. isIfStatement,
  50. isIndexedAccessType,
  51. isIntersectionTypeAnnotation,
  52. isLogicalExpression,
  53. isMemberExpression,
  54. isNewExpression,
  55. isNullableTypeAnnotation,
  56. isObjectPattern,
  57. isOptionalCallExpression,
  58. isOptionalMemberExpression,
  59. isReturnStatement,
  60. isSequenceExpression,
  61. isSwitchStatement,
  62. isTSArrayType,
  63. isTSAsExpression,
  64. isTSIntersectionType,
  65. isTSNonNullExpression,
  66. isTSOptionalType,
  67. isTSRestType,
  68. isTSTypeAssertion,
  69. isTSUnionType,
  70. isTaggedTemplateExpression,
  71. isThrowStatement,
  72. isTypeAnnotation,
  73. isUnaryLike,
  74. isUnionTypeAnnotation,
  75. isVariableDeclarator,
  76. isWhileStatement,
  77. isYieldExpression
  78. } = _t;
  79. const PRECEDENCE = {
  80. "||": 0,
  81. "??": 0,
  82. "&&": 1,
  83. "|": 2,
  84. "^": 3,
  85. "&": 4,
  86. "==": 5,
  87. "===": 5,
  88. "!=": 5,
  89. "!==": 5,
  90. "<": 6,
  91. ">": 6,
  92. "<=": 6,
  93. ">=": 6,
  94. in: 6,
  95. instanceof: 6,
  96. ">>": 7,
  97. "<<": 7,
  98. ">>>": 7,
  99. "+": 8,
  100. "-": 8,
  101. "*": 9,
  102. "/": 9,
  103. "%": 9,
  104. "**": 10
  105. };
  106. const isClassExtendsClause = (node, parent) => (isClassDeclaration(parent) || isClassExpression(parent)) && parent.superClass === node;
  107. const hasPostfixPart = (node, parent) => (isMemberExpression(parent) || isOptionalMemberExpression(parent)) && parent.object === node || (isCallExpression(parent) || isOptionalCallExpression(parent) || isNewExpression(parent)) && parent.callee === node || isTaggedTemplateExpression(parent) && parent.tag === node || isTSNonNullExpression(parent);
  108. function NullableTypeAnnotation(node, parent) {
  109. return isArrayTypeAnnotation(parent);
  110. }
  111. function FunctionTypeAnnotation(node, parent, printStack) {
  112. return isUnionTypeAnnotation(parent) || isIntersectionTypeAnnotation(parent) || isArrayTypeAnnotation(parent) || isTypeAnnotation(parent) && isArrowFunctionExpression(printStack[printStack.length - 3]);
  113. }
  114. function UpdateExpression(node, parent) {
  115. return hasPostfixPart(node, parent) || isClassExtendsClause(node, parent);
  116. }
  117. function ObjectExpression(node, parent, printStack) {
  118. return isFirstInContext(printStack, {
  119. expressionStatement: true,
  120. arrowBody: true
  121. });
  122. }
  123. function DoExpression(node, parent, printStack) {
  124. return !node.async && isFirstInContext(printStack, {
  125. expressionStatement: true
  126. });
  127. }
  128. function Binary(node, parent) {
  129. if (node.operator === "**" && isBinaryExpression(parent, {
  130. operator: "**"
  131. })) {
  132. return parent.left === node;
  133. }
  134. if (isClassExtendsClause(node, parent)) {
  135. return true;
  136. }
  137. if (hasPostfixPart(node, parent) || isUnaryLike(parent) || isAwaitExpression(parent)) {
  138. return true;
  139. }
  140. if (isBinary(parent)) {
  141. const parentOp = parent.operator;
  142. const parentPos = PRECEDENCE[parentOp];
  143. const nodeOp = node.operator;
  144. const nodePos = PRECEDENCE[nodeOp];
  145. if (parentPos === nodePos && parent.right === node && !isLogicalExpression(parent) || parentPos > nodePos) {
  146. return true;
  147. }
  148. }
  149. }
  150. function UnionTypeAnnotation(node, parent) {
  151. return isArrayTypeAnnotation(parent) || isNullableTypeAnnotation(parent) || isIntersectionTypeAnnotation(parent) || isUnionTypeAnnotation(parent);
  152. }
  153. function OptionalIndexedAccessType(node, parent) {
  154. return isIndexedAccessType(parent, {
  155. objectType: node
  156. });
  157. }
  158. function TSAsExpression() {
  159. return true;
  160. }
  161. function TSTypeAssertion() {
  162. return true;
  163. }
  164. function TSUnionType(node, parent) {
  165. return isTSArrayType(parent) || isTSOptionalType(parent) || isTSIntersectionType(parent) || isTSUnionType(parent) || isTSRestType(parent);
  166. }
  167. function TSInferType(node, parent) {
  168. return isTSArrayType(parent) || isTSOptionalType(parent);
  169. }
  170. function BinaryExpression(node, parent) {
  171. return node.operator === "in" && (isVariableDeclarator(parent) || isFor(parent));
  172. }
  173. function SequenceExpression(node, parent) {
  174. if (isForStatement(parent) || isThrowStatement(parent) || isReturnStatement(parent) || isIfStatement(parent) && parent.test === node || isWhileStatement(parent) && parent.test === node || isForInStatement(parent) && parent.right === node || isSwitchStatement(parent) && parent.discriminant === node || isExpressionStatement(parent) && parent.expression === node) {
  175. return false;
  176. }
  177. return true;
  178. }
  179. function YieldExpression(node, parent) {
  180. return isBinary(parent) || isUnaryLike(parent) || hasPostfixPart(node, parent) || isAwaitExpression(parent) && isYieldExpression(node) || isConditionalExpression(parent) && node === parent.test || isClassExtendsClause(node, parent);
  181. }
  182. function ClassExpression(node, parent, printStack) {
  183. return isFirstInContext(printStack, {
  184. expressionStatement: true,
  185. exportDefault: true
  186. });
  187. }
  188. function UnaryLike(node, parent) {
  189. return hasPostfixPart(node, parent) || isBinaryExpression(parent, {
  190. operator: "**",
  191. left: node
  192. }) || isClassExtendsClause(node, parent);
  193. }
  194. function FunctionExpression(node, parent, printStack) {
  195. return isFirstInContext(printStack, {
  196. expressionStatement: true,
  197. exportDefault: true
  198. });
  199. }
  200. function ArrowFunctionExpression(node, parent) {
  201. return isExportDeclaration(parent) || ConditionalExpression(node, parent);
  202. }
  203. function ConditionalExpression(node, parent) {
  204. if (isUnaryLike(parent) || isBinary(parent) || isConditionalExpression(parent, {
  205. test: node
  206. }) || isAwaitExpression(parent) || isTSTypeAssertion(parent) || isTSAsExpression(parent)) {
  207. return true;
  208. }
  209. return UnaryLike(node, parent);
  210. }
  211. function OptionalMemberExpression(node, parent) {
  212. return isCallExpression(parent, {
  213. callee: node
  214. }) || isMemberExpression(parent, {
  215. object: node
  216. });
  217. }
  218. function AssignmentExpression(node, parent) {
  219. if (isObjectPattern(node.left)) {
  220. return true;
  221. } else {
  222. return ConditionalExpression(node, parent);
  223. }
  224. }
  225. function LogicalExpression(node, parent) {
  226. switch (node.operator) {
  227. case "||":
  228. if (!isLogicalExpression(parent)) return false;
  229. return parent.operator === "??" || parent.operator === "&&";
  230. case "&&":
  231. return isLogicalExpression(parent, {
  232. operator: "??"
  233. });
  234. case "??":
  235. return isLogicalExpression(parent) && parent.operator !== "??";
  236. }
  237. }
  238. function Identifier(node, parent, printStack) {
  239. if (node.name === "let") {
  240. const isFollowedByBracket = isMemberExpression(parent, {
  241. object: node,
  242. computed: true
  243. }) || isOptionalMemberExpression(parent, {
  244. object: node,
  245. computed: true,
  246. optional: false
  247. });
  248. return isFirstInContext(printStack, {
  249. expressionStatement: isFollowedByBracket,
  250. forHead: isFollowedByBracket,
  251. forInHead: isFollowedByBracket,
  252. forOfHead: true
  253. });
  254. }
  255. return node.name === "async" && isForOfStatement(parent) && node === parent.left;
  256. }
  257. function isFirstInContext(printStack, {
  258. expressionStatement = false,
  259. arrowBody = false,
  260. exportDefault = false,
  261. forHead = false,
  262. forInHead = false,
  263. forOfHead = false
  264. }) {
  265. let i = printStack.length - 1;
  266. let node = printStack[i];
  267. i--;
  268. let parent = printStack[i];
  269. while (i >= 0) {
  270. if (expressionStatement && isExpressionStatement(parent, {
  271. expression: node
  272. }) || exportDefault && isExportDefaultDeclaration(parent, {
  273. declaration: node
  274. }) || arrowBody && isArrowFunctionExpression(parent, {
  275. body: node
  276. }) || forHead && isForStatement(parent, {
  277. init: node
  278. }) || forInHead && isForInStatement(parent, {
  279. left: node
  280. }) || forOfHead && isForOfStatement(parent, {
  281. left: node
  282. })) {
  283. return true;
  284. }
  285. if (hasPostfixPart(node, parent) && !isNewExpression(parent) || isSequenceExpression(parent) && parent.expressions[0] === node || isConditional(parent, {
  286. test: node
  287. }) || isBinary(parent, {
  288. left: node
  289. }) || isAssignmentExpression(parent, {
  290. left: node
  291. })) {
  292. node = parent;
  293. i--;
  294. parent = printStack[i];
  295. } else {
  296. return false;
  297. }
  298. }
  299. return false;
  300. }