populate.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 (!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. var _ref;
  39. applyReplacement(placeholder, ast, (_ref = replacements && replacements[placeholder.name]) != null ? _ref : null);
  40. } catch (e) {
  41. e.message = `@babel/template placeholder "${placeholder.name}": ${e.message}`;
  42. throw e;
  43. }
  44. });
  45. return ast;
  46. }
  47. function applyReplacement(placeholder, ast, replacement) {
  48. if (placeholder.isDuplicate) {
  49. if (Array.isArray(replacement)) {
  50. replacement = replacement.map(node => cloneNode(node));
  51. } else if (typeof replacement === "object") {
  52. replacement = cloneNode(replacement);
  53. }
  54. }
  55. const {
  56. parent,
  57. key,
  58. index
  59. } = placeholder.resolve(ast);
  60. if (placeholder.type === "string") {
  61. if (typeof replacement === "string") {
  62. replacement = stringLiteral(replacement);
  63. }
  64. if (!replacement || !isStringLiteral(replacement)) {
  65. throw new Error("Expected string substitution");
  66. }
  67. } else if (placeholder.type === "statement") {
  68. if (index === undefined) {
  69. if (!replacement) {
  70. replacement = emptyStatement();
  71. } else if (Array.isArray(replacement)) {
  72. replacement = blockStatement(replacement);
  73. } else if (typeof replacement === "string") {
  74. replacement = expressionStatement(identifier(replacement));
  75. } else if (!isStatement(replacement)) {
  76. replacement = expressionStatement(replacement);
  77. }
  78. } else {
  79. if (replacement && !Array.isArray(replacement)) {
  80. if (typeof replacement === "string") {
  81. replacement = identifier(replacement);
  82. }
  83. if (!isStatement(replacement)) {
  84. replacement = expressionStatement(replacement);
  85. }
  86. }
  87. }
  88. } else if (placeholder.type === "param") {
  89. if (typeof replacement === "string") {
  90. replacement = identifier(replacement);
  91. }
  92. if (index === undefined) throw new Error("Assertion failure.");
  93. } else {
  94. if (typeof replacement === "string") {
  95. replacement = identifier(replacement);
  96. }
  97. if (Array.isArray(replacement)) {
  98. throw new Error("Cannot replace single expression with an array.");
  99. }
  100. }
  101. function set(parent, key, value) {
  102. const node = parent[key];
  103. parent[key] = value;
  104. if (node.type === "Identifier" || node.type === "Placeholder") {
  105. if (node.typeAnnotation) {
  106. value.typeAnnotation = node.typeAnnotation;
  107. }
  108. if (node.optional) {
  109. value.optional = node.optional;
  110. }
  111. if (node.decorators) {
  112. value.decorators = node.decorators;
  113. }
  114. }
  115. }
  116. if (index === undefined) {
  117. validate(parent, key, replacement);
  118. set(parent, key, replacement);
  119. } else {
  120. const items = parent[key].slice();
  121. if (placeholder.type === "statement" || placeholder.type === "param") {
  122. if (replacement == null) {
  123. items.splice(index, 1);
  124. } else if (Array.isArray(replacement)) {
  125. items.splice(index, 1, ...replacement);
  126. } else {
  127. set(items, index, replacement);
  128. }
  129. } else {
  130. set(items, index, replacement);
  131. }
  132. validate(parent, key, items);
  133. parent[key] = items;
  134. }
  135. }
  136. //# sourceMappingURL=populate.js.map