whitespace.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. "use strict";
  2. var _map = require("lodash/map");
  3. var _map2 = _interopRequireDefault(_map);
  4. var _babelTypes = require("babel-types");
  5. var t = _interopRequireWildcard(_babelTypes);
  6. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. function crawl(node) {
  9. var state = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  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. exports.nodes = {
  43. AssignmentExpression: function AssignmentExpression(node) {
  44. var 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: function SwitchCase(node, parent) {
  53. return {
  54. before: node.consequent.length || parent.cases[0] === node
  55. };
  56. },
  57. LogicalExpression: function LogicalExpression(node) {
  58. if (t.isFunction(node.left) || t.isFunction(node.right)) {
  59. return {
  60. after: true
  61. };
  62. }
  63. },
  64. Literal: function Literal(node) {
  65. if (node.value === "use strict") {
  66. return {
  67. after: true
  68. };
  69. }
  70. },
  71. CallExpression: function CallExpression(node) {
  72. if (t.isFunction(node.callee) || isHelper(node)) {
  73. return {
  74. before: true,
  75. after: true
  76. };
  77. }
  78. },
  79. VariableDeclaration: function VariableDeclaration(node) {
  80. for (var i = 0; i < node.declarations.length; i++) {
  81. var declar = node.declarations[i];
  82. var enabled = isHelper(declar.id) && !isType(declar.init);
  83. if (!enabled) {
  84. var state = crawl(declar.init);
  85. enabled = isHelper(declar.init) && state.hasCall || state.hasFunction;
  86. }
  87. if (enabled) {
  88. return {
  89. before: true,
  90. after: true
  91. };
  92. }
  93. }
  94. },
  95. IfStatement: function IfStatement(node) {
  96. if (t.isBlockStatement(node.consequent)) {
  97. return {
  98. before: true,
  99. after: true
  100. };
  101. }
  102. }
  103. };
  104. exports.nodes.ObjectProperty = exports.nodes.ObjectTypeProperty = exports.nodes.ObjectMethod = exports.nodes.SpreadProperty = function (node, parent) {
  105. if (parent.properties[0] === node) {
  106. return {
  107. before: true
  108. };
  109. }
  110. };
  111. exports.list = {
  112. VariableDeclaration: function VariableDeclaration(node) {
  113. return (0, _map2.default)(node.declarations, "init");
  114. },
  115. ArrayExpression: function ArrayExpression(node) {
  116. return node.elements;
  117. },
  118. ObjectExpression: function ObjectExpression(node) {
  119. return node.properties;
  120. }
  121. };
  122. [["Function", true], ["Class", true], ["Loop", true], ["LabeledStatement", true], ["SwitchStatement", true], ["TryStatement", true]].forEach(function (_ref) {
  123. var type = _ref[0],
  124. amounts = _ref[1];
  125. if (typeof amounts === "boolean") {
  126. amounts = { after: amounts, before: amounts };
  127. }
  128. [type].concat(t.FLIPPED_ALIAS_KEYS[type] || []).forEach(function (type) {
  129. exports.nodes[type] = function () {
  130. return amounts;
  131. };
  132. });
  133. });