populate.js 4.3 KB

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