UNPKG

8.25 kBTypeScriptView Raw
1import * as t from "@babel/types";
2
3export interface GeneratorOptions {
4 /**
5 * Optional string to add as a block comment at the start of the output file.
6 */
7 auxiliaryCommentBefore?: string | undefined;
8
9 /**
10 * Optional string to add as a block comment at the end of the output file.
11 */
12 auxiliaryCommentAfter?: string | undefined;
13
14 /**
15 * Function that takes a comment (as a string) and returns true if the comment should be included in the output.
16 * By default, comments are included if `opts.comments` is `true` or if `opts.minifed` is `false` and the comment
17 * contains `@preserve` or `@license`.
18 */
19 shouldPrintComment?(comment: string): boolean;
20
21 /**
22 * Attempt to use the same line numbers in the output code as in the source code (helps preserve stack traces).
23 * Defaults to `false`.
24 */
25 retainLines?: boolean | undefined;
26
27 /**
28 * Retain parens around function expressions (could be used to change engine parsing behavior)
29 * Defaults to `false`.
30 */
31 retainFunctionParens?: boolean | undefined;
32
33 /**
34 * Should comments be included in output? Defaults to `true`.
35 */
36 comments?: boolean | undefined;
37
38 /**
39 * Set to true to avoid adding whitespace for formatting. Defaults to the value of `opts.minified`.
40 */
41 compact?: boolean | "auto" | undefined;
42
43 /**
44 * Should the output be minified. Defaults to `false`.
45 */
46 minified?: boolean | undefined;
47
48 /**
49 * Set to true to reduce whitespace (but not as much as opts.compact). Defaults to `false`.
50 */
51 concise?: boolean | undefined;
52
53 /**
54 * Used in warning messages
55 */
56 filename?: string | undefined;
57
58 /**
59 * Enable generating source maps. Defaults to `false`.
60 */
61 sourceMaps?: boolean | undefined;
62
63 /**
64 * A root for all relative URLs in the source map.
65 */
66 sourceRoot?: string | undefined;
67
68 /**
69 * The filename for the source code (i.e. the code in the `code` argument).
70 * This will only be used if `code` is a string.
71 */
72 sourceFileName?: string | undefined;
73
74 /**
75 * Set to true to run jsesc with "json": true to print "\u00A9" vs. "©";
76 */
77 jsonCompatibleStrings?: boolean | undefined;
78
79 /**
80 * Set to true to enable support for experimental decorators syntax before module exports.
81 * Defaults to `false`.
82 */
83 decoratorsBeforeExport?: boolean | undefined;
84
85 /**
86 * The import attributes/assertions syntax to use.
87 * When not specified, @babel/generator will try to match the style in the input code based on the AST shape.
88 */
89 importAttributesKeyword?: "with" | "assert" | "with-legacy";
90
91 /**
92 * Options for outputting jsesc representation.
93 */
94 jsescOption?: {
95 /**
96 * The default value for the quotes option is 'single'. This means that any occurrences of ' in the input
97 * string are escaped as \', so that the output can be used in a string literal wrapped in single quotes.
98 */
99 quotes?: "single" | "double" | "backtick" | undefined;
100
101 /**
102 * The default value for the numbers option is 'decimal'. This means that any numeric values are represented
103 * using decimal integer literals. Other valid options are binary, octal, and hexadecimal, which result in
104 * binary integer literals, octal integer literals, and hexadecimal integer literals, respectively.
105 */
106 numbers?: "binary" | "octal" | "decimal" | "hexadecimal" | undefined;
107
108 /**
109 * The wrap option takes a boolean value (true or false), and defaults to false (disabled). When enabled, the
110 * output is a valid JavaScript string literal wrapped in quotes. The type of quotes can be specified through
111 * the quotes setting.
112 */
113 wrap?: boolean | undefined;
114
115 /**
116 * The es6 option takes a boolean value (true or false), and defaults to false (disabled). When enabled, any
117 * astral Unicode symbols in the input are escaped using ECMAScript 6 Unicode code point escape sequences
118 * instead of using separate escape sequences for each surrogate half. If backwards compatibility with ES5
119 * environments is a concern, don’t enable this setting. If the json setting is enabled, the value for the es6
120 * setting is ignored (as if it was false).
121 */
122 es6?: boolean | undefined;
123
124 /**
125 * The escapeEverything option takes a boolean value (true or false), and defaults to false (disabled). When
126 * enabled, all the symbols in the output are escaped — even printable ASCII symbols.
127 */
128 escapeEverything?: boolean | undefined;
129
130 /**
131 * The minimal option takes a boolean value (true or false), and defaults to false (disabled). When enabled,
132 * only a limited set of symbols in the output are escaped: \0, \b, \t, \n, \f, \r, \\, \u2028, \u2029.
133 */
134 minimal?: boolean | undefined;
135
136 /**
137 * The isScriptContext option takes a boolean value (true or false), and defaults to false (disabled). When
138 * enabled, occurrences of </script and </style in the output are escaped as <\/script and <\/style, and <!--
139 * is escaped as \x3C!-- (or \u003C!-- when the json option is enabled). This setting is useful when jsesc’s
140 * output ends up as part of a <script> or <style> element in an HTML document.
141 */
142 isScriptContext?: boolean | undefined;
143
144 /**
145 * The compact option takes a boolean value (true or false), and defaults to true (enabled). When enabled,
146 * the output for arrays and objects is as compact as possible; it’s not formatted nicely.
147 */
148 compact?: boolean | undefined;
149
150 /**
151 * The indent option takes a string value, and defaults to '\t'. When the compact setting is enabled (true),
152 * the value of the indent option is used to format the output for arrays and objects.
153 */
154 indent?: string | undefined;
155
156 /**
157 * The indentLevel option takes a numeric value, and defaults to 0. It represents the current indentation level,
158 * i.e. the number of times the value of the indent option is repeated.
159 */
160 indentLevel?: number | undefined;
161
162 /**
163 * The json option takes a boolean value (true or false), and defaults to false (disabled). When enabled, the
164 * output is valid JSON. Hexadecimal character escape sequences and the \v or \0 escape sequences are not used.
165 * Setting json: true implies quotes: 'double', wrap: true, es6: false, although these values can still be
166 * overridden if needed — but in such cases, the output won’t be valid JSON anymore.
167 */
168 json?: boolean | undefined;
169
170 /**
171 * The lowercaseHex option takes a boolean value (true or false), and defaults to false (disabled). When enabled,
172 * any alphabetical hexadecimal digits in escape sequences as well as any hexadecimal integer literals (see the
173 * numbers option) in the output are in lowercase.
174 */
175 lowercaseHex?: boolean | undefined;
176 } | undefined;
177}
178
179export class CodeGenerator {
180 constructor(ast: t.Node, opts?: GeneratorOptions, code?: string);
181 generate(): GeneratorResult;
182}
183
184/**
185 * Turns an AST into code, maintaining sourcemaps, user preferences, and valid output.
186 * @param ast - the abstract syntax tree from which to generate output code.
187 * @param opts - used for specifying options for code generation.
188 * @param code - the original source code, used for source maps.
189 * @returns - an object containing the output code and source map.
190 */
191export default function generate(
192 ast: t.Node,
193 opts?: GeneratorOptions,
194 code?: string | { [filename: string]: string },
195): GeneratorResult;
196
197export interface GeneratorResult {
198 code: string;
199 map: {
200 version: number;
201 sources: string[];
202 names: string[];
203 sourceRoot?: string | undefined;
204 sourcesContent?: string[] | undefined;
205 mappings: string;
206 file: string;
207 } | null;
208}