extremeheat / JSPyBridge

🌉. Bridge to interoperate Node.js and Python
MIT License
694 stars 53 forks source link

Validating JS Syntax #86

Open oroojlooy opened 1 year ago

oroojlooy commented 1 year ago

I have bunch of JavaScript functions, all loaded in memory inside a python code, and I need to validate their syntax without writing them down in .js files. I was looking for a python package that could perform this task for me, and found this package. I tried to use javascript package for my goal, but was not sure if there is any way that I could only check syntax with that in python.

P.S.: JSHint/prettier work OK if I write each function in a file and then call something like os.system("jshint --verbose sample.js"), but writing the js functions in file is expensive and takes a lot of time. So, looking for a method/package that can do the same without writing the codes in file.

extremeheat commented 1 year ago

What you're looking for is a JavaScript AST parser library.

From a quick search, https://pypi.org/project/esprima/ is a native Python package on pip that can do the parsing.

If you want to use something from the JS side (that maybe more up to date/maintained), you can checkout the TypeScript AST parser, or use Babel's parser, with JSPyBridge usage demonstrated here:

https://github.com/MineDojo/Voyager/blob/01fb04666a8f3ba47dec74fb4cfd46e0125fe5a0/voyager/agents/action.py#L208-L213