UNPKG

6.37 kBTypeScriptView Raw
1// Type definitions for escodegen
2// Project: https://github.com/estools/escodegen
3// Definitions by: Simon de Lang <https://github.com/simondel>
4// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5
6export interface FormatOptions {
7 /**
8 * The indent options
9 */
10 indent?: IndentOptions | undefined;
11 /**
12 * New line string. Default is '\n'.
13 */
14 newline?: string | undefined;
15 /**
16 * White space string. Default is standard ' ' (\x20).
17 */
18 space?: string | undefined;
19 /**
20 * Enforce JSON format of numeric and string literals. This option takes precedence over option.format.hexadecimal and option.format.quotes. Default is false.
21 */
22 json?: boolean | undefined;
23 /**
24 * Try to generate shorter numeric literals than toString() (9.8.1). Default is false.
25 */
26 renumber?: boolean | undefined;
27 /**
28 * Generate hexadecimal a numeric literal if it is shorter than its equivalents. Requires option.format.renumber. Default is false.
29 */
30 hexadecimal?: boolean | undefined;
31 /**
32 * Delimiter to use for string literals. Accepted values are: 'single', 'double', and 'auto'. When 'auto' is specified, escodegen selects a delimiter that results in a shorter literal. Default is 'single'.
33 */
34 quotes?: string | undefined;
35 /**
36 * Escape as few characters in string literals as necessary. Default is false.
37 */
38 escapeless?: boolean | undefined;
39 /**
40 * Do not include superfluous whitespace characters and line terminators. Default is false.
41 */
42 compact?: boolean | undefined;
43 /**
44 * Preserve parentheses in new expressions that have no arguments. Default is true.
45 */
46 parentheses?: boolean | undefined;
47 /**
48 * Preserve semicolons at the end of blocks and programs. Default is true.
49 */
50 semicolons?: boolean | undefined;
51 safeConcatenation?: boolean | undefined;
52 preserveBlankLines?: boolean | undefined;
53}
54
55export interface IndentOptions {
56 /**
57 * Indent string. Default is 4 spaces (' ').
58 */
59 style?: string | undefined;
60 /**
61 * Base indent level. Default is 0.
62 */
63 base?: number | undefined;
64 /**
65 * Adjust the indentation of multiline comments to keep asterisks vertically aligned. Default is false.
66 */
67 adjustMultilineComment?: boolean | undefined;
68}
69
70export interface MozillaOptions {
71 /**
72 * Default: false
73 */
74 starlessGenerator?: boolean | undefined;
75 /**
76 * Default: false
77 */
78 parenthesizedComprehensionBlock?: boolean | undefined;
79 /**
80 * Default: false
81 */
82 comprehensionExpressionStartsWithAssignment?: boolean | undefined;
83}
84
85export interface GenerateOptions {
86 /**
87 * The format options
88 */
89 format?: FormatOptions | undefined;
90 moz?: MozillaOptions | undefined;
91 /**
92 * Mozilla Parser API compatible parse function, e.g., the parse function exported by esprima. If it is provided, generator tries to use the 'raw' representation. See esprima raw information. Default is null.
93 */
94 parse?: Function | undefined;
95 /**
96 * If comments are attached to AST, escodegen is going to emit comments to output code. Default is false.
97 */
98 comment?: boolean | undefined;
99 /**
100 * sourceMap is the source maps's source filename, that's a name that will show up in the browser debugger for the generated source (if source-maps is enabled).
101 * If a non-empty string value is provided, generate a source map.
102 */
103 sourceMap?: string | undefined;
104 /**
105 * . If sourceMapWithCode is true generator returns output hash, where output.map is a source-map representation, which can be serialized as output.map.toString(). output.code is a string with generated JS code (note that it's not going to have //@ sourceMappingURL comment in it).
106 */
107 sourceMapWithCode?: boolean | undefined;
108 /**
109 * Optionally option.sourceContent string can be passed (which represents original source of the file, for example it could be a source of coffeescript from which JS is being generated), if provided generated source map will have original source embedded in it.
110 */
111 sourceContent?: string | undefined;
112 sourceCode?: string | undefined;
113 /**
114 * Optionally option.sourceMapRoot can be provided, in which case option.sourceMap will be treated as relative to it. For more information about source map itself, see source map library document, V3 draft and HTML5Rocks introduction. Default is undefined
115 * sourceMapRoot is the source root for the source map (see the Mozilla documentation). If sourceMapWithCode is truthy, an object is returned from generate() of the form: { code: .. , map: .. }. If file is provided, it will be used as file property of generated source map.
116 */
117 sourceMapRoot?: string | undefined;
118 /**
119 * Recognize DirectiveStatement and distinguish it from ExpressionStatement. Default: false
120 */
121 directive?: boolean | undefined;
122 /**
123 * If file is provided, it will be used as file property of generated source map.
124 */
125 file?: string | undefined;
126 /**
127 * Providing verbatim code generation option to Expression nodes.
128 * verbatim option is provided by user as string. When generating Expression code,
129 * looking up node[option.verbatim] value and dump it instead of normal code generation.
130 *
131 * @example
132 *
133 */
134 verbatim?: string | undefined;
135}
136
137/**
138 * https://github.com/estools/escodegen/commit/adf113333cd4888cf59bfc4f957df98bf7db82b6
139 */
140export enum Precedence {
141 Sequence,
142 Yield,
143 Await,
144 Assignment,
145 Conditional,
146 ArrowFunction,
147 LogicalOR,
148 LogicalAND,
149 BitwiseOR,
150 BitwiseXOR,
151 BitwiseAND,
152 Equality,
153 Relational,
154 BitwiseSHIFT,
155 Additive,
156 Multiplicative,
157 Unary,
158 Postfix,
159 Call,
160 New,
161 TaggedTemplate,
162 Member,
163 Primary
164}
165
166/**
167 * Produces given Abstract Syntax Tree as javascript code
168 * @param ast The Abstract Syntax Tree to generate code from
169 * @param options The generation options
170 */
171export function generate(ast: any, options?: GenerateOptions): string;
172/**
173 * Attaching the comments is needed to keep the comments and to allow blank lines to be preserved.
174 */
175export function attachComments(ast: any, comments: any, tokens: any): any;