estree-builder
Handy functions for building estree nodes
Example
var e = require('estree-builder')
var estree = e.number(1)
var astring = require('astring').generate
astring(estree)
estree = e.fn(['a', 'b'], [
e('return', e('+', e.id('a'), e.id('b')))
], 'add')
astring(estree)
Usage
There are 3 ways to call a builder
e.number(1)
e['number'](1)
e('number', 1)
All builders can take a location
object as the last argument. (i.e. for generating source maps)
var loc = {
source: "some-file.js"
start: { line: 1, column: 0 },
end: { line: 1, column: 1 }
}
e('number', 1, loc)
List of builders
building block values
e('number', val)
e('string', val)
e('true')
e('false')
e('null')
e('undefined')
e('array', elements)
e('object-raw', pairs)
e('object-property', key, value)
e('object', obj)
given some json object, build the tree
variables
e('var', id, val)
e('let', id, val)
e('const', id, val)
e('identifier', name)
control flow
e('if', test, consequent, alternate)
e('ternary', test, consequent, alternate)
e('switch', discriminant, cases)
e('case', test, consequent)
e('default', consequent)
e('while', test, body)
e('for', init, test, update, body)
e('for-in', left, right, body)
e('for-of', left, right, body)
e('break')
e('continue')
e('return', arg)
e('throw', arg)
e('try', body, catchVar, catchStmt, finallyStmt)
functions
e('function', args, body, id)
e('call', callee, args)
e('arrow', args, body)
property access
e('.', obj, prop)
e('get', obj, prop)
e('get-in', obj, path)
language stuff
e('arguments')
e('this')
e('statement', expr)
e('block', body)
e('new', callee, args)
infix operators
e('==', left, right)
e('!=', left, right)
e('===', left, right)
e('!==', left, right)
e('<', left, right)
e('<=', left, right)
e('>', left, right)
e('>=', left, right)
e('<<', left, right)
e('>>', left, right)
e('>>>', left, right)
e('*', left, right)
e('/', left, right)
e('%', left, right)
e('|', left, right)
e('^', left, right)
e('&', left, right)
e('in', left, right)
e('instanceof', left, right)
e('&&', left, right)
e('||', left, right)
e('+', a, b)
e('-', a, b)
unary operators
e('!', arg)
e('~', arg)
e('typeof', arg)
e('void', arg)
e('delete', arg)
e('++', arg)
e('--', arg)
assignments
e('=', left, right)
e('+=', left, right)
e('-=', left, right)
e('*=', left, right)
e('/=', left, right)
e('%=', left, right)
e('<<=', left, right)
e('>>=', left, right)
e('>>>=', left, right)
e('|=', left, right)
e('^=', left, right)
e('&=', left, right)
destructuring
e('assign', left, right)
e('assign-property', key, value)
e('obj-pattern', properties)
e('arr-pattern', elements)
generator functions
e('genfn', args, body, id)
e('yield', arg, delegate)
classes
e('class', name, superClass, methods)
e('method', key, value, kind, computed, isStatic)
Contributing
Add tests to tests.js, run them like this:
or to automatically re-run them whenever you make a change
Re-generate the docs (README.md between the GEN-DOCS-BEGIN|END comments)
License
MIT