UNPKG

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