whitespace.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. },
  72. SwitchCase(node, parent) {
  73. return (!!node.consequent.length || parent.cases[0] === node ? 1 : 0) | (!node.consequent.length && parent.cases[parent.cases.length - 1] === node ? 2 : 0);
  74. },
  75. LogicalExpression(node) {
  76. if (isFunction(node.left) || isFunction(node.right)) {
  77. return 2;
  78. }
  79. },
  80. Literal(node) {
  81. if (isStringLiteral(node) && node.value === "use strict") {
  82. return 2;
  83. }
  84. },
  85. CallExpression(node) {
  86. if (isFunction(node.callee) || isHelper(node)) {
  87. return 1 | 2;
  88. }
  89. },
  90. OptionalCallExpression(node) {
  91. if (isFunction(node.callee)) {
  92. return 1 | 2;
  93. }
  94. },
  95. VariableDeclaration(node) {
  96. for (let i = 0; i < node.declarations.length; i++) {
  97. const declar = node.declarations[i];
  98. let enabled = isHelper(declar.id) && !isType(declar.init);
  99. if (!enabled && declar.init) {
  100. const state = crawl(declar.init);
  101. enabled = isHelper(declar.init) && state.hasCall || state.hasFunction;
  102. }
  103. if (enabled) {
  104. return 1 | 2;
  105. }
  106. }
  107. },
  108. IfStatement(node) {
  109. if (isBlockStatement(node.consequent)) {
  110. return 1 | 2;
  111. }
  112. }
  113. };
  114. nodes.ObjectProperty = nodes.ObjectTypeProperty = nodes.ObjectMethod = function (node, parent) {
  115. if (parent.properties[0] === node) {
  116. return 1;
  117. }
  118. };
  119. nodes.ObjectTypeCallProperty = function (node, parent) {
  120. var _parent$properties;
  121. if (parent.callProperties[0] === node && !((_parent$properties = parent.properties) != null && _parent$properties.length)) {
  122. return 1;
  123. }
  124. };
  125. nodes.ObjectTypeIndexer = function (node, parent) {
  126. var _parent$properties2, _parent$callPropertie;
  127. if (parent.indexers[0] === node && !((_parent$properties2 = parent.properties) != null && _parent$properties2.length) && !((_parent$callPropertie = parent.callProperties) != null && _parent$callPropertie.length)) {
  128. return 1;
  129. }
  130. };
  131. nodes.ObjectTypeInternalSlot = function (node, parent) {
  132. var _parent$properties3, _parent$callPropertie2, _parent$indexers;
  133. 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)) {
  134. return 1;
  135. }
  136. };
  137. [["Function", true], ["Class", true], ["Loop", true], ["LabeledStatement", true], ["SwitchStatement", true], ["TryStatement", true]].forEach(function ([type, amounts]) {
  138. [type].concat(FLIPPED_ALIAS_KEYS[type] || []).forEach(function (type) {
  139. const ret = amounts ? 1 | 2 : 0;
  140. nodes[type] = () => ret;
  141. });
  142. });
  143. //# sourceMappingURL=whitespace.js.map