UNPKG

3.27 kBTypeScriptView Raw
1// Type definitions for babel-generator 6.25
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// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5// TypeScript Version: 2.8
6
7import * as t from 'babel-types';
8
9/**
10 * Turns an AST into code, maintaining sourcemaps, user preferences, and valid output.
11 * @param ast - the abstract syntax tree from which to generate output code.
12 * @param opts - used for specifying options for code generation.
13 * @param code - the original source code, used for source maps.
14 * @returns - an object containing the output code and source map.
15 */
16export default function generate(ast: t.Node, opts?: GeneratorOptions, code?: string | {[filename: string]: string}): GeneratorResult;
17
18export interface GeneratorOptions {
19 /**
20 * Optional string to add as a block comment at the start of the output file.
21 */
22 auxiliaryCommentBefore?: string | undefined;
23
24 /**
25 * Optional string to add as a block comment at the end of the output file.
26 */
27 auxiliaryCommentAfter?: string | undefined;
28
29 /**
30 * Function that takes a comment (as a string) and returns true if the comment should be included in the output.
31 * By default, comments are included if `opts.comments` is `true` or if `opts.minifed` is `false` and the comment
32 * contains `@preserve` or `@license`.
33 */
34 shouldPrintComment?(comment: string): boolean;
35
36 /**
37 * Attempt to use the same line numbers in the output code as in the source code (helps preserve stack traces).
38 * Defaults to `false`.
39 */
40 retainLines?: 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 * The type of quote to use in the output. If omitted, autodetects based on `ast.tokens`.
64 */
65 quotes?: 'single' | 'double' | undefined;
66
67 /**
68 * Used in warning messages
69 */
70 filename?: string | undefined;
71
72 /**
73 * Enable generating source maps. Defaults to `false`.
74 */
75 sourceMaps?: boolean | undefined;
76
77 /**
78 * The filename of the generated code that the source map will be associated with.
79 */
80 sourceMapTarget?: string | undefined;
81
82 /**
83 * A root for all relative URLs in the source map.
84 */
85 sourceRoot?: string | undefined;
86
87 /**
88 * The filename for the source code (i.e. the code in the `code` argument).
89 * This will only be used if `code` is a string.
90 */
91 sourceFileName?: string | undefined;
92
93 /**
94 * Set to true to run jsesc with "json": true to print "\u00A9" vs. "©";
95 */
96 jsonCompatibleStrings?: boolean | undefined;
97}
98
99export interface GeneratorResult {
100 map: {};
101 code: string;
102}