The `typescript-parser` package provides a utility for parsing TypeScript and JavaScript files to generate a 'human-readable' Abstract Syntax Tree (AST). It leverages the official TypeScript compiler's parsing capabilities internally. The current stable version is 2.6.1, released in August 2018. Due to the lack of activity since then, its release cadence is effectively halted. Its key differentiator lies in its promise of a more accessible AST representation compared to the raw TypeScript compiler AST, aiming to simplify consumption for tools that need to analyze code structure, such as IDE extensions or code generators. It primarily operates by parsing either source code strings or file paths, and can also build a declaration index for multiple files.
npm install typescript-parserVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to instantiate the `TypescriptParser` and use it to parse a string of TypeScript source code, then log the names of the top-level declarations and members of a specific class found in the AST.
Consider alternative, actively maintained TypeScript AST parsers if working with modern TypeScript syntax, or fork and update the parsing logic to a newer TypeScript compiler version.
Profile performance in your specific use case. If parsing many files, consider using `parser.parseFiles` which might be optimized for batch operations, or explore other parsers if bottlenecks occur.
Update to at least `v2.4.0` where a fix for 'typescript error that reads length of undefined' was applied. Ensure input source code is valid TypeScript/JavaScript syntax.
Run `npm install typescript-parser` or `yarn add typescript-parser` in your project directory. Ensure your `node_modules` are accessible and environment variables like `NODE_PATH` are correctly configured if not using standard resolution.