whitespace.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. "use strict";
  2. var _isBoolean = require("lodash/isBoolean");
  3. var _isBoolean2 = _interopRequireDefault(_isBoolean);
  4. var _each = require("lodash/each");
  5. var _each2 = _interopRequireDefault(_each);
  6. var _map = require("lodash/map");
  7. var _map2 = _interopRequireDefault(_map);
  8. var _babelTypes = require("babel-types");
  9. var t = _interopRequireWildcard(_babelTypes);
  10. 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; } }
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. function crawl(node) {
  13. var state = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
  14. if (t.isMemberExpression(node)) {
  15. crawl(node.object, state);
  16. if (node.computed) crawl(node.property, state);
  17. } else if (t.isBinary(node) || t.isAssignmentExpression(node)) {
  18. crawl(node.left, state);
  19. crawl(node.right, state);
  20. } else if (t.isCallExpression(node)) {
  21. state.hasCall = true;
  22. crawl(node.callee, state);
  23. } else if (t.isFunction(node)) {
  24. state.hasFunction = true;
  25. } else if (t.isIdentifier(node)) {
  26. state.hasHelper = state.hasHelper || isHelper(node.callee);
  27. }
  28. return state;
  29. }
  30. function isHelper(node) {
  31. if (t.isMemberExpression(node)) {
  32. return isHelper(node.object) || isHelper(node.property);
  33. } else if (t.isIdentifier(node)) {
  34. return node.name === "require" || node.name[0] === "_";
  35. } else if (t.isCallExpression(node)) {
  36. return isHelper(node.callee);
  37. } else if (t.isBinary(node) || t.isAssignmentExpression(node)) {
  38. return t.isIdentifier(node.left) && isHelper(node.left) || isHelper(node.right);
  39. } else {
  40. return false;
  41. }
  42. }
  43. function isType(node) {
  44. return t.isLiteral(node) || t.isObjectExpression(node) || t.isArrayExpression(node) || t.isIdentifier(node) || t.isMemberExpression(node);
  45. }
  46. exports.nodes = {
  47. AssignmentExpression: function AssignmentExpression(node) {
  48. var state = crawl(node.right);
  49. if (state.hasCall && state.hasHelper || state.hasFunction) {
  50. return {
  51. before: state.hasFunction,
  52. after: true
  53. };
  54. }
  55. },
  56. SwitchCase: function SwitchCase(node, parent) {
  57. return {
  58. before: node.consequent.length || parent.cases[0] === node
  59. };
  60. },
  61. LogicalExpression: function LogicalExpression(node) {
  62. if (t.isFunction(node.left) || t.isFunction(node.right)) {
  63. return {
  64. after: true
  65. };
  66. }
  67. },
  68. Literal: function Literal(node) {
  69. if (node.value === "use strict") {
  70. return {
  71. after: true
  72. };
  73. }
  74. },
  75. CallExpression: function CallExpression(node) {
  76. if (t.isFunction(node.callee) || isHelper(node)) {
  77. return {
  78. before: true,
  79. after: true
  80. };
  81. }
  82. },
  83. VariableDeclaration: function VariableDeclaration(node) {
  84. for (var i = 0; i < node.declarations.length; i++) {
  85. var declar = node.declarations[i];
  86. var enabled = isHelper(declar.id) && !isType(declar.init);
  87. if (!enabled) {
  88. var state = crawl(declar.init);
  89. enabled = isHelper(declar.init) && state.hasCall || state.hasFunction;
  90. }
  91. if (enabled) {
  92. return {
  93. before: true,
  94. after: true
  95. };
  96. }
  97. }
  98. },
  99. IfStatement: function IfStatement(node) {
  100. if (t.isBlockStatement(node.consequent)) {
  101. return {
  102. before: true,
  103. after: true
  104. };
  105. }
  106. }
  107. };
  108. exports.nodes.ObjectProperty = exports.nodes.ObjectTypeProperty = exports.nodes.ObjectMethod = exports.nodes.SpreadProperty = function (node, parent) {
  109. if (parent.properties[0] === node) {
  110. return {
  111. before: true
  112. };
  113. }
  114. };
  115. exports.list = {
  116. VariableDeclaration: function VariableDeclaration(node) {
  117. return (0, _map2.default)(node.declarations, "init");
  118. },
  119. ArrayExpression: function ArrayExpression(node) {
  120. return node.elements;
  121. },
  122. ObjectExpression: function ObjectExpression(node) {
  123. return node.properties;
  124. }
  125. };
  126. (0, _each2.default)({
  127. Function: true,
  128. Class: true,
  129. Loop: true,
  130. LabeledStatement: true,
  131. SwitchStatement: true,
  132. TryStatement: true
  133. }, function (amounts, type) {
  134. if ((0, _isBoolean2.default)(amounts)) {
  135. amounts = { after: amounts, before: amounts };
  136. }
  137. (0, _each2.default)([type].concat(t.FLIPPED_ALIAS_KEYS[type] || []), function (type) {
  138. exports.nodes[type] = function () {
  139. return amounts;
  140. };
  141. });
  142. });