index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.needsWhitespace = needsWhitespace;
  6. exports.needsWhitespaceBefore = needsWhitespaceBefore;
  7. exports.needsWhitespaceAfter = needsWhitespaceAfter;
  8. exports.needsParens = needsParens;
  9. var whitespace = _interopRequireWildcard(require("./whitespace"));
  10. var parens = _interopRequireWildcard(require("./parentheses"));
  11. function t() {
  12. const data = _interopRequireWildcard(require("@babel/types"));
  13. t = function () {
  14. return data;
  15. };
  16. return data;
  17. }
  18. 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)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
  19. function expandAliases(obj) {
  20. const newObj = {};
  21. function add(type, func) {
  22. const fn = newObj[type];
  23. newObj[type] = fn ? function (node, parent, stack) {
  24. const result = fn(node, parent, stack);
  25. return result == null ? func(node, parent, stack) : result;
  26. } : func;
  27. }
  28. for (const type of Object.keys(obj)) {
  29. const aliases = t().FLIPPED_ALIAS_KEYS[type];
  30. if (aliases) {
  31. for (const alias of aliases) {
  32. add(alias, obj[type]);
  33. }
  34. } else {
  35. add(type, obj[type]);
  36. }
  37. }
  38. return newObj;
  39. }
  40. const expandedParens = expandAliases(parens);
  41. const expandedWhitespaceNodes = expandAliases(whitespace.nodes);
  42. const expandedWhitespaceList = expandAliases(whitespace.list);
  43. function find(obj, node, parent, printStack) {
  44. const fn = obj[node.type];
  45. return fn ? fn(node, parent, printStack) : null;
  46. }
  47. function isOrHasCallExpression(node) {
  48. if (t().isCallExpression(node)) {
  49. return true;
  50. }
  51. if (t().isMemberExpression(node)) {
  52. return isOrHasCallExpression(node.object) || !node.computed && isOrHasCallExpression(node.property);
  53. } else {
  54. return false;
  55. }
  56. }
  57. function needsWhitespace(node, parent, type) {
  58. if (!node) return 0;
  59. if (t().isExpressionStatement(node)) {
  60. node = node.expression;
  61. }
  62. let linesInfo = find(expandedWhitespaceNodes, node, parent);
  63. if (!linesInfo) {
  64. const items = find(expandedWhitespaceList, node, parent);
  65. if (items) {
  66. for (let i = 0; i < items.length; i++) {
  67. linesInfo = needsWhitespace(items[i], node, type);
  68. if (linesInfo) break;
  69. }
  70. }
  71. }
  72. if (typeof linesInfo === "object" && linesInfo !== null) {
  73. return linesInfo[type] || 0;
  74. }
  75. return 0;
  76. }
  77. function needsWhitespaceBefore(node, parent) {
  78. return needsWhitespace(node, parent, "before");
  79. }
  80. function needsWhitespaceAfter(node, parent) {
  81. return needsWhitespace(node, parent, "after");
  82. }
  83. function needsParens(node, parent, printStack) {
  84. if (!parent) return false;
  85. if (t().isNewExpression(parent) && parent.callee === node) {
  86. if (isOrHasCallExpression(node)) return true;
  87. }
  88. return find(expandedParens, node, parent, printStack);
  89. }