A parser that converts TypeScript source code into an ESTree compatible form
—
Configuration options and services for controlling parsing behavior and accessing TypeScript type information. These options allow fine-grained control over the parsing process and enable integration with TypeScript's type system.
Main configuration interface for parsing options, controlling output format, TypeScript integration, and performance settings.
/**
* Configuration options for TypeScript ESTree parsing
*/
interface TSESTreeOptions {
/** Controls whether location information is included on AST nodes */
loc?: boolean;
/** Controls whether range information is included on AST nodes */
range?: boolean;
/** Set to true to create a top-level array containing all tokens */
tokens?: boolean;
/** Create a top-level comments array containing all comments */
comment?: boolean;
/** Enable parsing of JSX syntax */
jsx?: boolean;
/** Absolute path to the file being parsed */
filePath?: string;
/** Specify the source type (script or module) */
sourceType?: SourceType;
/** Allows invalid AST from TypeScript for error recovery */
allowInvalidAST?: boolean;
/** Error if unknown AST node type encountered */
errorOnUnknownASTType?: boolean;
/** Error on TypeScript syntax and semantic issues */
errorOnTypeScriptSyntacticAndSemanticIssues?: boolean;
/** Custom logging function or false to disable */
loggerFn?: ((message: string) => void) | false;
/** Suppress deprecated property warnings */
suppressDeprecatedPropertyWarnings?: boolean;
/** Debug level configuration */
debugLevel?: DebugLevel;
/** JSDoc parsing mode for TypeScript 5.3+ */
jsDocParsingMode?: JSDocParsingMode;
}Options for integrating with TypeScript projects and enabling type information services.
/**
* Additional options for parseAndGenerateServices with project integration
*/
interface ParseAndGenerateServicesOptions extends TSESTreeOptions {
/** Path(s) to tsconfig.json or true to find nearest */
project?: boolean | string | string[] | null;
/** Root directory for resolving project paths */
tsconfigRootDir?: string;
/** Folders to ignore when resolving project globs */
projectFolderIgnoreList?: string[];
/** Whether to create a shared TypeScript project service */
projectService?: boolean | ProjectServiceOptions;
/** Non-standard file extensions to parse */
extraFileExtensions?: string[];
/** Array of TypeScript Program instances to use */
programs?: ts.Program[] | null;
/** Control AST node maps preservation */
preserveNodeMaps?: boolean;
/** Disable automatic single-run inference for performance */
disallowAutomaticSingleRunInference?: boolean;
/** Cache lifetime configuration */
cacheLifetime?: {
glob?: CacheDurationSeconds;
};
}Usage Examples:
import { parseAndGenerateServices, type TSESTreeOptions } from "@typescript-eslint/typescript-estree";
// Basic options
const basicOptions: TSESTreeOptions = {
loc: true,
range: true,
tokens: true,
comment: true,
jsx: false
};
// Project-based options
const projectOptions = {
...basicOptions,
project: './tsconfig.json',
tsconfigRootDir: __dirname,
extraFileExtensions: ['.vue']
};
// Project service options (recommended)
const serviceOptions = {
...basicOptions,
projectService: {
allowDefaultProject: ['*.js'],
maximumDefaultProjectFileMatchCount: 8
}
};Services provided by parseAndGenerateServices for accessing TypeScript type information and AST mappings.
/**
* Base parser services available in all parsing modes
*/
interface ParserServicesBase {
emitDecoratorMetadata: boolean | undefined;
experimentalDecorators: boolean | undefined;
isolatedDeclarations: boolean | undefined;
}
/**
* Node mapping services for converting between ESTree and TypeScript nodes
*/
interface ParserServicesNodeMaps {
esTreeNodeToTSNodeMap: ParserWeakMapESTreeToTSNode;
tsNodeToESTreeNodeMap: ParserWeakMap<TSNode | TSToken, TSESTree.Node>;
}
/**
* Parser services with full TypeScript type information
*/
interface ParserServicesWithTypeInformation extends ParserServicesNodeMaps, ParserServicesBase {
program: ts.Program;
getSymbolAtLocation: (node: TSESTree.Node) => ts.Symbol | undefined;
getTypeAtLocation: (node: TSESTree.Node) => ts.Type;
}
/**
* Parser services without TypeScript type information
*/
interface ParserServicesWithoutTypeInformation extends ParserServicesNodeMaps, ParserServicesBase {
program: null;
}
/**
* Union type of all parser services
*/
type ParserServices =
| ParserServicesWithoutTypeInformation
| ParserServicesWithTypeInformation;Usage Examples:
import { parseAndGenerateServices } from "@typescript-eslint/typescript-estree";
const result = parseAndGenerateServices(code, { project: './tsconfig.json' });
// Check if type information is available
if (result.services.program) {
// TypeScript program is available
const checker = result.services.program.getTypeChecker();
const sourceFile = result.services.program.getSourceFile('file.ts');
// Get type information for nodes
const someNode = result.ast.body[0];
const symbol = result.services.getSymbolAtLocation(someNode);
const type = result.services.getTypeAtLocation(someNode);
}
// Access node mappings
const esTreeNode = result.ast.body[0];
const tsNode = result.services.esTreeNodeToTSNodeMap.get(esTreeNode);Functions for managing parser configuration and project setup.
/**
* Removes project-related options for isolated parsing
* @param opts - Options object to modify
* @returns Options without project-related properties
*/
function withoutProjectParserOptions<Options extends object>(
opts: Options
): Omit<Options, 'EXPERIMENTAL_useProjectService' | 'project' | 'projectService'>;
/**
* Adds a candidate directory for TSConfig root detection
* @param candidate - Directory path to add as candidate
*/
function addCandidateTSConfigRootDir(candidate: string): void;
/**
* Clears all candidate TSConfig root directories
*/
function clearCandidateTSConfigRootDirs(): void;Usage Examples:
import {
withoutProjectParserOptions,
addCandidateTSConfigRootDir
} from "@typescript-eslint/typescript-estree";
// Remove project options for isolated parsing
const isolatedOptions = withoutProjectParserOptions({
project: './tsconfig.json',
projectService: true,
loc: true,
range: true
});
// Result: { loc: true, range: true }
// Add candidate TSConfig directories
addCandidateTSConfigRootDir('/path/to/project');
addCandidateTSConfigRootDir('/another/project');// Weak map interfaces for type safety
interface ParserWeakMap<Key, ValueBase> {
get<Value extends ValueBase>(key: Key): Value;
has(key: unknown): boolean;
}
interface ParserWeakMapESTreeToTSNode<Key extends TSESTree.Node = TSESTree.Node> {
get<KeyBase extends Key>(key: KeyBase): TSESTreeToTSNode<KeyBase>;
has(key: unknown): boolean;
}
// Source type enumeration
type SourceType = 'script' | 'module';
// Debug level configuration
type DebugLevel = boolean | ('typescript-eslint' | 'eslint' | 'typescript')[];
// JSDoc parsing modes for TypeScript 5.3+
type JSDocParsingMode = 'all' | 'none' | 'type-info';
// Cache duration configuration
type CacheDurationSeconds = number | 'Infinity';Install with Tessl CLI
npx tessl i tessl/npm-typescript-eslint--typescript-estree