You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.5 KiB

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