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.

170 lines
5.0 KiB

5 years ago
  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 ast = parseWithCodeFrame(code, opts.parser);
  14. const {
  15. placeholderWhitelist,
  16. placeholderPattern,
  17. preserveComments,
  18. syntacticPlaceholders
  19. } = opts;
  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. let name;
  49. if (t.isPlaceholder(node)) {
  50. if (state.syntacticPlaceholders === false) {
  51. throw new Error("%%foo%%-style placeholders can't be used when " + "'.syntacticPlaceholders' is false.");
  52. } else {
  53. name = node.name.name;
  54. state.isLegacyRef.value = false;
  55. }
  56. } else if (state.isLegacyRef.value === false || state.syntacticPlaceholders) {
  57. return;
  58. } else if (t.isIdentifier(node) || t.isJSXIdentifier(node)) {
  59. name = node.name;
  60. state.isLegacyRef.value = true;
  61. } else if (t.isStringLiteral(node)) {
  62. name = node.value;
  63. state.isLegacyRef.value = true;
  64. } else {
  65. return;
  66. }
  67. if (!state.isLegacyRef.value && (state.placeholderPattern != null || state.placeholderWhitelist != null)) {
  68. throw new Error("'.placeholderWhitelist' and '.placeholderPattern' aren't compatible" + " with '.syntacticPlaceholders: true'");
  69. }
  70. if (state.isLegacyRef.value && (state.placeholderPattern === false || !(state.placeholderPattern || PATTERN).test(name)) && (!state.placeholderWhitelist || !state.placeholderWhitelist.has(name))) {
  71. return;
  72. }
  73. ancestors = ancestors.slice();
  74. const {
  75. node: parent,
  76. key
  77. } = ancestors[ancestors.length - 1];
  78. let type;
  79. if (t.isStringLiteral(node) || t.isPlaceholder(node, {
  80. expectedNode: "StringLiteral"
  81. })) {
  82. type = "string";
  83. } else if (t.isNewExpression(parent) && key === "arguments" || t.isCallExpression(parent) && key === "arguments" || t.isFunction(parent) && key === "params") {
  84. type = "param";
  85. } else if (t.isExpressionStatement(parent) && !t.isPlaceholder(node)) {
  86. type = "statement";
  87. ancestors = ancestors.slice(0, -1);
  88. } else if (t.isStatement(node) && t.isPlaceholder(node)) {
  89. type = "statement";
  90. } else {
  91. type = "other";
  92. }
  93. const {
  94. placeholders,
  95. placeholderNames
  96. } = state.isLegacyRef.value ? state.legacy : state.syntactic;
  97. placeholders.push({
  98. name,
  99. type,
  100. resolve: ast => resolveAncestors(ast, ancestors),
  101. isDuplicate: placeholderNames.has(name)
  102. });
  103. placeholderNames.add(name);
  104. }
  105. function resolveAncestors(ast, ancestors) {
  106. let parent = ast;
  107. for (let i = 0; i < ancestors.length - 1; i++) {
  108. const {
  109. key,
  110. index
  111. } = ancestors[i];
  112. if (index === undefined) {
  113. parent = parent[key];
  114. } else {
  115. parent = parent[key][index];
  116. }
  117. }
  118. const {
  119. key,
  120. index
  121. } = ancestors[ancestors.length - 1];
  122. return {
  123. parent,
  124. key,
  125. index
  126. };
  127. }
  128. function parseWithCodeFrame(code, parserOpts) {
  129. parserOpts = Object.assign({
  130. allowReturnOutsideFunction: true,
  131. allowSuperOutsideMethod: true,
  132. sourceType: "module"
  133. }, parserOpts, {
  134. plugins: (parserOpts.plugins || []).concat("placeholders")
  135. });
  136. try {
  137. return (0, _parser.parse)(code, parserOpts);
  138. } catch (err) {
  139. const loc = err.loc;
  140. if (loc) {
  141. err.message += "\n" + (0, _codeFrame.codeFrameColumns)(code, {
  142. start: loc
  143. });
  144. err.code = "BABEL_TEMPLATE_PARSE_ERROR";
  145. }
  146. throw err;
  147. }
  148. }