You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
5.3 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.list = exports.nodes = void 0;
  6. var t = _interopRequireWildcard(require("@babel/types"));
  7. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  8. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  9. function crawl(node, state = {}) {
  10. if (t.isMemberExpression(node)) {
  11. crawl(node.object, state);
  12. if (node.computed) crawl(node.property, state);
  13. } else if (t.isBinary(node) || t.isAssignmentExpression(node)) {
  14. crawl(node.left, state);
  15. crawl(node.right, state);
  16. } else if (t.isCallExpression(node)) {
  17. state.hasCall = true;
  18. crawl(node.callee, state);
  19. } else if (t.isFunction(node)) {
  20. state.hasFunction = true;
  21. } else if (t.isIdentifier(node)) {
  22. state.hasHelper = state.hasHelper || isHelper(node.callee);
  23. }
  24. return state;
  25. }
  26. function isHelper(node) {
  27. if (t.isMemberExpression(node)) {
  28. return isHelper(node.object) || isHelper(node.property);
  29. } else if (t.isIdentifier(node)) {
  30. return node.name === "require" || node.name[0] === "_";
  31. } else if (t.isCallExpression(node)) {
  32. return isHelper(node.callee);
  33. } else if (t.isBinary(node) || t.isAssignmentExpression(node)) {
  34. return t.isIdentifier(node.left) && isHelper(node.left) || isHelper(node.right);
  35. } else {
  36. return false;
  37. }
  38. }
  39. function isType(node) {
  40. return t.isLiteral(node) || t.isObjectExpression(node) || t.isArrayExpression(node) || t.isIdentifier(node) || t.isMemberExpression(node);
  41. }
  42. const nodes = {
  43. AssignmentExpression(node) {
  44. const state = crawl(node.right);
  45. if (state.hasCall && state.hasHelper || state.hasFunction) {
  46. return {
  47. before: state.hasFunction,
  48. after: true
  49. };
  50. }
  51. },
  52. SwitchCase(node, parent) {
  53. return {
  54. before: node.consequent.length || parent.cases[0] === node,
  55. after: !node.consequent.length && parent.cases[parent.cases.length - 1] === node
  56. };
  57. },
  58. LogicalExpression(node) {
  59. if (t.isFunction(node.left) || t.isFunction(node.right)) {
  60. return {
  61. after: true
  62. };
  63. }
  64. },
  65. Literal(node) {
  66. if (node.value === "use strict") {
  67. return {
  68. after: true
  69. };
  70. }
  71. },
  72. CallExpression(node) {
  73. if (t.isFunction(node.callee) || isHelper(node)) {
  74. return {
  75. before: true,
  76. after: true
  77. };
  78. }
  79. },
  80. VariableDeclaration(node) {
  81. for (let i = 0; i < node.declarations.length; i++) {
  82. const declar = node.declarations[i];
  83. let enabled = isHelper(declar.id) && !isType(declar.init);
  84. if (!enabled) {
  85. const state = crawl(declar.init);
  86. enabled = isHelper(declar.init) && state.hasCall || state.hasFunction;
  87. }
  88. if (enabled) {
  89. return {
  90. before: true,
  91. after: true
  92. };
  93. }
  94. }
  95. },
  96. IfStatement(node) {
  97. if (t.isBlockStatement(node.consequent)) {
  98. return {
  99. before: true,
  100. after: true
  101. };
  102. }
  103. }
  104. };
  105. exports.nodes = nodes;
  106. nodes.ObjectProperty = nodes.ObjectTypeProperty = nodes.ObjectMethod = function (node, parent) {
  107. if (parent.properties[0] === node) {
  108. return {
  109. before: true
  110. };
  111. }
  112. };
  113. nodes.ObjectTypeCallProperty = function (node, parent) {
  114. if (parent.callProperties[0] === node && (!parent.properties || !parent.properties.length)) {
  115. return {
  116. before: true
  117. };
  118. }
  119. };
  120. nodes.ObjectTypeIndexer = function (node, parent) {
  121. if (parent.indexers[0] === node && (!parent.properties || !parent.properties.length) && (!parent.callProperties || !parent.callProperties.length)) {
  122. return {
  123. before: true
  124. };
  125. }
  126. };
  127. nodes.ObjectTypeInternalSlot = function (node, parent) {
  128. if (parent.internalSlots[0] === node && (!parent.properties || !parent.properties.length) && (!parent.callProperties || !parent.callProperties.length) && (!parent.indexers || !parent.indexers.length)) {
  129. return {
  130. before: true
  131. };
  132. }
  133. };
  134. const list = {
  135. VariableDeclaration(node) {
  136. return node.declarations.map(decl => decl.init);
  137. },
  138. ArrayExpression(node) {
  139. return node.elements;
  140. },
  141. ObjectExpression(node) {
  142. return node.properties;
  143. }
  144. };
  145. exports.list = list;
  146. [["Function", true], ["Class", true], ["Loop", true], ["LabeledStatement", true], ["SwitchStatement", true], ["TryStatement", true]].forEach(function ([type, amounts]) {
  147. if (typeof amounts === "boolean") {
  148. amounts = {
  149. after: amounts,
  150. before: amounts
  151. };
  152. }
  153. [type].concat(t.FLIPPED_ALIAS_KEYS[type] || []).forEach(function (type) {
  154. nodes[type] = function () {
  155. return amounts;
  156. };
  157. });
  158. });