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.

178 lines
5.2 KiB

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = parseAndBuildMetadata;
  6. var t = _interopRequireWildcard(require("@babel/types"));
  7. var _parser = require("@babel/parser");
  8. var _codeFrame = require("@babel/code-frame");
  9. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  10. 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; }
  11. const PATTERN = /^[_$A-Z0-9]+$/;
  12. function parseAndBuildMetadata(formatter, code, opts) {
  13. const {
  14. placeholderWhitelist,
  15. placeholderPattern,
  16. preserveComments,
  17. syntacticPlaceholders
  18. } = opts;
  19. const ast = parseWithCodeFrame(code, opts.parser, syntacticPlaceholders);
  20. t.removePropertiesDeep(ast, {
  21. preserveComments
  22. });
  23. formatter.validate(ast);
  24. const syntactic = {
  25. placeholders: [],
  26. placeholderNames: new Set()
  27. };
  28. const legacy = {
  29. placeholders: [],
  30. placeholderNames: new Set()
  31. };
  32. const isLegacyRef = {
  33. value: undefined
  34. };
  35. t.traverse(ast, placeholderVisitorHandler, {
  36. syntactic,
  37. legacy,
  38. isLegacyRef,
  39. placeholderWhitelist,
  40. placeholderPattern,
  41. syntacticPlaceholders
  42. });
  43. return Object.assign({
  44. ast
  45. }, isLegacyRef.value ? legacy : syntactic);
  46. }
  47. function placeholderVisitorHandler(node, ancestors, state) {
  48. var _state$placeholderWhi;
  49. let name;
  50. if (t.isPlaceholder(node)) {
  51. if (state.syntacticPlaceholders === false) {
  52. throw new Error("%%foo%%-style placeholders can't be used when " + "'.syntacticPlaceholders' is false.");
  53. } else {
  54. name = node.name.name;
  55. state.isLegacyRef.value = false;
  56. }
  57. } else if (state.isLegacyRef.value === false || state.syntacticPlaceholders) {
  58. return;
  59. } else if (t.isIdentifier(node) || t.isJSXIdentifier(node)) {
  60. name = node.name;
  61. state.isLegacyRef.value = true;
  62. } else if (t.isStringLiteral(node)) {
  63. name = node.value;
  64. state.isLegacyRef.value = true;
  65. } else {
  66. return;
  67. }
  68. if (!state.isLegacyRef.value && (state.placeholderPattern != null || state.placeholderWhitelist != null)) {
  69. throw new Error("'.placeholderWhitelist' and '.placeholderPattern' aren't compatible" + " with '.syntacticPlaceholders: true'");
  70. }
  71. if (state.isLegacyRef.value && (state.placeholderPattern === false || !(state.placeholderPattern || PATTERN).test(name)) && !((_state$placeholderWhi = state.placeholderWhitelist) == null ? void 0 : _state$placeholderWhi.has(name))) {
  72. return;
  73. }
  74. ancestors = ancestors.slice();
  75. const {
  76. node: parent,
  77. key
  78. } = ancestors[ancestors.length - 1];
  79. let type;
  80. if (t.isStringLiteral(node) || t.isPlaceholder(node, {
  81. expectedNode: "StringLiteral"
  82. })) {
  83. type = "string";
  84. } else if (t.isNewExpression(parent) && key === "arguments" || t.isCallExpression(parent) && key === "arguments" || t.isFunction(parent) && key === "params") {
  85. type = "param";
  86. } else if (t.isExpressionStatement(parent) && !t.isPlaceholder(node)) {
  87. type = "statement";
  88. ancestors = ancestors.slice(0, -1);
  89. } else if (t.isStatement(node) && t.isPlaceholder(node)) {
  90. type = "statement";
  91. } else {
  92. type = "other";
  93. }
  94. const {
  95. placeholders,
  96. placeholderNames
  97. } = state.isLegacyRef.value ? state.legacy : state.syntactic;
  98. placeholders.push({
  99. name,
  100. type,
  101. resolve: ast => resolveAncestors(ast, ancestors),
  102. isDuplicate: placeholderNames.has(name)
  103. });
  104. placeholderNames.add(name);
  105. }
  106. function resolveAncestors(ast, ancestors) {
  107. let parent = ast;
  108. for (let i = 0; i < ancestors.length - 1; i++) {
  109. const {
  110. key,
  111. index
  112. } = ancestors[i];
  113. if (index === undefined) {
  114. parent = parent[key];
  115. } else {
  116. parent = parent[key][index];
  117. }
  118. }
  119. const {
  120. key,
  121. index
  122. } = ancestors[ancestors.length - 1];
  123. return {
  124. parent,
  125. key,
  126. index
  127. };
  128. }
  129. function parseWithCodeFrame(code, parserOpts, syntacticPlaceholders) {
  130. const plugins = (parserOpts.plugins || []).slice();
  131. if (syntacticPlaceholders !== false) {
  132. plugins.push("placeholders");
  133. }
  134. parserOpts = Object.assign({
  135. allowReturnOutsideFunction: true,
  136. allowSuperOutsideMethod: true,
  137. sourceType: "module"
  138. }, parserOpts, {
  139. plugins
  140. });
  141. try {
  142. return (0, _parser.parse)(code, parserOpts);
  143. } catch (err) {
  144. const loc = err.loc;
  145. if (loc) {
  146. err.message += "\n" + (0, _codeFrame.codeFrameColumns)(code, {
  147. start: loc
  148. });
  149. err.code = "BABEL_TEMPLATE_PARSE_ERROR";
  150. }
  151. throw err;
  152. }
  153. }