{"id":14998,"library":"typescript-parser","title":"TypeScript Parser for AST Generation","description":"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.","status":"abandoned","version":"2.6.1","language":"javascript","source_language":"en","source_url":"https://github.com/TypeScript-Heroes/node-typescript-parser","tags":["javascript","typescript","parser","AST","parsing"],"install":[{"cmd":"npm install typescript-parser","lang":"bash","label":"npm"},{"cmd":"yarn add typescript-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add typescript-parser","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"This library relies on the official TypeScript compiler's internal parser. Compatibility with newer TypeScript versions beyond 2018 might be an issue.","package":"typescript","optional":false}],"imports":[{"note":"The library primarily uses ES module syntax in its documentation, but CommonJS might work for older Node.js versions. For modern TypeScript projects, use `import`.","wrong":"const TypescriptParser = require('typescript-parser').TypescriptParser;","symbol":"TypescriptParser","correct":"import { TypescriptParser } from 'typescript-parser';"},{"note":"Specific AST node types are exported, allowing for type-safe manipulation of the parsed structure.","symbol":"AST declarations (e.g., ClassDeclaration)","correct":"import { ClassDeclaration, PropertyDeclaration } from 'typescript-parser';"}],"quickstart":{"code":"import { TypescriptParser } from 'typescript-parser';\n\nasync function parseCode() {\n  const parser = new TypescriptParser();\n\n  const sourceCode = `\n    interface MyInterface {\n      id: string;\n      name?: string;\n    }\n\n    export class MyClass implements MyInterface {\n      private _id: string;\n      public name: string;\n\n      constructor(id: string, name: string) {\n        this._id = id;\n        this.name = name;\n      }\n\n      public get id(): string {\n        return this._id;\n      }\n\n      public greet(): string {\n        return `Hello, ${this.name}!`;\n      }\n    }\n  `;\n\n  try {\n    const parsed = await parser.parseSource(sourceCode);\n    console.log('Parsed declarations:', parsed.declarations.map(d => d.name));\n    // Example: Find the class declaration\n    const myClass = parsed.declarations.find(d => d.name === 'MyClass');\n    if (myClass && 'members' in myClass) {\n      console.log('MyClass members:', myClass.members.map((m: any) => m.name));\n    }\n  } catch (error) {\n    console.error('Error parsing source:', error);\n  }\n}\n\nparseCode();\n","lang":"typescript","description":"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."},"warnings":[{"fix":"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.","message":"The package has not been updated since August 2018. This means it may not correctly parse newer TypeScript syntax features (e.g., optional chaining, nullish coalescing, private class fields, import assertions) introduced in TypeScript versions 3.x, 4.x, or 5.x.","severity":"gotcha","affected_versions":">=2.6.1"},{"fix":"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.","message":"Performance for very large files or projects might be a concern. The library's internal parsing mechanism (recursive vs. iterative BTree Walker) has seen changes in minor versions, which could affect performance characteristics, especially in older versions.","severity":"gotcha","affected_versions":"<=2.6.1"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"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.","cause":"This error was reported in older versions due to an issue with the parser when encountering specific TypeScript constructs, especially with malformed or unexpected input.","error":"TypeError: Cannot read properties of undefined (reading 'length')"},{"fix":"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.","cause":"The `typescript-parser` package was not found in `node_modules`, likely due to incorrect installation or usage in an environment where dependencies are not properly resolved.","error":"Error: Cannot find module 'typescript-parser'"}],"ecosystem":"npm"}