web 3d图形渲染器
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.

106 lines
3.4 KiB

  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. return t.isMemberExpression(node) && isOrHasCallExpression(node.object);
  47. }
  48. function needsWhitespace(node, parent, type) {
  49. if (!node) return 0;
  50. if (t.isExpressionStatement(node)) {
  51. node = node.expression;
  52. }
  53. let linesInfo = find(expandedWhitespaceNodes, node, parent);
  54. if (!linesInfo) {
  55. const items = find(expandedWhitespaceList, node, parent);
  56. if (items) {
  57. for (let i = 0; i < items.length; i++) {
  58. linesInfo = needsWhitespace(items[i], node, type);
  59. if (linesInfo) break;
  60. }
  61. }
  62. }
  63. if (typeof linesInfo === "object" && linesInfo !== null) {
  64. return linesInfo[type] || 0;
  65. }
  66. return 0;
  67. }
  68. function needsWhitespaceBefore(node, parent) {
  69. return needsWhitespace(node, parent, "before");
  70. }
  71. function needsWhitespaceAfter(node, parent) {
  72. return needsWhitespace(node, parent, "after");
  73. }
  74. function needsParens(node, parent, printStack) {
  75. if (!parent) return false;
  76. if (t.isNewExpression(parent) && parent.callee === node) {
  77. if (isOrHasCallExpression(node)) return true;
  78. }
  79. return find(expandedParens, node, parent, printStack);
  80. }