| 1 |
import type { ParserOptions as ParserOpts } from "@babel/parser"; |
| 2 |
|
| 3 |
export type { ParserOpts }; |
| 4 |
|
| 5 |
/** |
| 6 |
* These are the options that 'babel-template' actually accepts and typechecks |
| 7 |
* when called. All other options are passed through to the parser. |
| 8 |
*/ |
| 9 |
export type PublicOpts = { |
| 10 |
/** |
| 11 |
* A set of placeholder names to automatically accept, ignoring the given |
| 12 |
* pattern entirely. |
| 13 |
* |
| 14 |
* This option can be used when using %%foo%% style placeholders. |
| 15 |
*/ |
| 16 |
placeholderWhitelist?: Set<string>; |
| 17 |
/** |
| 18 |
* A pattern to search for when looking for Identifier and StringLiteral |
| 19 |
* nodes that can be replaced. |
| 20 |
* |
| 21 |
* 'false' will disable placeholder searching entirely, leaving only the |
| 22 |
* 'placeholderWhitelist' value to find replacements. |
| 23 |
* |
| 24 |
* Defaults to /^[_$A-Z0-9]+$/. |
| 25 |
* |
| 26 |
* This option can be used when using %%foo%% style placeholders. |
| 27 |
*/ |
| 28 |
placeholderPattern?: RegExp | false; |
| 29 |
/** |
| 30 |
* 'true' to pass through comments from the template into the resulting AST, |
| 31 |
* or 'false' to automatically discard comments. Defaults to 'false'. |
| 32 |
*/ |
| 33 |
preserveComments?: boolean; |
| 34 |
/** |
| 35 |
* 'true' to use %%foo%% style placeholders, 'false' to use legacy placeholders |
| 36 |
* described by placeholderPattern or placeholderWhitelist. |
| 37 |
* When it is not set, it behaves as 'true' if there are syntactic placeholders, |
| 38 |
* otherwise as 'false'. |
| 39 |
*/ |
| 40 |
syntacticPlaceholders?: boolean | null; |
| 41 |
}; |
| 42 |
|
| 43 |
export type TemplateOpts = { |
| 44 |
parser: ParserOpts; |
| 45 |
placeholderWhitelist?: Set<string>; |
| 46 |
placeholderPattern?: RegExp | false; |
| 47 |
preserveComments?: boolean; |
| 48 |
syntacticPlaceholders?: boolean; |
| 49 |
}; |
| 50 |
|
| 51 |
export function merge(a: TemplateOpts, b: TemplateOpts): TemplateOpts { |
| 52 |
const { |
| 53 |
placeholderWhitelist = a.placeholderWhitelist, |
| 54 |
placeholderPattern = a.placeholderPattern, |
| 55 |
preserveComments = a.preserveComments, |
| 56 |
syntacticPlaceholders = a.syntacticPlaceholders, |
| 57 |
} = b; |
| 58 |
|
| 59 |
return { |
| 60 |
parser: { |
| 61 |
...a.parser, |
| 62 |
...b.parser, |
| 63 |
}, |
| 64 |
placeholderWhitelist, |
| 65 |
placeholderPattern, |
| 66 |
preserveComments, |
| 67 |
syntacticPlaceholders, |
| 68 |
}; |
| 69 |
} |
| 70 |
|
| 71 |
export function validate(opts: unknown): TemplateOpts { |
| 72 |
if (opts != null && typeof opts !== "object") { |
| 73 |
throw new Error("Unknown template options."); |
| 74 |
} |
| 75 |
|
| 76 |
const { |
| 77 |
placeholderWhitelist, |
| 78 |
placeholderPattern, |
| 79 |
preserveComments, |
| 80 |
syntacticPlaceholders, |
| 81 |
...parser |
| 82 |
} = opts || ({} as any); |
| 83 |
|
| 84 |
if (placeholderWhitelist != null && !(placeholderWhitelist instanceof Set)) { |
| 85 |
throw new Error( |
| 86 |
"'.placeholderWhitelist' must be a Set, null, or undefined", |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
if ( |
| 91 |
placeholderPattern != null && |
| 92 |
!(placeholderPattern instanceof RegExp) && |
| 93 |
placeholderPattern !== false |
| 94 |
) { |
| 95 |
throw new Error( |
| 96 |
"'.placeholderPattern' must be a RegExp, false, null, or undefined", |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
if (preserveComments != null && typeof preserveComments !== "boolean") { |
| 101 |
throw new Error( |
| 102 |
"'.preserveComments' must be a boolean, null, or undefined", |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
if ( |
| 107 |
syntacticPlaceholders != null && |
| 108 |
typeof syntacticPlaceholders !== "boolean" |
| 109 |
) { |
| 110 |
throw new Error( |
| 111 |
"'.syntacticPlaceholders' must be a boolean, null, or undefined", |
| 112 |
); |
| 113 |
} |
| 114 |
if ( |
| 115 |
syntacticPlaceholders === true && |
| 116 |
(placeholderWhitelist != null || placeholderPattern != null) |
| 117 |
) { |
| 118 |
throw new Error( |
| 119 |
"'.placeholderWhitelist' and '.placeholderPattern' aren't compatible" + |
| 120 |
" with '.syntacticPlaceholders: true'", |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
return { |
| 125 |
parser, |
| 126 |
placeholderWhitelist: placeholderWhitelist || undefined, |
| 127 |
placeholderPattern: |
| 128 |
placeholderPattern == null ? undefined : placeholderPattern, |
| 129 |
preserveComments: preserveComments == null ? undefined : preserveComments, |
| 130 |
syntacticPlaceholders: |
| 131 |
syntacticPlaceholders == null ? undefined : syntacticPlaceholders, |
| 132 |
}; |
| 133 |
} |
| 134 |
|
| 135 |
export type PublicReplacements = { [x: string]: unknown } | Array<unknown>; |
| 136 |
export type TemplateReplacements = { [x: string]: unknown } | void; |
| 137 |
|
| 138 |
export function normalizeReplacements( |
| 139 |
replacements: unknown, |
| 140 |
): TemplateReplacements { |
| 141 |
if (Array.isArray(replacements)) { |
| 142 |
return replacements.reduce((acc, replacement, i) => { |
| 143 |
acc["$" + i] = replacement; |
| 144 |
return acc; |
| 145 |
}, {}); |
| 146 |
} else if (typeof replacements === "object" || replacements == null) { |
| 147 |
return (replacements as any) || undefined; |
| 148 |
} |
| 149 |
|
| 150 |
throw new Error( |
| 151 |
"Template replacements must be an array, object, null, or undefined", |
| 152 |
); |
| 153 |
} |