QueryAST
A library to traverse/modify an AST
Documentation
Read the API documentation
Usage
let createQueryWrapper = require('query-ast')
let $ = createQueryWrapper(ast, options)
Getting Started
QueryAST aims to provide a jQuery like API for traversing an AST.
let ast = {
type: 'program',
value: [{
type: 'item_container',
value: [{
type: 'item',
value: 'a'
}]
}, {
type: 'item_container',
value: []
}, {
type: 'item',
value: 'b'
}]
}
let $ = createQueryWrapper(ast)
$('item').length()
$('item_container').filter((n) => {
return $(n).has('item')
}).length()
Selectors
Most of the traversal functions take an optional QueryWrapper~Selector
argument that will
be use to filter the results.
A selector can be 1 of 3 types:
string
that is compared against the return value of options.getType()
regexp
that is compared against the return value of options.getType()
function
that will be passed a NodeWrapper
and expected to return a boolean
let ast = {
type: 'program',
value: [{
type: 'item_container',
value: [{
type: 'item',
value: 'a'
}]
}, {
type: 'item',
value: 'b'
}]
}
let $ = createQueryWrapper(ast)
$('item').length()
$(/item/).length()
$((n) => n.node.value === 'a').length()
Default format
By default, QueryAST assumes that an AST will be formatted as a node tree
where each node has a type
key and a value
key that either contains the
string value of the node or an array of child nodes.
let ast = {
type: 'program',
value: [{
type: 'item',
value: 'a'
}]
}
Alternate formats
Not every AST follows the same format, so QueryAST also provides a way
to traverse any tree structure. Below are the default options used to
handle the above AST structure.
let options = {
hasChildren: (node) => Array.isArray(node.value),
getChildren: (node) => node.value,
getType: (node) => node.type,
toJSON: (node, children) => {
return Object.assign({}, node, {
value: children ? children : node.value
})
},
toString: (node) => {
return typeof node.value === 'string' ? node.value : ''
}
}
Running tests
Clone the repository, then:
npm install
npm test
Generate Documentation
npm run doc