Angular ESTree Parser
raw JSON →angular-estree-parser is a utility library designed to convert specific Angular template syntax constructs (such as actions, bindings, interpolation expressions, and template bindings) into an Abstract Syntax Tree (AST) that conforms to the ESTree specification. This makes it a critical component for tools like code formatters (e.g., Prettier), linters, and other static analysis tools operating within the Angular ecosystem. The library is currently stable at version 15.4.3 and maintains a reactive release cadence, frequently updating to support new major and minor versions of @angular/compiler as they are released. Its core differentiator lies in its deep integration with @angular/compiler's internal parsing mechanisms, ensuring accuracy and alignment with Angular's evolving syntax, while providing an ESTree-compatible output that is widely understood by the JavaScript tooling ecosystem. Users must explicitly install the appropriate @angular/compiler peer dependency to match their Angular project's version.
Common errors
error Error: Cannot find module '@angular/compiler' or its corresponding type declarations. ↓
@angular/compiler using npm install @angular/compiler or yarn add @angular/compiler. Ensure the version satisfies the angular-estree-parser peer dependency range. error TypeError: ngEstreeParser.parseBinding is not a function ↓
import * as ngEstreeParser from 'angular-estree-parser'; or specific named imports like import { parseBinding } from 'angular-estree-parser'; in an ESM-compatible environment. error Error: The "angular-estree-parser" package requires Node.js version >= 20. Current version: v18.17.0 ↓
nvm install 20 and nvm use 20 or similar version management tools. Warnings
breaking Version 15.0.0 introduced a breaking change by requiring `@angular/compiler` version `21.0.7` or higher. Projects using older `@angular/compiler` versions will need to upgrade or remain on `angular-estree-parser` v14.x. ↓
gotcha The `@angular/compiler` package is a peer dependency and must be explicitly installed alongside `angular-estree-parser`. Forgetting to install it will lead to runtime errors (e.g., 'Cannot find module'). ↓
gotcha The package requires Node.js version 20 or higher, as specified in its `engines` field. Running on older Node.js versions will result in an error upon installation or execution. ↓
gotcha Versions of `angular-estree-parser` prior to `15.4.3` may have had issues with module resolution due to an incorrect `exports` field, potentially affecting compatibility in certain build environments (e.g., specific ESM/CJS interop scenarios). ↓
Install
npm install angular-estree-parser yarn add angular-estree-parser pnpm add angular-estree-parser Imports
- * as ngEstreeParser wrong
const ngEstreeParser = require('angular-estree-parser');correctimport * as ngEstreeParser from 'angular-estree-parser'; - parseBinding wrong
import ngEstreeParser from 'angular-estree-parser'; ngEstreeParser.parseBinding(...);correctimport { parseBinding } from 'angular-estree-parser'; - AST wrong
import { AST } from 'angular-estree-parser';correctimport type { AST } from 'angular-estree-parser';
Quickstart
import * as ngEstreeParser from 'angular-estree-parser';
import type { AST } from 'angular-estree-parser';
// Example: Parse an Angular binding expression
const bindingInput = 'user | uppercase';
const bindingAst: AST = ngEstreeParser.parseBinding(bindingInput);
console.log('Binding AST:', JSON.stringify(bindingAst, null, 2));
// Example: Parse an Angular action expression
const actionInput = 'onClick($event.target.value, item.id)';
const actionAst: AST = ngEstreeParser.parseAction(actionInput);
console.log('Action AST:', JSON.stringify(actionAst, null, 2));
// Example: Parse an Angular interpolation expression
const interpolationInput = '{{ title + " - " + version }}';
const interpolationAst: AST = ngEstreeParser.parseInterpolationExpression(interpolationInput);
console.log('Interpolation AST:', JSON.stringify(interpolationAst, null, 2));