@niweera/node-asterer

0.1.3 • Public • Published

npm version NPM Downloads GitHub codecov Build Status Known Vulnerabilities Dependency Status devDependencies Status Total alerts Language grade: JavaScript

Node-ASTerer

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.

Usage

$ 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 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.

Example

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"
}

Readme

Keywords

Package Sidebar

Install

npm i @niweera/node-asterer

Weekly Downloads

1

Version

0.1.3

License

MIT

Unpacked Size

20.4 kB

Total Files

23

Last publish

Collaborators

  • niweera