Manu Sridharan edited this page Nov 8, 2015 · 24 revisions



escodegen.generate(AST[, options]);


default options,

        format: {
            indent: {
                style: '    ',
                base: 0,
                adjustMultilineComment: false
            newline: '\n',
            space: ' ',
            json: false,
            renumber: false,
            hexadecimal: false,
            quotes: 'single',
            escapeless: false,
            compact: false,
            parentheses: true,
            semicolons: true,
            safeConcatenation: false
        moz: {
            starlessGenerator: false,
            parenthesizedComprehensionBlock: false,
            comprehensionExpressionStartsWithAssignment: false
        parse: null,
        comment: false,
        sourceMap: undefined,
        sourceMapRoot: null,
        sourceMapWithCode: false,
        file: undefined,
        sourceContent: originalSource,
        directive: false,
        verbatim: undefined

Indent string. Default is 4 spaces ('    ').


Base indent level. Default is 0.


Adjust the indentation of multiline comments to keep asterisks vertically aligned. Default is false.


New line string. Default is '\n'.

White space string. Default is standard ' ' (\x20).


Enforce JSON format of numeric and string literals. This option takes precedence over option.format.hexadecimal and option.format.quotes. Default is false.


Try to generate shorter numeric literals than toString() (9.8.1). Default is false.


Generate hexadecimal a numeric literal if it is shorter than its equivalents. Requires option.format.renumber. Default is false.


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'.


Escape as few characters in string literals as necessary. Default is false.


Do not include superfluous whitespace characters and line terminators. Default is false.


Preserve parentheses in new expressions that have no arguments. Default is true.


Preserve semicolons at the end of blocks and programs. Default is true.


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.


If comments are attached to AST, escodegen is going to emit comments to output code. Default is false.

option.sourceMap, option.sourceMapRoot, option.sourceMapWithCode, option.file & option.sourceContent

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). If a non-empty string value is provided, generate a source map. If sourceMapWithCode is true generator returns output hash, where is a source-map representation, which can be serialized as output.code is a string with generated JS code (note that it's not going to have //@ sourceMappingURL comment in it). 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. 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. 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.


Recognize DirectiveStatement and distinguish it from ExpressionStatement


Providing verbatim code generation option to Expression nodes. verbatim option is provided by user as string. When generating Expression code, looking up node[option.verbatim] value and dump it instead of normal code generation.

For example,

    type: 'Literal',
    value: 200,
    'x-verbatim-property': '2e2'
}, { verbatim: 'x-verbatim-property' });



Also, an object can be assigned in 'x-verbatim-property' property.

    type: 'Literal',
    value: 200,
    'x-verbatim-property': {
        content : '2e2',
        precedence : escodegen.Precedence.Primary
}, { verbatim: 'x-verbatim-property' });

For more detail on verbatim, refer to More user-controllable verbatim property · Issue #173

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.