A parser that converts TypeScript source code into an ESTree compatible form
—
Primary parsing functions that convert TypeScript source code into ESTree-compatible AST format. These functions are the main entry points for parsing TypeScript code with optional type information services.
Parses TypeScript source code and returns an ESTree-compatible AST without type information. This is the fastest parsing mode, suitable when you don't need TypeScript type checking services.
/**
* Parses TypeScript source code into ESTree-compatible AST
* @param code - TypeScript source code as string
* @param options - Optional parsing configuration
* @returns ESTree-compatible AST with optional comments and tokens
*/
function parse<T extends TSESTreeOptions = TSESTreeOptions>(
code: string,
options?: T
): AST<T>;Usage Examples:
import { parse } from "@typescript-eslint/typescript-estree";
// Basic parsing
const ast = parse('const x: number = 42;');
// With location and range information
const astWithLoc = parse('const x: number = 42;', {
loc: true,
range: true
});
// Include comments and tokens
const astWithTokens = parse('/* comment */ const x = 42;', {
comment: true,
tokens: true,
loc: true,
range: true
});Parses TypeScript code and returns both the AST and parser services with optional TypeScript type information. Use this when you need access to TypeScript's type checker and program.
/**
* Parses TypeScript code and generates parser services with type information
* @param code - TypeScript source code as string or ts.SourceFile
* @param options - Parsing configuration with project settings
* @returns Object containing both AST and parser services
*/
function parseAndGenerateServices<T extends TSESTreeOptions = TSESTreeOptions>(
code: string | ts.SourceFile,
options: T
): ParseAndGenerateServicesResult<T>;Usage Examples:
import { parseAndGenerateServices } from "@typescript-eslint/typescript-estree";
// Parse with TypeScript project
const result = parseAndGenerateServices('const x: number = 42;', {
project: './tsconfig.json',
loc: true,
range: true
});
// Access type information
if (result.services.program) {
const checker = result.services.program.getTypeChecker();
// Use TypeScript's type checker...
}
// Parse single file without project
const isolatedResult = parseAndGenerateServices('const x = 42;', {
loc: true,
range: true,
jsx: false
});Functions for clearing internal caches. Useful for testing and memory management in long-running processes.
/**
* Clears the program cache used for single-run scenarios
* @deprecated Use clearCaches() instead
*/
function clearProgramCache(): void;
/**
* Clears all program caches and matched files for fresh parsing state
*/
function clearDefaultProjectMatchedFiles(): void;
/**
* Clears parse and generate services call tracking
* @internal For testing purposes only
*/
function clearParseAndGenerateServicesCalls(): void;Usage Examples:
import { clearCaches } from "@typescript-eslint/typescript-estree";
// Clear all caches (recommended)
clearCaches();
// In testing scenarios
beforeEach(() => {
clearCaches();
});// Conditional AST type based on options
type AST<T extends TSESTreeOptions> =
(T['comment'] extends true ? { comments: TSESTree.Comment[] } : {}) &
(T['tokens'] extends true ? { tokens: TSESTree.Token[] } : {}) &
TSESTree.Program;
// Result from parseAndGenerateServices
interface ParseAndGenerateServicesResult<T extends TSESTreeOptions> {
ast: AST<T>;
services: ParserServices;
}
// Internal result type with node maps
interface ParseWithNodeMapsResult<T extends TSESTreeOptions> {
ast: AST<T>;
esTreeNodeToTSNodeMap: ParserWeakMapESTreeToTSNode;
tsNodeToESTreeNodeMap: ParserWeakMap<TSNode | TSToken, TSESTree.Node>;
}Install with Tessl CLI
npx tessl i tessl/npm-typescript-eslint--typescript-estree