Pyjsparser
Pyjsparser is a fast JavaScript parser for Python, based on a manual translation of esprima.js. It aims to be a comprehensible JavaScript parser for Python, supporting ECMAScript 5.1 and parts of ECMAScript 6. The library is currently at version 2.7.1, with its last release dating back to April 2019, suggesting a maintenance-level release cadence rather than active development.
Warnings
- gotcha The library's last release was in April 2019. While functional, it has not received updates for newer ECMAScript features beyond ES5.1 and parts of ES6. This means it may not correctly parse modern JavaScript syntax (ES2016+).
- gotcha Although the `pyjsparser` wheel (version 2.7.1) mentions 'py2-none-any.whl', indicating Python 2 compatibility, the library also works with Python 3. However, due to the lack of recent updates, users might encounter compatibility issues or unexpected behavior with very recent Python 3 versions. Ensure thorough testing in your target Python environment.
- gotcha The documentation states support for ECMAScript 5.1 and *parts* of ECMAScript 6. Users should not expect full compatibility with all features introduced in ES6 or any subsequent ECMAScript versions. Missing features might lead to parsing errors or an incomplete AST for modern JavaScript code.
Install
-
pip install pyjsparser
Imports
- parse
from pyjsparser import parse
Quickstart
from pyjsparser import parse
js_code = 'var message = "Hello, Pyjsparser!"; console.log(message);'
ast = parse(js_code)
print(ast)
# Example of accessing a property (structure depends on AST)
# For illustration, this may need adjustment based on the exact AST output
# if ast and ast['type'] == 'Program' and ast['body']:
# first_statement = ast['body'][0]
# if first_statement['type'] == 'VariableDeclaration':
# print(f"Variable declared: {first_statement['declarations'][0]['id']['name']}")