Node-ASTerer is a CLI tool to generate the AST of a given JavaScript file, and it will save it in a *.json file.
Node-ASTerer uses AcornJS under the hood to generate the AST.
$ npm install -g @niweera/node-asterer
$ node-asterer -i /path/to/javascript/file.js -o /output/path/of/file.json
You can provide a Acorn
configuration file to customize the AST generation with Acorn
.
Follow Acorn documentation to provide the configuration options.
$ node-asterer -i /path/to/javascript/file.js -o /output/path/of/file.json -c /path/to/config/file.json
The Acorn
configuration JSON file should look like this.
{
"ecmaVersion": 2020,
"locations": true,
"sourceType": "script",
"allowReserved": true,
"allowHashBang": true
}
If a custom Acorn
configuration file is not provided, AST generation will be done using the Acorn
default configuration.
The following is the input JavaScript file sample.js
.
const example = "example";
The following is the output JSON file output.json
.
{
"type": "Program",
"start": 0,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 0
}
},
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 26,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 26
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 6,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 25
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 13
}
},
"name": "example"
},
"init": {
"type": "Literal",
"start": 16,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 25
}
},
"value": "example",
"raw": "\"example\""
}
}
],
"kind": "const"
}
],
"sourceType": "script"
}