parse.js 4.4 KB

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