Skip to content

esast/esast

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Data structures for JavaScript syntax trees, based on the estree spec.

Esast trees can be passed to any function expecting an estree. They add type safety and are more convenient to construct.

Esast is ideal for generating JavaScript code. Don't output a string directly; it's bug-prone and makes it hard to support source maps. Construct an estree and let it take care of rendering for you.

Install

npm install --save esast/esast
# or:
bower install --save esast/esast

Use

import {BinaryExpression, LiteralNumber} from 'esast/lib/Expression'
const four = new BinaryExpression('+', new LiteralNumber(2), new LiteralNumber(2))
console.log(four)

Documentation

HTML documentation coming soon! For now, see AST and Loc documentation.

Other libraries

op: Essential to handling optional subtrees.

For compilers:

Other:

Source Maps

There is a loc property on every esast Node. You can ignore it, but you'll need it to support source maps.

import {Literal} from 'esast/lib/ast'
import Loc, {Pos} from 'esast/lib/Loc'
import {renderWithSourceMap} from 'esast-render-fast/lib/render'
import {loc} from 'esast-create-util/lib/util'

const ast = loc(new Literal(5), new Loc(Pos.start, new Pos(1, 5)))
console.log(renderWithSourceMap(ast, 'inFileName', 'outFileName.js'))

If you're writing a compiler, generate Locs while lexing, attach them to tokens, and attach them to your own ASTs while parsing. Then when transpiling, copy the loc from your AST to the esast Node corresponding to it. (Not all Nodes need a loc; when it is missing, a node will be assumed to have the loc of its parent.)

Contribute

Please make an issue if there's anything you'd like added or changed.

About

Type-safe EcmaScript AST representation

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published