whitespace.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.nodes = exports.list = 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 crawl(node, state = {}) {
  24. if (isMemberExpression(node) || isOptionalMemberExpression(node)) {
  25. crawl(node.object, state);
  26. if (node.computed) crawl(node.property, state);
  27. } else if (isBinary(node) || isAssignmentExpression(node)) {
  28. crawl(node.left, state);
  29. crawl(node.right, state);
  30. } else if (isCallExpression(node) || isOptionalCallExpression(node)) {
  31. state.hasCall = true;
  32. crawl(node.callee, state);
  33. } else if (isFunction(node)) {
  34. state.hasFunction = true;
  35. } else if (isIdentifier(node)) {
  36. state.hasHelper = state.hasHelper || isHelper(node.callee);
  37. }
  38. return state;
  39. }
  40. function isHelper(node) {
  41. if (isMemberExpression(node)) {
  42. return isHelper(node.object) || isHelper(node.property);
  43. } else if (isIdentifier(node)) {
  44. return node.name === "require" || node.name[0] === "_";
  45. } else if (isCallExpression(node)) {
  46. return isHelper(node.callee);
  47. } else if (isBinary(node) || isAssignmentExpression(node)) {
  48. return isIdentifier(node.left) && isHelper(node.left) || isHelper(node.right);
  49. } else {
  50. return false;
  51. }
  52. }
  53. function isType(node) {
  54. return isLiteral(node) || isObjectExpression(node) || isArrayExpression(node) || isIdentifier(node) || isMemberExpression(node);
  55. }
  56. const nodes = {
  57. AssignmentExpression(node) {
  58. const state = crawl(node.right);
  59. if (state.hasCall && state.hasHelper || state.hasFunction) {
  60. return {
  61. before: state.hasFunction,
  62. after: true
  63. };
  64. }
  65. },
  66. SwitchCase(node, parent) {
  67. return {
  68. before: !!node.consequent.length || parent.cases[0] === node,
  69. after: !node.consequent.length && parent.cases[parent.cases.length - 1] === node
  70. };
  71. },
  72. LogicalExpression(node) {
  73. if (isFunction(node.left) || isFunction(node.right)) {
  74. return {
  75. after: true
  76. };
  77. }
  78. },
  79. Literal(node) {
  80. if (isStringLiteral(node) && node.value === "use strict") {
  81. return {
  82. after: true
  83. };
  84. }
  85. },
  86. CallExpression(node) {
  87. if (isFunction(node.callee) || isHelper(node)) {
  88. return {
  89. before: true,
  90. after: true
  91. };
  92. }
  93. },
  94. OptionalCallExpression(node) {
  95. if (isFunction(node.callee)) {
  96. return {
  97. before: true,
  98. after: true
  99. };
  100. }
  101. },
  102. VariableDeclaration(node) {
  103. for (let i = 0; i < node.declarations.length; i++) {
  104. const declar = node.declarations[i];
  105. let enabled = isHelper(declar.id) && !isType(declar.init);
  106. if (!enabled) {
  107. const state = crawl(declar.init);
  108. enabled = isHelper(declar.init) && state.hasCall || state.hasFunction;
  109. }
  110. if (enabled) {
  111. return {
  112. before: true,
  113. after: true
  114. };
  115. }
  116. }
  117. },
  118. IfStatement(node) {
  119. if (isBlockStatement(node.consequent)) {
  120. return {
  121. before: true,
  122. after: true
  123. };
  124. }
  125. }
  126. };
  127. exports.nodes = nodes;
  128. nodes.ObjectProperty = nodes.ObjectTypeProperty = nodes.ObjectMethod = function (node, parent) {
  129. if (parent.properties[0] === node) {
  130. return {
  131. before: true
  132. };
  133. }
  134. };
  135. nodes.ObjectTypeCallProperty = function (node, parent) {
  136. var _parent$properties;
  137. if (parent.callProperties[0] === node && !((_parent$properties = parent.properties) != null && _parent$properties.length)) {
  138. return {
  139. before: true
  140. };
  141. }
  142. };
  143. nodes.ObjectTypeIndexer = function (node, parent) {
  144. var _parent$properties2, _parent$callPropertie;
  145. if (parent.indexers[0] === node && !((_parent$properties2 = parent.properties) != null && _parent$properties2.length) && !((_parent$callPropertie = parent.callProperties) != null && _parent$callPropertie.length)) {
  146. return {
  147. before: true
  148. };
  149. }
  150. };
  151. nodes.ObjectTypeInternalSlot = function (node, parent) {
  152. var _parent$properties3, _parent$callPropertie2, _parent$indexers;
  153. 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)) {
  154. return {
  155. before: true
  156. };
  157. }
  158. };
  159. const list = {
  160. VariableDeclaration(node) {
  161. return node.declarations.map(decl => decl.init);
  162. },
  163. ArrayExpression(node) {
  164. return node.elements;
  165. },
  166. ObjectExpression(node) {
  167. return node.properties;
  168. }
  169. };
  170. exports.list = list;
  171. [["Function", true], ["Class", true], ["Loop", true], ["LabeledStatement", true], ["SwitchStatement", true], ["TryStatement", true]].forEach(function ([type, amounts]) {
  172. if (typeof amounts === "boolean") {
  173. amounts = {
  174. after: amounts,
  175. before: amounts
  176. };
  177. }
  178. [type].concat(FLIPPED_ALIAS_KEYS[type] || []).forEach(function (type) {
  179. nodes[type] = function () {
  180. return amounts;
  181. };
  182. });
  183. });