https://github.com/jamen/estree-parent
Get parent of an ESTree node
https://github.com/jamen/estree-parent
Last synced: 9 months ago
JSON representation
Get parent of an ESTree node
- Host: GitHub
- URL: https://github.com/jamen/estree-parent
- Owner: jamen
- License: mit
- Created: 2016-12-05T05:06:20.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-01-07T23:15:30.000Z (over 9 years ago)
- Last Synced: 2025-08-19T22:42:23.138Z (10 months ago)
- Language: JavaScript
- Homepage:
- Size: 12.7 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# estree-parent
> Get parent of an ESTree node
```js
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
```sh
$ npm install --save estree-parent
```
## Usage
### `parent(node, source)`
Get parent the parent of a node
- `node` (ESTree `Node`): Node you are getting parent of
- `source` (ESTree `Node`): Node with children (e.g. `Program`) that contains `node`
Returns `null` if node was not found in the tree
```js
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` (EStree `Node`): Node you are getting ancestors of
- `source` (ESTree `Node`): Node with children that contains `node`
```js
parent.ancestors(node, source)
// [ ReturnStatement { ... },
// SwitchCase { ... },
// BlockExpression { ... },
// SwitchStatement { ... },
// .....
// Program { ... } ]
```
## License
MIT © [Jamen Marz](https://git.io/jamen)
---
[][package] [](https://travis-ci.org/jamen/estree-parent) [][package] [][package] [](https://github.com/jamen)
[package]: https://npmjs.org/package/estree-parent