UNPKG

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