parse.js 4.4 KB

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