estree-parent
v0.3.1
Published
Get parent of an ESTree node
Downloads
8
Maintainers
Readme
estree-parent
Get parent of an ESTree node
var parent = require('estree-parent')
var esprima = require('esprima')
// Get source and node somehow:
var source = esprima.parse(/* ... */)
var node = getNodeSomehow(source)
// Get parent:
parent(node, source)
// SwitchStatement { ... }
It uses a WeakMap
cache behind the scenes to make repeated use faster
Installation
$ npm install --save estree-parent
Usage
parent(node, source)
Get parent the parent of a node
node
(ESTreeNode
): Node you are getting parent ofsource
(ESTreeNode
): Node with children (e.g.Program
) that containsnode
Returns null
if node was not found in the tree
parent(node, source)
// FunctionDeclaration { ... }
parent(node)
// node.parent || null
parent.ancestors(node, source)
Get an array of ancestor nodes from parent
up to source
.
node
(EStreeNode
): Node you are getting ancestors ofsource
(ESTreeNode
): Node with children that containsnode
parent.ancestors(node, source)
// [ ReturnStatement { ... },
// SwitchCase { ... },
// BlockExpression { ... },
// SwitchStatement { ... },
// .....
// Program { ... } ]
License
MIT © Jamen Marz