populate.js 3.8 KB

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