1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = populatePlaceholders;
|
---|
7 | var _t = require("@babel/types");
|
---|
8 | const {
|
---|
9 | blockStatement,
|
---|
10 | cloneNode,
|
---|
11 | emptyStatement,
|
---|
12 | expressionStatement,
|
---|
13 | identifier,
|
---|
14 | isStatement,
|
---|
15 | isStringLiteral,
|
---|
16 | stringLiteral,
|
---|
17 | validate
|
---|
18 | } = _t;
|
---|
19 | function populatePlaceholders(metadata, replacements) {
|
---|
20 | const ast = cloneNode(metadata.ast);
|
---|
21 | if (replacements) {
|
---|
22 | metadata.placeholders.forEach(placeholder => {
|
---|
23 | if (!hasOwnProperty.call(replacements, placeholder.name)) {
|
---|
24 | const placeholderName = placeholder.name;
|
---|
25 | throw new Error(`Error: No substitution given for "${placeholderName}". If this is not meant to be a
|
---|
26 | placeholder you may want to consider passing one of the following options to @babel/template:
|
---|
27 | - { placeholderPattern: false, placeholderWhitelist: new Set(['${placeholderName}'])}
|
---|
28 | - { placeholderPattern: /^${placeholderName}$/ }`);
|
---|
29 | }
|
---|
30 | });
|
---|
31 | Object.keys(replacements).forEach(key => {
|
---|
32 | if (!metadata.placeholderNames.has(key)) {
|
---|
33 | throw new Error(`Unknown substitution "${key}" given`);
|
---|
34 | }
|
---|
35 | });
|
---|
36 | }
|
---|
37 | metadata.placeholders.slice().reverse().forEach(placeholder => {
|
---|
38 | try {
|
---|
39 | applyReplacement(placeholder, ast, replacements && replacements[placeholder.name] || 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") {
|
---|
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 |
|
---|
137 | //# sourceMappingURL=populate.js.map
|
---|