0
# Core Parsing
1
2
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.
3
4
## Capabilities
5
6
### Parse Function
7
8
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.
9
10
```typescript { .api }
11
/**
12
* Parses TypeScript source code into ESTree-compatible AST
13
* @param code - TypeScript source code as string
14
* @param options - Optional parsing configuration
15
* @returns ESTree-compatible AST with optional comments and tokens
16
*/
17
function parse<T extends TSESTreeOptions = TSESTreeOptions>(
18
code: string,
19
options?: T
20
): AST<T>;
21
```
22
23
**Usage Examples:**
24
25
```typescript
26
import { parse } from "@typescript-eslint/typescript-estree";
27
28
// Basic parsing
29
const ast = parse('const x: number = 42;');
30
31
// With location and range information
32
const astWithLoc = parse('const x: number = 42;', {
33
loc: true,
34
range: true
35
});
36
37
// Include comments and tokens
38
const astWithTokens = parse('/* comment */ const x = 42;', {
39
comment: true,
40
tokens: true,
41
loc: true,
42
range: true
43
});
44
```
45
46
### Parse and Generate Services Function
47
48
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.
49
50
```typescript { .api }
51
/**
52
* Parses TypeScript code and generates parser services with type information
53
* @param code - TypeScript source code as string or ts.SourceFile
54
* @param options - Parsing configuration with project settings
55
* @returns Object containing both AST and parser services
56
*/
57
function parseAndGenerateServices<T extends TSESTreeOptions = TSESTreeOptions>(
58
code: string | ts.SourceFile,
59
options: T
60
): ParseAndGenerateServicesResult<T>;
61
```
62
63
**Usage Examples:**
64
65
```typescript
66
import { parseAndGenerateServices } from "@typescript-eslint/typescript-estree";
67
68
// Parse with TypeScript project
69
const result = parseAndGenerateServices('const x: number = 42;', {
70
project: './tsconfig.json',
71
loc: true,
72
range: true
73
});
74
75
// Access type information
76
if (result.services.program) {
77
const checker = result.services.program.getTypeChecker();
78
// Use TypeScript's type checker...
79
}
80
81
// Parse single file without project
82
const isolatedResult = parseAndGenerateServices('const x = 42;', {
83
loc: true,
84
range: true,
85
jsx: false
86
});
87
```
88
89
### Cache Clearing Functions
90
91
Functions for clearing internal caches. Useful for testing and memory management in long-running processes.
92
93
```typescript { .api }
94
/**
95
* Clears the program cache used for single-run scenarios
96
* @deprecated Use clearCaches() instead
97
*/
98
function clearProgramCache(): void;
99
100
/**
101
* Clears all program caches and matched files for fresh parsing state
102
*/
103
function clearDefaultProjectMatchedFiles(): void;
104
105
/**
106
* Clears parse and generate services call tracking
107
* @internal For testing purposes only
108
*/
109
function clearParseAndGenerateServicesCalls(): void;
110
```
111
112
**Usage Examples:**
113
114
```typescript
115
import { clearCaches } from "@typescript-eslint/typescript-estree";
116
117
// Clear all caches (recommended)
118
clearCaches();
119
120
// In testing scenarios
121
beforeEach(() => {
122
clearCaches();
123
});
124
```
125
126
## Result Types
127
128
```typescript { .api }
129
// Conditional AST type based on options
130
type AST<T extends TSESTreeOptions> =
131
(T['comment'] extends true ? { comments: TSESTree.Comment[] } : {}) &
132
(T['tokens'] extends true ? { tokens: TSESTree.Token[] } : {}) &
133
TSESTree.Program;
134
135
// Result from parseAndGenerateServices
136
interface ParseAndGenerateServicesResult<T extends TSESTreeOptions> {
137
ast: AST<T>;
138
services: ParserServices;
139
}
140
141
// Internal result type with node maps
142
interface ParseWithNodeMapsResult<T extends TSESTreeOptions> {
143
ast: AST<T>;
144
esTreeNodeToTSNodeMap: ParserWeakMapESTreeToTSNode;
145
tsNodeToESTreeNodeMap: ParserWeakMap<TSNode | TSToken, TSESTree.Node>;
146
}
147
```