UNPKG

3.13 kBTypeScriptView Raw
1import { ParserOptions } from "@babel/parser";
2import { Expression, Program, Statement } from "@babel/types";
3
4export interface TemplateBuilderOptions extends ParserOptions {
5 /**
6 * A set of placeholder names to automatically accept.
7 * Items in this list do not need to match `placeholderPattern`.
8 *
9 * This option cannot be used when using `%%foo%%` style placeholders.
10 */
11 placeholderWhitelist?: Set<string> | null | undefined;
12
13 /**
14 * A pattern to search for when looking for `Identifier` and `StringLiteral`
15 * nodes that should be considered as placeholders.
16 *
17 * `false` will disable placeholder searching placeholders, leaving only
18 * the `placeholderWhitelist` value to find replacements.
19 *
20 * This option cannot be used when using `%%foo%%` style placeholders.
21 *
22 * @default /^[_$A-Z0-9]+$/
23 */
24 placeholderPattern?: RegExp | false | null | undefined;
25
26 /**
27 * Set this to `true` to preserve comments from the template string
28 * into the resulting AST, or `false` to automatically discard comments.
29 *
30 * @default false
31 */
32 preserveComments?: boolean | null | undefined;
33
34 /**
35 * Set to `true` to use `%%foo%%` style placeholders, `false` to use legacy placeholders
36 * described by `placeholderPattern` or `placeholderWhitelist`.
37 *
38 * When it is not set, it behaves as `true` if there are syntactic placeholders, otherwise as `false`.
39 *
40 * @since 7.4.0
41 */
42 syntacticPlaceholders?: boolean | null | undefined;
43}
44
45export interface TemplateBuilder<T> {
46 /**
47 * Build a new builder, merging the given options with the previous ones.
48 */
49 (opts: TemplateBuilderOptions): TemplateBuilder<T>;
50
51 /**
52 * Building from a string produces an AST builder function by default.
53 */
54 (code: string, opts?: TemplateBuilderOptions): (arg?: PublicReplacements) => T;
55
56 /**
57 * Building from a template literal produces an AST builder function by default.
58 */
59 (tpl: TemplateStringsArray, ...args: unknown[]): (arg?: PublicReplacements) => T;
60
61 /**
62 * Allow users to explicitly create templates that produce ASTs,
63 * skipping the need for an intermediate function.
64 *
65 * Does not allow `%%foo%%` style placeholders.
66 */
67 ast: {
68 (tpl: string, opts?: TemplateBuilderOptions): T;
69 (tpl: TemplateStringsArray, ...args: unknown[]): T;
70 };
71}
72
73export type PublicReplacements = { [index: string]: unknown } | unknown[];
74
75export const smart: TemplateBuilder<Statement | Statement[]>;
76export const statement: TemplateBuilder<Statement>;
77export const statements: TemplateBuilder<Statement[]>;
78export const expression: TemplateBuilder<Expression>;
79export const program: TemplateBuilder<Program>;
80
81type DefaultTemplateBuilder = typeof smart & {
82 smart: typeof smart;
83 statement: typeof statement;
84 statements: typeof statements;
85 expression: typeof expression;
86 program: typeof program;
87 ast: typeof smart.ast;
88};
89
90declare const templateBuilder: DefaultTemplateBuilder;
91
92export default templateBuilder;