ast-parentchild
Version:
Convert your JavaScript file to acron AST format and then output a json file whihc have parent-child realtionship between nodes
165 lines (147 loc) • 3.87 kB
Markdown
<div id="headline" align="center">
<h1>Ast-ParentChild</h1>
<a href="https://www.npmjs.com/package/ast-parentchild">
<img src="https://badge.fury.io/js/ast-parentchild.svg" alt="PyPI version"></img>
</a>
</div>
Ast-Parent-Child is a CLI tool to generate the [AST](https://www.digitalocean.com/community/tutorials/js-traversing-ast) of a given JavaScript file, and then it will output a json file which has a parent-child relationship format for the AST rather than outputting a ordinary AST JSON format.
Ast-ParentChild uses [AcornJS](https://github.com/acornjs/acorn) under the hood to generate the AST.
```bash
$ npm i ast-parentchild
$ ast-parentchild /path/to/javascript/file.js /output/path/of/file.json
```
The following is the input JavaScript file `sample.js`.
```javascript
const example = "example";
```
The following is the output JSON file `output.json`.
```json
{
"id": 7,
"data": [
[
"type",
"Program"
],
[
"start",
0
],
[
"end",
25
],
[
"sourceType",
"script"
]
],
"children": [
{
"id": 6,
"data": null,
"children": [
{
"id": 5,
"data": [
[
"type",
"VariableDeclaration"
],
[
"start",
0
],
[
"end",
25
],
[
"kind",
"const"
]
],
"children": [
{
"id": 4,
"data": null,
"children": [
{
"id": 3,
"data": [
[
"type",
"VariableDeclarator"
],
[
"start",
6
],
[
"end",
25
]
],
"children": [
{
"id": 1,
"data": [
[
"type",
"Identifier"
],
[
"start",
6
],
[
"end",
13
],
[
"name",
"example"
]
],
"children": []
},
{
"id": 2,
"data": [
[
"type",
"Literal"
],
[
"start",
16
],
[
"end",
25
],
[
"value",
"example"
],
[
"raw",
"\"example\""
]
],
"children": []
}
]
}
]
}
]
}
]
}
]
}
```