whitespace.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.nodes = void 0;
  6. var _t = require("@babel/types");
  7. const {
  8. FLIPPED_ALIAS_KEYS,
  9. isArrayExpression,
  10. isAssignmentExpression,
  11. isBinary,
  12. isBlockStatement,
  13. isCallExpression,
  14. isFunction,
  15. isIdentifier,
  16. isLiteral,
  17. isMemberExpression,
  18. isObjectExpression,
  19. isOptionalCallExpression,
  20. isOptionalMemberExpression,
  21. isStringLiteral
  22. } = _t;
  23. function crawlInternal(node, state) {
  24. if (!node) return state;
  25. if (isMemberExpression(node) || isOptionalMemberExpression(node)) {
  26. crawlInternal(node.object, state);
  27. if (node.computed) crawlInternal(node.property, state);
  28. } else if (isBinary(node) || isAssignmentExpression(node)) {
  29. crawlInternal(node.left, state);
  30. crawlInternal(node.right, state);
  31. } else if (isCallExpression(node) || isOptionalCallExpression(node)) {
  32. state.hasCall = true;
  33. crawlInternal(node.callee, state);
  34. } else if (isFunction(node)) {
  35. state.hasFunction = true;
  36. } else if (isIdentifier(node)) {
  37. state.hasHelper =
  38. state.hasHelper || node.callee && isHelper(node.callee);
  39. }
  40. return state;
  41. }
  42. function crawl(node) {
  43. return crawlInternal(node, {
  44. hasCall: false,
  45. hasFunction: false,
  46. hasHelper: false
  47. });
  48. }
  49. function isHelper(node) {
  50. if (!node) return false;
  51. if (isMemberExpression(node)) {
  52. return isHelper(node.object) || isHelper(node.property);
  53. } else if (isIdentifier(node)) {
  54. return node.name === "require" || node.name.charCodeAt(0) === 95;
  55. } else if (isCallExpression(node)) {
  56. return isHelper(node.callee);
  57. } else if (isBinary(node) || isAssignmentExpression(node)) {
  58. return isIdentifier(node.left) && isHelper(node.left) || isHelper(node.right);
  59. } else {
  60. return false;
  61. }
  62. }
  63. function isType(node) {
  64. return isLiteral(node) || isObjectExpression(node) || isArrayExpression(node) || isIdentifier(node) || isMemberExpression(node);
  65. }
  66. const nodes = {
  67. AssignmentExpression(node) {
  68. const state = crawl(node.right);
  69. if (state.hasCall && state.hasHelper || state.hasFunction) {
  70. return state.hasFunction ? 1 | 2 : 2;
  71. }
  72. },
  73. SwitchCase(node, parent) {
  74. return (!!node.consequent.length || parent.cases[0] === node ? 1 : 0) | (!node.consequent.length && parent.cases[parent.cases.length - 1] === node ? 2 : 0);
  75. },
  76. LogicalExpression(node) {
  77. if (isFunction(node.left) || isFunction(node.right)) {
  78. return 2;
  79. }
  80. },
  81. Literal(node) {
  82. if (isStringLiteral(node) && node.value === "use strict") {
  83. return 2;
  84. }
  85. },
  86. CallExpression(node) {
  87. if (isFunction(node.callee) || isHelper(node)) {
  88. return 1 | 2;
  89. }
  90. },
  91. OptionalCallExpression(node) {
  92. if (isFunction(node.callee)) {
  93. return 1 | 2;
  94. }
  95. },
  96. VariableDeclaration(node) {
  97. for (let i = 0; i < node.declarations.length; i++) {
  98. const declar = node.declarations[i];
  99. let enabled = isHelper(declar.id) && !isType(declar.init);
  100. if (!enabled && declar.init) {
  101. const state = crawl(declar.init);
  102. enabled = isHelper(declar.init) && state.hasCall || state.hasFunction;
  103. }
  104. if (enabled) {
  105. return 1 | 2;
  106. }
  107. }
  108. },
  109. IfStatement(node) {
  110. if (isBlockStatement(node.consequent)) {
  111. return 1 | 2;
  112. }
  113. }
  114. };
  115. exports.nodes = nodes;
  116. nodes.ObjectProperty = nodes.ObjectTypeProperty = nodes.ObjectMethod = function (node, parent) {
  117. if (parent.properties[0] === node) {
  118. return 1;
  119. }
  120. };
  121. nodes.ObjectTypeCallProperty = function (node, parent) {
  122. var _parent$properties;
  123. if (parent.callProperties[0] === node && !((_parent$properties = parent.properties) != null && _parent$properties.length)) {
  124. return 1;
  125. }
  126. };
  127. nodes.ObjectTypeIndexer = function (node, parent) {
  128. var _parent$properties2, _parent$callPropertie;
  129. if (parent.indexers[0] === node && !((_parent$properties2 = parent.properties) != null && _parent$properties2.length) && !((_parent$callPropertie = parent.callProperties) != null && _parent$callPropertie.length)) {
  130. return 1;
  131. }
  132. };
  133. nodes.ObjectTypeInternalSlot = function (node, parent) {
  134. var _parent$properties3, _parent$callPropertie2, _parent$indexers;
  135. if (parent.internalSlots[0] === node && !((_parent$properties3 = parent.properties) != null && _parent$properties3.length) && !((_parent$callPropertie2 = parent.callProperties) != null && _parent$callPropertie2.length) && !((_parent$indexers = parent.indexers) != null && _parent$indexers.length)) {
  136. return 1;
  137. }
  138. };
  139. [["Function", true], ["Class", true], ["Loop", true], ["LabeledStatement", true], ["SwitchStatement", true], ["TryStatement", true]].forEach(function ([type, amounts]) {
  140. [type].concat(FLIPPED_ALIAS_KEYS[type] || []).forEach(function (type) {
  141. const ret = amounts ? 1 | 2 : 0;
  142. nodes[type] = () => ret;
  143. });
  144. });
  145. //# sourceMappingURL=whitespace.js.map