index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.needsParens = needsParens;
  6. exports.needsWhitespace = needsWhitespace;
  7. exports.needsWhitespaceAfter = needsWhitespaceAfter;
  8. exports.needsWhitespaceBefore = needsWhitespaceBefore;
  9. var whitespace = require("./whitespace");
  10. var parens = require("./parentheses");
  11. var _t = require("@babel/types");
  12. const {
  13. FLIPPED_ALIAS_KEYS,
  14. isCallExpression,
  15. isExpressionStatement,
  16. isMemberExpression,
  17. isNewExpression
  18. } = _t;
  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 = 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 (isCallExpression(node)) {
  49. return true;
  50. }
  51. return isMemberExpression(node) && isOrHasCallExpression(node.object);
  52. }
  53. function needsWhitespace(node, parent, type) {
  54. if (!node) return 0;
  55. if (isExpressionStatement(node)) {
  56. node = node.expression;
  57. }
  58. let linesInfo = find(expandedWhitespaceNodes, node, parent);
  59. if (!linesInfo) {
  60. const items = find(expandedWhitespaceList, node, parent);
  61. if (items) {
  62. for (let i = 0; i < items.length; i++) {
  63. linesInfo = needsWhitespace(items[i], node, type);
  64. if (linesInfo) break;
  65. }
  66. }
  67. }
  68. if (typeof linesInfo === "object" && linesInfo !== null) {
  69. return linesInfo[type] || 0;
  70. }
  71. return 0;
  72. }
  73. function needsWhitespaceBefore(node, parent) {
  74. return needsWhitespace(node, parent, "before");
  75. }
  76. function needsWhitespaceAfter(node, parent) {
  77. return needsWhitespace(node, parent, "after");
  78. }
  79. function needsParens(node, parent, printStack) {
  80. if (!parent) return false;
  81. if (isNewExpression(parent) && parent.callee === node) {
  82. if (isOrHasCallExpression(node)) return true;
  83. }
  84. return find(expandedParens, node, parent, printStack);
  85. }