shift-parser is an ECMAScript parser that generates an Abstract Syntax Tree (AST) conforming to the Shift format. Currently at version 8.0.0, the library provides distinct functions for parsing ECMAScript scripts and modules, offering capabilities for static analysis and code transformation. Although its README suggests support for ECMA-262, version 6 (ES2015), the underlying Shift AST Specification, which `shift-parser` implements, states support for ECMAScript 2019. This indicates the parser likely handles features up to ES2019. The package is generally stable, with major versions released periodically rather than on a strict cadence. A key differentiator is its adherence to the machine-readable Shift AST format, which promotes consistency across various JavaScript tooling. It also offers an extended interface for capturing detailed location and comment information using `WeakMap`.
npm install shift-parserVerified import paths — ran on the pinned version, not inferred.
Demonstrates parsing an ECMAScript module, accessing the AST, retrieving location data for nodes, and extracting comments.
Assume broader modern ECMAScript feature support (up to ES2019) than stated in the README for `shift-parser` v8.x. Always test with specific features if uncertain.
Always use `parseModule` for ECMAScript modules (files with `import`/`export` statements) and `parseScript` for standard scripts to avoid parsing errors related to context.
Ensure that the JavaScript environment where `shift-parser` is executed supports `WeakMap` if location tracking is a critical feature. For most modern Node.js and browser targets, this is not an issue.
Use `import { parseModule } from 'shift-parser';` or `import { parseModuleWithLocation } from 'shift-parser';` for code containing `import`/`export` statements.Review the input JavaScript code for syntax errors. `shift-parser` performs strict parsing and will throw an error on malformed code.
To retrieve location and comment information, ensure you are using `parseScriptWithLocation` or `parseModuleWithLocation`. Verify `WeakMap` support in your runtime environment if this error persists.