Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def py2js(value):
"""Convert Python code or Python function to JavaScript code."""
if isinstance(value, str):
parsed = ast.parse(value, '', 'exec')
return ';\n'.join([Py2JSVisitor().visit(node) for node in parsed.body])
if isinstance(value, (types.FunctionType, types.MethodType)):
if getattr(value, '__name__', '') in ('', ''):
raise RuntimeError('Anonymous functions not supported')
value = inspect.getsource(value)
module = ast.parse(value, '', 'exec')
func = module.body[0]
return ';\n'.join([Py2JSVisitor().visit(node) for node in func.body])
raise RuntimeError('py2js only supports a code string or function as input')