Skip to content

How to use #2

@mrdeveloperdude

Description

@mrdeveloperdude

Brother, your project is legitimately lit!

I was hoping to (ab)-use it together with esprima to create a simple javascript formatter/minifier in my pure python project (no dependency on nodejs or similar). Unfortunately I am unable to combine the two. So my question is, how do you parse your js to produce input for jscodegen?

If you are curious, here is my proof of concept code;

import esprima
from .jscodegen import CodeGenerator

generator = CodeGenerator(indent = 2)

def js2ast(js: str):
    return esprima.parseScript(js)

def ast2js(ast: dict):
    return generator.generate(ast)

raw = "var lol=123; bob='mystr'; function arne(){console.log(lol, bob);} arne(); alert('hello');"

output = ast2js( js2ast(raw) )

And it fails like this:

output = ast2js( js2ast(raw) )
../__init__.py:26: in ast2js
    return generator.generate(ast)
../jscodegen/__init__.py:430: in generate
    if self.is_statement(node):
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <.jscodegen.CodeGenerator object at 0x7f8037a65c90>
node = {
    type: "Program",
    sourceType: "script",
    body: [
        {
            type: "VariableDeclaration",
      ...,
                    generator: False
                },
                arguments: []
            }
        }
    ]
}

    def is_statement(self, node):
>       return Syntax(node["type"]) in Statements
E       TypeError: 'Script' object is not subscriptable

../jscodegen/__init__.py:392: TypeError

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions