JSPM

  • Created
  • Published
  • Downloads 3151529
  • Score
    100M100P100Q210286F
  • License ISC

JavaScript code generator from an ESTree-formatted AST node.

Package Exports

  • astring

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (astring) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Astring

A tiny and fast ESTree formatted AST to JavaScript code generator.

Key features:

  • Supports ECMAScript versions 5 and 6.
  • Considerably faster than Escodegen and Esotope.
  • No dependencies and small footprint (11 KB minified, 3 KB gziped).
  • Output code is readable and executable.
  • Reduced formatting options.

Installation

Checkout this repository, then run from the main directory:

npm install

Alternatively, install it with the Node Package Manager:

npm install astring

Usage

Astring is the perfect companion of Acorn, a blazingly fast JavaScript parser.

// Import modules
acorn = require( 'acorn' )
astring = require( 'astring' )

// Example code
code = "let answer = 4 + 7 * 5 + 3;"

// Parse it into an AST
node = acorn.parse( code, { ecmaVersion: 6 } )

// Set formatting options
options = {
    indent: '   ',
    lineEnd: '\n'
}

// Format it
result = astring( node, options )

// Check it
if ( code === result )
    console.log( 'It works !' )
else
    console.log( 'Something went wrong…' )

The exported function returns a string representing the rendered code of the provided AST node. The options are:

  • indent: string to use for indentation (defaults to \t)
  • lineEnd: string to use for line endings (defaults to \n)
  • startingIndentLevel: indent level to start from (default to 0)

Caveats

Testing is not done yet (undergoing).