index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. "use strict";
  2. exports.__esModule = true;
  3. var _getIterator2 = require("babel-runtime/core-js/get-iterator");
  4. var _getIterator3 = _interopRequireDefault(_getIterator2);
  5. var _keys = require("babel-runtime/core-js/object/keys");
  6. var _keys2 = _interopRequireDefault(_keys);
  7. exports.needsWhitespace = needsWhitespace;
  8. exports.needsWhitespaceBefore = needsWhitespaceBefore;
  9. exports.needsWhitespaceAfter = needsWhitespaceAfter;
  10. exports.needsParens = needsParens;
  11. var _whitespace = require("./whitespace");
  12. var _whitespace2 = _interopRequireDefault(_whitespace);
  13. var _parentheses = require("./parentheses");
  14. var parens = _interopRequireWildcard(_parentheses);
  15. var _babelTypes = require("babel-types");
  16. var t = _interopRequireWildcard(_babelTypes);
  17. 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; } }
  18. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  19. function expandAliases(obj) {
  20. var newObj = {};
  21. function add(type, func) {
  22. var fn = newObj[type];
  23. newObj[type] = fn ? function (node, parent, stack) {
  24. var result = fn(node, parent, stack);
  25. return result == null ? func(node, parent, stack) : result;
  26. } : func;
  27. }
  28. for (var _iterator = (0, _keys2.default)(obj), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  29. var _ref;
  30. if (_isArray) {
  31. if (_i >= _iterator.length) break;
  32. _ref = _iterator[_i++];
  33. } else {
  34. _i = _iterator.next();
  35. if (_i.done) break;
  36. _ref = _i.value;
  37. }
  38. var type = _ref;
  39. var aliases = t.FLIPPED_ALIAS_KEYS[type];
  40. if (aliases) {
  41. for (var _iterator2 = aliases, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) {
  42. var _ref2;
  43. if (_isArray2) {
  44. if (_i2 >= _iterator2.length) break;
  45. _ref2 = _iterator2[_i2++];
  46. } else {
  47. _i2 = _iterator2.next();
  48. if (_i2.done) break;
  49. _ref2 = _i2.value;
  50. }
  51. var alias = _ref2;
  52. add(alias, obj[type]);
  53. }
  54. } else {
  55. add(type, obj[type]);
  56. }
  57. }
  58. return newObj;
  59. }
  60. var expandedParens = expandAliases(parens);
  61. var expandedWhitespaceNodes = expandAliases(_whitespace2.default.nodes);
  62. var expandedWhitespaceList = expandAliases(_whitespace2.default.list);
  63. function find(obj, node, parent, printStack) {
  64. var fn = obj[node.type];
  65. return fn ? fn(node, parent, printStack) : null;
  66. }
  67. function isOrHasCallExpression(node) {
  68. if (t.isCallExpression(node)) {
  69. return true;
  70. }
  71. if (t.isMemberExpression(node)) {
  72. return isOrHasCallExpression(node.object) || !node.computed && isOrHasCallExpression(node.property);
  73. } else {
  74. return false;
  75. }
  76. }
  77. function needsWhitespace(node, parent, type) {
  78. if (!node) return 0;
  79. if (t.isExpressionStatement(node)) {
  80. node = node.expression;
  81. }
  82. var linesInfo = find(expandedWhitespaceNodes, node, parent);
  83. if (!linesInfo) {
  84. var items = find(expandedWhitespaceList, node, parent);
  85. if (items) {
  86. for (var i = 0; i < items.length; i++) {
  87. linesInfo = needsWhitespace(items[i], node, type);
  88. if (linesInfo) break;
  89. }
  90. }
  91. }
  92. return linesInfo && linesInfo[type] || 0;
  93. }
  94. function needsWhitespaceBefore(node, parent) {
  95. return needsWhitespace(node, parent, "before");
  96. }
  97. function needsWhitespaceAfter(node, parent) {
  98. return needsWhitespace(node, parent, "after");
  99. }
  100. function needsParens(node, parent, printStack) {
  101. if (!parent) return false;
  102. if (t.isNewExpression(parent) && parent.callee === node) {
  103. if (isOrHasCallExpression(node)) return true;
  104. }
  105. return find(expandedParens, node, parent, printStack);
  106. }