| 1 |
import { |
| 2 |
blockStatement, |
| 3 |
cloneNode, |
| 4 |
emptyStatement, |
| 5 |
expressionStatement, |
| 6 |
identifier, |
| 7 |
isStatement, |
| 8 |
isStringLiteral, |
| 9 |
stringLiteral, |
| 10 |
validate, |
| 11 |
} from "@babel/types"; |
| 12 |
import type * as t from "@babel/types"; |
| 13 |
|
| 14 |
import type { TemplateReplacements } from "./options"; |
| 15 |
import type { Metadata, Placeholder } from "./parse"; |
| 16 |
|
| 17 |
export default function populatePlaceholders( |
| 18 |
metadata: Metadata, |
| 19 |
replacements: TemplateReplacements, |
| 20 |
): t.File { |
| 21 |
const ast = cloneNode(metadata.ast); |
| 22 |
|
| 23 |
if (replacements) { |
| 24 |
metadata.placeholders.forEach(placeholder => { |
| 25 |
if ( |
| 26 |
!Object.prototype.hasOwnProperty.call(replacements, placeholder.name) |
| 27 |
) { |
| 28 |
const placeholderName = placeholder.name; |
| 29 |
|
| 30 |
throw new Error( |
| 31 |
`Error: No substitution given for "${placeholderName}". If this is not meant to be a |
| 32 |
placeholder you may want to consider passing one of the following options to @babel/template: |
| 33 |
- { placeholderPattern: false, placeholderWhitelist: new Set(['${placeholderName}'])} |
| 34 |
- { placeholderPattern: /^${placeholderName}$/ }`, |
| 35 |
); |
| 36 |
} |
| 37 |
}); |
| 38 |
Object.keys(replacements).forEach(key => { |
| 39 |
if (!metadata.placeholderNames.has(key)) { |
| 40 |
throw new Error(`Unknown substitution "${key}" given`); |
| 41 |
} |
| 42 |
}); |
| 43 |
} |
| 44 |
|
| 45 |
// Process in reverse order so AST mutation doesn't change indices that |
| 46 |
// will be needed for later calls to `placeholder.resolve()`. |
| 47 |
metadata.placeholders |
| 48 |
.slice() |
| 49 |
.reverse() |
| 50 |
.forEach(placeholder => { |
| 51 |
try { |
| 52 |
applyReplacement( |
| 53 |
placeholder, |
| 54 |
ast, |
| 55 |
(replacements && replacements[placeholder.name]) || null, |
| 56 |
); |
| 57 |
} catch (e) { |
| 58 |
e.message = `@babel/template placeholder "${placeholder.name}": ${e.message}`; |
| 59 |
throw e; |
| 60 |
} |
| 61 |
}); |
| 62 |
|
| 63 |
return ast; |
| 64 |
} |
| 65 |
|
| 66 |
function applyReplacement( |
| 67 |
placeholder: Placeholder, |
| 68 |
ast: t.File, |
| 69 |
replacement: any, |
| 70 |
) { |
| 71 |
// Track inserted nodes and clone them if they are inserted more than |
| 72 |
// once to avoid injecting the same node multiple times. |
| 73 |
if (placeholder.isDuplicate) { |
| 74 |
if (Array.isArray(replacement)) { |
| 75 |
replacement = replacement.map(node => cloneNode(node)); |
| 76 |
} else if (typeof replacement === "object") { |
| 77 |
replacement = cloneNode(replacement); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
const { parent, key, index } = placeholder.resolve(ast); |
| 82 |
|
| 83 |
if (placeholder.type === "string") { |
| 84 |
if (typeof replacement === "string") { |
| 85 |
replacement = stringLiteral(replacement); |
| 86 |
} |
| 87 |
if (!replacement || !isStringLiteral(replacement)) { |
| 88 |
throw new Error("Expected string substitution"); |
| 89 |
} |
| 90 |
} else if (placeholder.type === "statement") { |
| 91 |
if (index === undefined) { |
| 92 |
if (!replacement) { |
| 93 |
replacement = emptyStatement(); |
| 94 |
} else if (Array.isArray(replacement)) { |
| 95 |
replacement = blockStatement(replacement); |
| 96 |
} else if (typeof replacement === "string") { |
| 97 |
replacement = expressionStatement(identifier(replacement)); |
| 98 |
} else if (!isStatement(replacement)) { |
| 99 |
replacement = expressionStatement(replacement); |
| 100 |
} |
| 101 |
} else { |
| 102 |
if (replacement && !Array.isArray(replacement)) { |
| 103 |
if (typeof replacement === "string") { |
| 104 |
replacement = identifier(replacement); |
| 105 |
} |
| 106 |
if (!isStatement(replacement)) { |
| 107 |
replacement = expressionStatement(replacement); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
} else if (placeholder.type === "param") { |
| 112 |
if (typeof replacement === "string") { |
| 113 |
replacement = identifier(replacement); |
| 114 |
} |
| 115 |
|
| 116 |
if (index === undefined) throw new Error("Assertion failure."); |
| 117 |
} else { |
| 118 |
if (typeof replacement === "string") { |
| 119 |
replacement = identifier(replacement); |
| 120 |
} |
| 121 |
if (Array.isArray(replacement)) { |
| 122 |
throw new Error("Cannot replace single expression with an array."); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
if (index === undefined) { |
| 127 |
validate(parent, key, replacement); |
| 128 |
|
| 129 |
(parent as any)[key] = replacement; |
| 130 |
} else { |
| 131 |
const items: Array<t.Node> = (parent as any)[key].slice(); |
| 132 |
|
| 133 |
if (placeholder.type === "statement" || placeholder.type === "param") { |
| 134 |
if (replacement == null) { |
| 135 |
items.splice(index, 1); |
| 136 |
} else if (Array.isArray(replacement)) { |
| 137 |
items.splice(index, 1, ...replacement); |
| 138 |
} else { |
| 139 |
items[index] = replacement; |
| 140 |
} |
| 141 |
} else { |
| 142 |
items[index] = replacement; |
| 143 |
} |
| 144 |
|
| 145 |
validate(parent, key, items); |
| 146 |
(parent as any)[key] = items; |
| 147 |
} |
| 148 |
} |