@niweera/node-asterer
Version:
A CLI tool to generate the AST of a given JavaScript File and save it in *.JSON format.
144 lines (127 loc) • 5.27 kB
Markdown
[](https://badge.fury.io/js/%40niweera%2Fnode-asterer)
[](https://www.npmjs.com/package/@niweera/node-asterer)

[](https://codecov.io/gh/Niweera/node-asterer)
[](https://travis-ci.com/Niweera/node-asterer)
[](https://snyk.io/test/github/Niweera/node-asterer?targetFile=package.json)
[](https://david-dm.org/Niweera/node-asterer)
[](https://david-dm.org/Niweera/node-asterer?type=dev)
[](https://lgtm.com/projects/g/Niweera/node-asterer/alerts/)
[](https://lgtm.com/projects/g/Niweera/node-asterer/context:javascript)
# [Node-ASTerer](https://www.npmjs.com/package/@niweera/node-asterer)
Node-ASTerer is a CLI tool to generate the [AST](https://www.digitalocean.com/community/tutorials/js-traversing-ast) of a given JavaScript file, and it will save it in a *.json file.
Node-ASTerer uses [AcornJS](https://github.com/acornjs/acorn) under the hood to generate the AST.
## Usage
```bash
$ npm install -g @niweera/node-asterer
$ node-asterer -i /path/to/javascript/file.js -o /output/path/of/file.json
```
## Advanced Usage
You can provide a `Acorn` configuration file to customize the AST generation with `Acorn`.
Follow [Acorn documentation](https://www.npmjs.com/package/acorn#interface) to provide the configuration options.
```bash
$ 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.
```json
{
"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.
## Example
The following is the input JavaScript file `sample.js`.
```javascript
const example = "example";
```
The following is the output JSON file `output.json`.
```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"
}
```