source: frontend/node_modules/@babel/template/lib/populate.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 4.3 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = populatePlaceholders;
7var _t = require("@babel/types");
8const {
9 blockStatement,
10 cloneNode,
11 emptyStatement,
12 expressionStatement,
13 identifier,
14 isStatement,
15 isStringLiteral,
16 stringLiteral,
17 validate
18} = _t;
19function 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 var _ref;
40 applyReplacement(placeholder, ast, (_ref = replacements && replacements[placeholder.name]) != null ? _ref : null);
41 } catch (e) {
42 e.message = `@babel/template placeholder "${placeholder.name}": ${e.message}`;
43 throw e;
44 }
45 });
46 return ast;
47}
48function applyReplacement(placeholder, ast, replacement) {
49 if (placeholder.isDuplicate) {
50 if (Array.isArray(replacement)) {
51 replacement = replacement.map(node => cloneNode(node));
52 } else if (typeof replacement === "object") {
53 replacement = cloneNode(replacement);
54 }
55 }
56 const {
57 parent,
58 key,
59 index
60 } = placeholder.resolve(ast);
61 if (placeholder.type === "string") {
62 if (typeof replacement === "string") {
63 replacement = stringLiteral(replacement);
64 }
65 if (!replacement || !isStringLiteral(replacement)) {
66 throw new Error("Expected string substitution");
67 }
68 } else if (placeholder.type === "statement") {
69 if (index === undefined) {
70 if (!replacement) {
71 replacement = emptyStatement();
72 } else if (Array.isArray(replacement)) {
73 replacement = blockStatement(replacement);
74 } else if (typeof replacement === "string") {
75 replacement = expressionStatement(identifier(replacement));
76 } else if (!isStatement(replacement)) {
77 replacement = expressionStatement(replacement);
78 }
79 } else {
80 if (replacement && !Array.isArray(replacement)) {
81 if (typeof replacement === "string") {
82 replacement = identifier(replacement);
83 }
84 if (!isStatement(replacement)) {
85 replacement = expressionStatement(replacement);
86 }
87 }
88 }
89 } else if (placeholder.type === "param") {
90 if (typeof replacement === "string") {
91 replacement = identifier(replacement);
92 }
93 if (index === undefined) throw new Error("Assertion failure.");
94 } else {
95 if (typeof replacement === "string") {
96 replacement = identifier(replacement);
97 }
98 if (Array.isArray(replacement)) {
99 throw new Error("Cannot replace single expression with an array.");
100 }
101 }
102 function set(parent, key, value) {
103 const node = parent[key];
104 parent[key] = value;
105 if (node.type === "Identifier" || node.type === "Placeholder") {
106 if (node.typeAnnotation) {
107 value.typeAnnotation = node.typeAnnotation;
108 }
109 if (node.optional) {
110 value.optional = node.optional;
111 }
112 if (node.decorators) {
113 value.decorators = node.decorators;
114 }
115 }
116 }
117 if (index === undefined) {
118 validate(parent, key, replacement);
119 set(parent, key, replacement);
120 } else {
121 const items = parent[key].slice();
122 if (placeholder.type === "statement" || placeholder.type === "param") {
123 if (replacement == null) {
124 items.splice(index, 1);
125 } else if (Array.isArray(replacement)) {
126 items.splice(index, 1, ...replacement);
127 } else {
128 set(items, index, replacement);
129 }
130 } else {
131 set(items, index, replacement);
132 }
133 validate(parent, key, items);
134 parent[key] = items;
135 }
136}
137
138//# sourceMappingURL=populate.js.map
Note: See TracBrowser for help on using the repository browser.