parse.js 4.8 KB

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