whitespace.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 = state.hasHelper || node.callee && isHelper(node.callee);
  38. }
  39. return state;
  40. }
  41. function crawl(node) {
  42. return crawlInternal(node, {
  43. hasCall: false,
  44. hasFunction: false,
  45. hasHelper: false
  46. });
  47. }
  48. function isHelper(node) {
  49. if (!node) return false;
  50. if (isMemberExpression(node)) {
  51. return isHelper(node.object) || isHelper(node.property);
  52. } else if (isIdentifier(node)) {
  53. return node.name === "require" || node.name.charCodeAt(0) === 95;
  54. } else if (isCallExpression(node)) {
  55. return isHelper(node.callee);
  56. } else if (isBinary(node) || isAssignmentExpression(node)) {
  57. return isIdentifier(node.left) && isHelper(node.left) || isHelper(node.right);
  58. } else {
  59. return false;
  60. }
  61. }
  62. function isType(node) {
  63. return isLiteral(node) || isObjectExpression(node) || isArrayExpression(node) || isIdentifier(node) || isMemberExpression(node);
  64. }
  65. const nodes = exports.nodes = {
  66. AssignmentExpression(node) {
  67. const state = crawl(node.right);
  68. if (state.hasCall && state.hasHelper || state.hasFunction) {
  69. return state.hasFunction ? 1 | 2 : 2;
  70. }
  71. return 0;
  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. return 0;
  81. },
  82. Literal(node) {
  83. if (isStringLiteral(node) && node.value === "use strict") {
  84. return 2;
  85. }
  86. return 0;
  87. },
  88. CallExpression(node) {
  89. if (isFunction(node.callee) || isHelper(node)) {
  90. return 1 | 2;
  91. }
  92. return 0;
  93. },
  94. OptionalCallExpression(node) {
  95. if (isFunction(node.callee)) {
  96. return 1 | 2;
  97. }
  98. return 0;
  99. },
  100. VariableDeclaration(node) {
  101. for (let i = 0; i < node.declarations.length; i++) {
  102. const declar = node.declarations[i];
  103. let enabled = isHelper(declar.id) && !isType(declar.init);
  104. if (!enabled && declar.init) {
  105. const state = crawl(declar.init);
  106. enabled = isHelper(declar.init) && state.hasCall || state.hasFunction;
  107. }
  108. if (enabled) {
  109. return 1 | 2;
  110. }
  111. }
  112. return 0;
  113. },
  114. IfStatement(node) {
  115. if (isBlockStatement(node.consequent)) {
  116. return 1 | 2;
  117. }
  118. return 0;
  119. }
  120. };
  121. nodes.ObjectProperty = nodes.ObjectTypeProperty = nodes.ObjectMethod = function (node, parent) {
  122. if (parent.properties[0] === node) {
  123. return 1;
  124. }
  125. return 0;
  126. };
  127. nodes.ObjectTypeCallProperty = function (node, parent) {
  128. var _parent$properties;
  129. if (parent.callProperties[0] === node && !((_parent$properties = parent.properties) != null && _parent$properties.length)) {
  130. return 1;
  131. }
  132. return 0;
  133. };
  134. nodes.ObjectTypeIndexer = function (node, parent) {
  135. var _parent$properties2, _parent$callPropertie;
  136. if (parent.indexers[0] === node && !((_parent$properties2 = parent.properties) != null && _parent$properties2.length) && !((_parent$callPropertie = parent.callProperties) != null && _parent$callPropertie.length)) {
  137. return 1;
  138. }
  139. return 0;
  140. };
  141. nodes.ObjectTypeInternalSlot = function (node, parent) {
  142. var _parent$properties3, _parent$callPropertie2, _parent$indexers;
  143. 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)) {
  144. return 1;
  145. }
  146. return 0;
  147. };
  148. [["Function", true], ["Class", true], ["Loop", true], ["LabeledStatement", true], ["SwitchStatement", true], ["TryStatement", true]].forEach(function ([type, amounts]) {
  149. [type].concat(FLIPPED_ALIAS_KEYS[type] || []).forEach(function (type) {
  150. const ret = amounts ? 1 | 2 : 0;
  151. nodes[type] = () => ret;
  152. });
  153. });
  154. //# sourceMappingURL=whitespace.js.map