PiotrDabkowski / pyjsparser

Fast JavaScript parser for Python.
MIT License
246 stars 37 forks source link

How can I convert the AST back into JS code? #37

Closed halloleo closed 1 year ago

halloleo commented 1 year ago

I want to use pyjsparser to write an identifier renamer. For this I generate an AST from the JScode, modify the AST and then convert it back to JavaScript. I know how to generate the AST with pyjsparser. Can I then somehow generate JavaScript from the AST?

PiotrDabkowski commented 1 year ago

You can use a NPM library called escodegen. You can use my Js2Py library to use it from python, example is here:

https://github.com/PiotrDabkowski/Js2Py/blob/b16d7ce90ac9c03358010c1599c3e87698c9993f/simple_test.py#L131

It will be a bit slow though as this is auto-translated JS code to Python.

halloleo commented 1 year ago

Thanks for the pointer, @PiotrDabkowski.

Will certainly checkout escodegen and I might sue it via your Js2Py.