@@ -29,54 +29,140 @@ yarn add -D @typescript-eslint/typescript-estree
29
29
30
30
## API
31
31
32
- ### parse(code, options)
32
+ ### Parsing
33
33
34
- Parses the given string of code with the options provided and returns an ESTree-compatible AST. The options object has the following properties:
34
+ #### ` parse( code, options) `
35
35
36
- ``` js
37
- {
38
- // attach range information to each node
39
- range: false ,
36
+ Parses the given string of code with the options provided and returns an ESTree-compatible AST.
40
37
41
- // attach line/column location information to each node
42
- loc: false ,
38
+ ``` ts
39
+ interface ParseOptions {
40
+ /**
41
+ * create a top-level comments array containing all comments
42
+ */
43
+ comment? : boolean ;
43
44
44
- // create a top-level tokens array containing all tokens
45
- tokens: false ,
45
+ /**
46
+ * An array of modules to turn explicit debugging on for.
47
+ * - 'typescript-eslint' is the same as setting the env var `DEBUG=typescript-eslint:*`
48
+ * - 'eslint' is the same as setting the env var `DEBUG=eslint:*`
49
+ * - 'typescript' is the same as setting `extendedDiagnostics: true` in your tsconfig compilerOptions
50
+ *
51
+ * For convenience, also supports a boolean:
52
+ * - true === ['typescript-eslint']
53
+ * - false === []
54
+ */
55
+ debugLevel? : boolean | (' typescript-eslint' | ' eslint' | ' typescript' )[];
46
56
47
- // create a top-level comments array containing all comments
48
- comment: false ,
57
+ /**
58
+ * Cause the parser to error if it encounters an unknown AST node type (useful for testing).
59
+ * This case only usually occurs when TypeScript releases new features.
60
+ */
61
+ errorOnUnknownASTType? : boolean ;
49
62
50
- /*
51
- * enable parsing JSX. For more details, see https://www.typescriptlang.org/docs/handbook/jsx.html
63
+ /**
64
+ * The absolute path to the file being parsed.
65
+ */
66
+ filePath? : string ;
67
+
68
+ /**
69
+ * Enable parsing of JSX.
70
+ * For more details, see https://www.typescriptlang.org/docs/handbook/jsx.html
52
71
*
53
72
* NOTE: this setting does not effect known file types (.js, .jsx, .ts, .tsx, .json) because the
54
73
* TypeScript compiler has its own internal handling for known file extensions.
55
74
*
56
- * Exact behaviour:
57
- * - .js, .jsx, .tsx files are parsed as if this is true
58
- * - .ts files are parsed as if this is false
59
- * - unknown extensions (.md, .vue) will respect this setting
75
+ * For the exact behavior, see https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/parser#parseroptionsecmafeaturesjsx
60
76
*/
61
- jsx: false ,
77
+ jsx? : boolean ;
78
+
79
+ /**
80
+ * Controls whether the `loc` information to each node.
81
+ * The `loc` property is an object which contains the exact line/column the node starts/ends on.
82
+ * This is similar to the `range` property, except it is line/column relative.
83
+ */
84
+ loc? : boolean ;
85
+
86
+ /*
87
+ * Allows overriding of function used for logging.
88
+ * When value is `false`, no logging will occur.
89
+ * When value is not provided, `console.log()` will be used.
90
+ */
91
+ loggerFn? : Function | false ;
92
+
93
+ /**
94
+ * Controls whether the `range` property is included on AST nodes.
95
+ * The `range` property is a [number, number] which indicates the start/end index of the node in the file contents.
96
+ * This is similar to the `loc` property, except this is the absolute index.
97
+ */
98
+ range? : boolean ;
99
+
100
+ /**
101
+ * Set to true to create a top-level array containing all tokens from the file.
102
+ */
103
+ tokens? : boolean ;
62
104
63
105
/*
64
106
* The JSX AST changed the node type for string literals
65
107
* inside a JSX Element from `Literal` to `JSXText`.
66
108
* When value is `true`, these nodes will be parsed as type `JSXText`.
67
109
* When value is `false`, these nodes will be parsed as type `Literal`.
68
110
*/
69
- useJSXTextNode: false ,
111
+ useJSXTextNode? : boolean ;
112
+ }
70
113
71
- // Cause the parser to error if it encounters an unknown AST node type (useful for testing)
114
+ const PARSE_DEFAULT_OPTIONS: ParseOptions = {
115
+ comment: false ,
72
116
errorOnUnknownASTType: false ,
117
+ filePath: ' estree.ts' , // or 'estree.tsx', if you pass jsx: true
118
+ jsx: false ,
119
+ loc: false ,
120
+ loggerFn: undefined ,
121
+ range: false ,
122
+ tokens: false ,
123
+ useJSXTextNode: false ,
124
+ };
73
125
74
- /*
75
- * Allows overriding of function used for logging.
76
- * When value is `false`, no logging will occur.
77
- * When value is not provided, `console.log()` will be used.
126
+ declare function parse(
127
+ code : string ,
128
+ options : ParseOptions = PARSE_DEFAULT_OPTIONS ,
129
+ ): TSESTree .Program ;
130
+ ```
131
+
132
+ Example usage:
133
+
134
+ ``` js
135
+ import { parse } from ' @typescript-eslint/typescript-estree' ;
136
+
137
+ const code = ` const hello: string = 'world';` ;
138
+ const ast = parse (code, {
139
+ loc: true ,
140
+ range: true ,
141
+ });
142
+ ```
143
+
144
+ #### ` parseAndGenerateServices(code, options) `
145
+
146
+ Parses the given string of code with the options provided and returns an ESTree-compatible AST. Accepts additional options which can be used to generate type information along with the AST.
147
+
148
+ ``` ts
149
+ interface ParseAndGenerateServicesOptions extends ParseOptions {
150
+ /**
151
+ * Causes the parser to error if the TypeScript compiler returns any unexpected syntax/semantic errors.
78
152
*/
79
- loggerFn: undefined ,
153
+ errorOnTypeScriptSyntacticAndSemanticIssues? : boolean ;
154
+
155
+ /**
156
+ * When `project` is provided, this controls the non-standard file extensions which will be parsed.
157
+ * It accepts an array of file extensions, each preceded by a `.`.
158
+ */
159
+ extraFileExtensions? : string [];
160
+
161
+ /**
162
+ * The absolute path to the file being parsed.
163
+ * When `project` is provided, this is required, as it is used to fetch the file from the TypeScript compiler's cache.
164
+ */
165
+ filePath? : string ;
80
166
81
167
/**
82
168
* Allows the user to control whether or not two-way AST node maps are preserved
@@ -88,42 +174,67 @@ Parses the given string of code with the options provided and returns an ESTree-
88
174
* NOTE: If `preserveNodeMaps` is explicitly set by the user, it will be respected,
89
175
* regardless of whether or not `project` is in use.
90
176
*/
91
- preserveNodeMaps: undefined
177
+ preserveNodeMaps? : boolean ;
178
+
179
+ /**
180
+ * Absolute (or relative to `tsconfigRootDir`) paths to the tsconfig(s).
181
+ * If this is provided, type information will be returned.
182
+ */
183
+ project? : string | string [];
184
+
185
+ /**
186
+ * The absolute path to the root directory for all provided `project`s.
187
+ */
188
+ tsconfigRootDir? : string ;
189
+
190
+ /**
191
+ ***************************************************************************************
192
+ * IT IS RECOMMENDED THAT YOU DO NOT USE THIS OPTION, AS IT CAUSES PERFORMANCE ISSUES. *
193
+ ***************************************************************************************
194
+ *
195
+ * When passed with `project`, this allows the parser to create a catch-all, default program.
196
+ * This means that if the parser encounters a file not included in any of the provided `project`s,
197
+ * it will not error, but will instead parse the file and its dependencies in a new program.
198
+ */
199
+ createDefaultProgram? : boolean ;
92
200
}
201
+
202
+ const PARSE_AND_GENERATE_SERVICES_DEFAULT_OPTIONS: ParseOptions = {
203
+ ... PARSE_DEFAULT_OPTIONS ,
204
+ errorOnTypeScriptSyntacticAndSemanticIssues: false ,
205
+ extraFileExtensions: [],
206
+ preserveNodeMaps: false , // or true, if you do not set this, but pass `project`
207
+ project: undefined ,
208
+ tsconfigRootDir: process .cwd (),
209
+ };
210
+
211
+ declare function parseAndGenerateServices(
212
+ code : string ,
213
+ options : ParseOptions = PARSE_DEFAULT_OPTIONS ,
214
+ ): TSESTree .Program ;
93
215
```
94
216
95
217
Example usage:
96
218
97
219
``` js
98
- const parser = require (' @typescript-eslint/typescript-estree' );
220
+ import { parseAndGenerateServices } from ' @typescript-eslint/typescript-estree' ;
221
+
99
222
const code = ` const hello: string = 'world';` ;
100
- const ast = parser . parse (code, {
101
- range : true ,
223
+ const ast = parseAndGenerateServices (code, {
224
+ filePath : ' /some/path/to/file/foo.ts ' ,
102
225
loc: true ,
226
+ project: ' ./tsconfig.json' ,
227
+ range: true ,
103
228
});
104
229
```
105
230
106
- ### version
231
+ ### ` TSESTree ` , ` AST_NODE_TYPES ` and ` AST_TOKEN_TYPES `
107
232
108
- Exposes the current version of ` typescript-estree ` as specified in ` package.json ` .
233
+ Types for the AST produced by the parse functions .
109
234
110
- Example usage:
111
-
112
- ``` js
113
- const parser = require (' @typescript-eslint/typescript-estree' );
114
- const version = parser .version ;
115
- ```
116
-
117
- ### ` AST_NODE_TYPES `
118
-
119
- Exposes an object that contains the AST node types produced by the parser.
120
-
121
- Example usage:
122
-
123
- ``` js
124
- const parser = require (' @typescript-eslint/typescript-estree' );
125
- const astNodeTypes = parser .AST_NODE_TYPES ;
126
- ```
235
+ - ` TSESTree ` is a namespace which contains object types representing all of the AST Nodes produced by the parser.
236
+ - ` AST_NODE_TYPES ` is an enum which provides the values for every single AST node's ` type ` property.
237
+ - ` AST_TOKEN_TYPES ` is an enum which provides the values for every single AST token's ` type ` property.
127
238
128
239
## Supported TypeScript Version
129
240
0 commit comments