ast-parentchild

0.1.9 • Public • Published

Ast-ParentChild

Ast-ParentChild

PyPI version

Ast-Parent-Child is a CLI tool to generate the 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 under the hood to generate the AST.

Usage

$ npm i ast-parentchild
$ ast-parentchild  /path/to/javascript/file.js  /output/path/of/file.json

Example

The following is the input JavaScript file sample.js.

const example = "example";

The following is the output JSON file output.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": []
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

Package Sidebar

Install

npm i ast-parentchild

Weekly Downloads

0

Version

0.1.9

License

ISC

Unpacked Size

12.9 kB

Total Files

15

Last publish

Collaborators

  • ffalpha