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.

126 lines
4.6 KiB

5 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = populatePlaceholders;
  6. var t = _interopRequireWildcard(require("@babel/types"));
  7. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  8. 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; }
  9. function populatePlaceholders(metadata, replacements) {
  10. const ast = t.cloneNode(metadata.ast);
  11. if (replacements) {
  12. metadata.placeholders.forEach(placeholder => {
  13. if (!Object.prototype.hasOwnProperty.call(replacements, placeholder.name)) {
  14. const placeholderName = placeholder.name;
  15. throw new Error(`Error: No substitution given for "${placeholderName}". If this is not meant to be a
  16. placeholder you may want to consider passing one of the following options to @babel/template:
  17. - { placeholderPattern: false, placeholderWhitelist: new Set(['${placeholderName}'])}
  18. - { placeholderPattern: /^${placeholderName}$/ }`);
  19. }
  20. });
  21. Object.keys(replacements).forEach(key => {
  22. if (!metadata.placeholderNames.has(key)) {
  23. throw new Error(`Unknown substitution "${key}" given`);
  24. }
  25. });
  26. }
  27. metadata.placeholders.slice().reverse().forEach(placeholder => {
  28. try {
  29. applyReplacement(placeholder, ast, replacements && replacements[placeholder.name] || null);
  30. } catch (e) {
  31. e.message = `@babel/template placeholder "${placeholder.name}": ${e.message}`;
  32. throw e;
  33. }
  34. });
  35. return ast;
  36. }
  37. function applyReplacement(placeholder, ast, replacement) {
  38. if (placeholder.isDuplicate) {
  39. if (Array.isArray(replacement)) {
  40. replacement = replacement.map(node => t.cloneNode(node));
  41. } else if (typeof replacement === "object") {
  42. replacement = t.cloneNode(replacement);
  43. }
  44. }
  45. const {
  46. parent,
  47. key,
  48. index
  49. } = placeholder.resolve(ast);
  50. if (placeholder.type === "string") {
  51. if (typeof replacement === "string") {
  52. replacement = t.stringLiteral(replacement);
  53. }
  54. if (!replacement || !t.isStringLiteral(replacement)) {
  55. throw new Error("Expected string substitution");
  56. }
  57. } else if (placeholder.type === "statement") {
  58. if (index === undefined) {
  59. if (!replacement) {
  60. replacement = t.emptyStatement();
  61. } else if (Array.isArray(replacement)) {
  62. replacement = t.blockStatement(replacement);
  63. } else if (typeof replacement === "string") {
  64. replacement = t.expressionStatement(t.identifier(replacement));
  65. } else if (!t.isStatement(replacement)) {
  66. replacement = t.expressionStatement(replacement);
  67. }
  68. } else {
  69. if (replacement && !Array.isArray(replacement)) {
  70. if (typeof replacement === "string") {
  71. replacement = t.identifier(replacement);
  72. }
  73. if (!t.isStatement(replacement)) {
  74. replacement = t.expressionStatement(replacement);
  75. }
  76. }
  77. }
  78. } else if (placeholder.type === "param") {
  79. if (typeof replacement === "string") {
  80. replacement = t.identifier(replacement);
  81. }
  82. if (index === undefined) throw new Error("Assertion failure.");
  83. } else {
  84. if (typeof replacement === "string") {
  85. replacement = t.identifier(replacement);
  86. }
  87. if (Array.isArray(replacement)) {
  88. throw new Error("Cannot replace single expression with an array.");
  89. }
  90. }
  91. if (index === undefined) {
  92. t.validate(parent, key, replacement);
  93. parent[key] = replacement;
  94. } else {
  95. const items = parent[key].slice();
  96. if (placeholder.type === "statement" || placeholder.type === "param") {
  97. if (replacement == null) {
  98. items.splice(index, 1);
  99. } else if (Array.isArray(replacement)) {
  100. items.splice(index, 1, ...replacement);
  101. } else {
  102. items[index] = replacement;
  103. }
  104. } else {
  105. items[index] = replacement;
  106. }
  107. t.validate(parent, key, items);
  108. parent[key] = items;
  109. }
  110. }