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