UNPKG

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