index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.TokenContext = void 0;
  6. exports.isLastChild = isLastChild;
  7. exports.needsParens = needsParens;
  8. exports.needsWhitespace = needsWhitespace;
  9. exports.needsWhitespaceAfter = needsWhitespaceAfter;
  10. exports.needsWhitespaceBefore = needsWhitespaceBefore;
  11. var whitespace = require("./whitespace.js");
  12. var parens = require("./parentheses.js");
  13. var _t = require("@babel/types");
  14. const {
  15. FLIPPED_ALIAS_KEYS,
  16. VISITOR_KEYS,
  17. isCallExpression,
  18. isDecorator,
  19. isExpressionStatement,
  20. isMemberExpression,
  21. isNewExpression,
  22. isParenthesizedExpression
  23. } = _t;
  24. const TokenContext = exports.TokenContext = {
  25. expressionStatement: 1,
  26. arrowBody: 2,
  27. exportDefault: 4,
  28. forHead: 8,
  29. forInHead: 16,
  30. forOfHead: 32,
  31. arrowFlowReturnType: 64
  32. };
  33. function expandAliases(obj) {
  34. const map = new Map();
  35. function add(type, func) {
  36. const fn = map.get(type);
  37. map.set(type, fn ? function (node, parent, stack, inForInit, getRawIdentifier) {
  38. var _fn;
  39. return (_fn = fn(node, parent, stack, inForInit, getRawIdentifier)) != null ? _fn : func(node, parent, stack, inForInit, getRawIdentifier);
  40. } : func);
  41. }
  42. for (const type of Object.keys(obj)) {
  43. const aliases = FLIPPED_ALIAS_KEYS[type];
  44. if (aliases) {
  45. for (const alias of aliases) {
  46. add(alias, obj[type]);
  47. }
  48. } else {
  49. add(type, obj[type]);
  50. }
  51. }
  52. return map;
  53. }
  54. const expandedParens = expandAliases(parens);
  55. const expandedWhitespaceNodes = expandAliases(whitespace.nodes);
  56. function isOrHasCallExpression(node) {
  57. if (isCallExpression(node)) {
  58. return true;
  59. }
  60. return isMemberExpression(node) && isOrHasCallExpression(node.object);
  61. }
  62. function needsWhitespace(node, parent, type) {
  63. var _expandedWhitespaceNo;
  64. if (!node) return false;
  65. if (isExpressionStatement(node)) {
  66. node = node.expression;
  67. }
  68. const flag = (_expandedWhitespaceNo = expandedWhitespaceNodes.get(node.type)) == null ? void 0 : _expandedWhitespaceNo(node, parent);
  69. if (typeof flag === "number") {
  70. return (flag & type) !== 0;
  71. }
  72. return false;
  73. }
  74. function needsWhitespaceBefore(node, parent) {
  75. return needsWhitespace(node, parent, 1);
  76. }
  77. function needsWhitespaceAfter(node, parent) {
  78. return needsWhitespace(node, parent, 2);
  79. }
  80. function needsParens(node, parent, tokenContext, inForInit, getRawIdentifier) {
  81. var _expandedParens$get;
  82. if (!parent) return false;
  83. if (isNewExpression(parent) && parent.callee === node) {
  84. if (isOrHasCallExpression(node)) return true;
  85. }
  86. if (isDecorator(parent)) {
  87. return !isDecoratorMemberExpression(node) && !(isCallExpression(node) && isDecoratorMemberExpression(node.callee)) && !isParenthesizedExpression(node);
  88. }
  89. return (_expandedParens$get = expandedParens.get(node.type)) == null ? void 0 : _expandedParens$get(node, parent, tokenContext, inForInit, getRawIdentifier);
  90. }
  91. function isDecoratorMemberExpression(node) {
  92. switch (node.type) {
  93. case "Identifier":
  94. return true;
  95. case "MemberExpression":
  96. return !node.computed && node.property.type === "Identifier" && isDecoratorMemberExpression(node.object);
  97. default:
  98. return false;
  99. }
  100. }
  101. function isLastChild(parent, child) {
  102. const visitorKeys = VISITOR_KEYS[parent.type];
  103. for (let i = visitorKeys.length - 1; i >= 0; i--) {
  104. const val = parent[visitorKeys[i]];
  105. if (val === child) {
  106. return true;
  107. } else if (Array.isArray(val)) {
  108. let j = val.length - 1;
  109. while (j >= 0 && val[j] === null) j--;
  110. return j >= 0 && val[j] === child;
  111. } else if (val) {
  112. return false;
  113. }
  114. }
  115. return false;
  116. }
  117. //# sourceMappingURL=index.js.map