/JavaScriptASTConverter

A simple demo that converts redundant Abstract Syntax Tree generated by esprima into a simpler tree-structure.

Primary LanguagePython

JavaScript AST Converter

Introduction

A simple demo that converts redundant Abstract Syntax Tree generated by esprima into a simpler tree-structure.

For Example, a node of VariableDeclarator is as follow.

{
    "id": {
        "name": "x",
        "type": "Identifier"
    },
    "init": null,
    "type": "VariableDeclarator"
}

Here we simplify it following these rules.

  1. Each node has only two members, node and children.
  2. In general, assign node with type, and children with the rest.
  3. Ignore string, boolean, null members.

The we have

{
    "node": "VariableDeclarator",
    "children": [
        {
            "node": "Identifier",
            "children": [
                {
                    "node": "a",
                    "children": []
                }
            ]
        }
    ]
}

Requirement

  1. js2py
  2. json

Usage

$   python demo.py

Reference