Package Exports
- @buxlabs/ast
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 (@buxlabs/ast) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
AbstractSyntaxTree
You might find the class useful if you want to abstract away some ast manipulations such as adding, removing, replacing the nodes and others. The test folder is a good starting point for examples of usage. PRs are highly welcome.
Installation
npm install @buxlabs/ast
Methods
has
Check if ast contains a node of given type.
var source = 'var a = "x";'
var ast = new AbstractSyntaxTree(source)
ast.has('VariableDeclaration')
count
Count ast nodes of given type.
var source = 'var a = "x"; var b = "y";'
var ast = new AbstractSyntaxTree(source)
ast.count('VariableDeclaration')
find
Find all nodes of given type.
var source = 'var a = "x";'
var ast = new AbstractSyntaxTree(source)
ast.find('VariableDeclaration')
each
Iterate over all nodes of given type.
var source = 'var a = "x";'
var ast = new AbstractSyntaxTree(source)
ast.each('VariableDeclaration', node => {
console.log(node)
})
first
First first node of given type.
var source = 'var a = "x";'
var ast = new AbstractSyntaxTree(source)
ast.first('VariableDeclaration')
last
Find last node of given type.
var source = 'var a = "x";'
var ast = new AbstractSyntaxTree(source)
ast.last('VariableDeclaration')
remove
Remove all nodes that match the criteria.
var source = '"use strict"; var b = 4;'
var ast = new AbstractSyntaxTree(source)
ast.remove({ type: 'Literal', value: 'use strict' })
var source = 'function hello () { var foo = "bar"; return "world"; }'
var ast = new AbstractSyntaxTree(source)
ast.remove('BlockStatement > VariableDeclaration')
walk
Walks over all nodes
var source = 'var a = 1'
var ast = new AbstractSyntaxTree(source)
ast.walk((node, parent) => {
console.log(node, parent)
})
traverse
Walks over all nodes
var source = 'var a = 1'
var ast = new AbstractSyntaxTree(source)
ast.walk({
enter (node) {
console.log(node)
},
leave (node) {
console.log(node)
}
})
replace
Replace all nodes that match the criteria.
var source = 'var a = 1'
var ast = new AbstractSyntaxTree(source)
ast.replace({
enter (node) {
if (node.type === 'VariableDeclaration') {
node.kind = 'let'
}
return node
}
})
prepend
Prepend a node to the body.
var source = 'var a = 1;'
var ast = new AbstractSyntaxTree(source)
ast.prepend({
type: 'ExpressionStatement',
expression: {
type: 'Literal',
value: 'use strict'
}
})
append
Append a node to the body.
var source = 'var a = 1;'
var ast = new AbstractSyntaxTree(source)
ast.append({
type: 'ExpressionStatement',
expression: {
type: 'Literal',
value: 'test'
}
})
wrap
Wrap body with given node.
var source = 'var a = 1;'
var ast = new AbstractSyntaxTree(source)
ast.wrap(body => {
return [
{
"type": "ExpressionStatement",
"expression": {
"type": "CallExpression",
"callee": {
"type": "FunctionExpression",
"id": null,
"params": [],
"defaults": [],
"body": {
"type": "BlockStatement",
"body": body
},
"rest": null,
"generator": false,
"expression": false
},
"arguments": []
}
}
]
})
unwrap
Change the code to the first BlockStatement body
var source = '(function () { console.log(1); }())'
var ast = new AbstractSyntaxTree(source)
ast.unwrap()
ast.toSource()
template
Create ast partials from templates
var source = 'console.log(1);'
var ast = new AbstractSyntaxTree(source)
ast.template('var foo = <%= bar %>;' { bar: { type: 'Literal', value: 1 } })
toSource
Convert the ast to string.
var source = 'var a = 1;'
var ast = new AbstractSyntaxTree(source)
var source = ast.toSource()
console.log(source)
var source = 'var a = 1;'
var ast = new AbstractSyntaxTree(source)
var { source, map } = ast.toSource({ sourceMap: true })
console.log(source)
console.log(map)
toSourceMap
Generates a source map.
var source = 'var a = 1;'
var ast = new AbstractSyntaxTree(source)
var map = ast.toSourceMap()
console.log(map)