{"id":6619,"library":"esprima","title":"Esprima Python","description":"Esprima Python is a Python port of the Esprima ECMAScript parser. It provides an ECMAScript (JavaScript) parsing infrastructure for multipurpose analysis in Python, generating a syntax tree that adheres to the SpiderMonkey AST format. The current version is 4.0.1, with releases typically following updates to the upstream JavaScript Esprima project or significant internal Python improvements.","status":"active","version":"4.0.1","language":"en","source_language":"en","source_url":"https://github.com/Kronuz/esprima-python","tags":["parser","javascript","ecmascript","ast","static-analysis"],"install":[{"cmd":"pip install esprima","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"The primary function to parse JavaScript/ECMAScript code.","symbol":"parse","correct":"from esprima import parse"},{"note":"For handling parsing errors.","symbol":"EsprimaException","correct":"from esprima.error import EsprimaException"}],"quickstart":{"code":"import esprima\n\njs_code = \"var answer = 42; console.log(answer);\"\n\ntry:\n    # Parse the JavaScript code\n    tree = esprima.parse(js_code, options={'loc': True, 'range': True})\n\n    # The 'tree' object is an AST. It can be converted to a dictionary.\n    ast_dict = tree.to_dict()\n\n    # Print a part of the AST (e.g., the first statement's type)\n    if ast_dict and 'body' in ast_dict and len(ast_dict['body']) > 0:\n        print(f\"Parsed statement type: {ast_dict['body'][0]['type']}\")\n\n    # Example of accessing tokens (if options={'tokens': True} was used)\n    # tokens = esprima.tokenize(js_code)\n    # print(f\"First token: {tokens[0].to_dict()}\")\n\nexcept esprima.error.EsprimaException as e:\n    print(f\"Parsing error: {e}\")","lang":"python","description":"This quickstart demonstrates parsing a simple JavaScript string using `esprima.parse()` and then converting the resulting Abstract Syntax Tree (AST) object to a Python dictionary for inspection. It also shows how to catch parsing-specific exceptions."},"warnings":[{"fix":"Consult the `esprima-python` GitHub repository's README and changelog for detailed API changes when upgrading from Esprima Python 3.x to 4.x.","message":"Major API changes occurred between version 3.x and 4.x. If upgrading from an older major version, review the GitHub README for updated usage patterns. Direct migration without code changes is unlikely to work.","severity":"breaking","affected_versions":"3.x to 4.x"},{"fix":"Replace calls to `ast_object.to_json()` with `ast_object.to_dict()`. If JSON output is required, use `json.dumps(ast_object.to_dict())`.","message":"The `AST.to_json()` method was removed in version 4.0.0. Use `AST.to_dict()` instead for converting the AST object to a Python dictionary, which can then be serialized to JSON using Python's `json` module if needed.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"When working with the AST, refer to the SpiderMonkey Parser API documentation or `esprima-python` examples to understand the expected node types and properties. Do not expect a Python-native AST structure.","message":"Esprima Python generates an AST that conforms to the SpiderMonkey Parser API's AST format for ECMAScript (JavaScript). This means the structure and node types in the resulting AST (e.g., from `tree.to_dict()`) will reflect JavaScript syntax, not Python's native AST or common Pythonic conventions. Users familiar with Python's `ast` module might find the structure different.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}