@@ -9,8 +9,14 @@ import calculateProjectParserOptions from './tsconfig-parser';
9
9
import semver from 'semver' ;
10
10
import ts from 'typescript' ;
11
11
import convert from './ast-converter' ;
12
+ import {
13
+ Extra ,
14
+ ParserOptions ,
15
+ ESTreeToken ,
16
+ ESTreeComment
17
+ } from './temp-types-based-on-js-source' ;
18
+ import { Program } from './estree/spec' ;
12
19
import util from './node-utils' ;
13
- import { Extra , ParserOptions } from './temp-types-based-on-js-source' ;
14
20
15
21
const packageJSON = require ( '../package.json' ) ;
16
22
@@ -24,6 +30,15 @@ const isRunningSupportedTypeScriptVersion = semver.satisfies(
24
30
let extra : Extra ;
25
31
let warnedAboutTSVersion = false ;
26
32
33
+ /**
34
+ * Compute the filename based on the parser options
35
+ *
36
+ * @param options Parser options
37
+ */
38
+ function getFileName ( { jsx } : { jsx ?: boolean } ) {
39
+ return jsx ? 'estree.tsx' : 'estree.ts' ;
40
+ }
41
+
27
42
/**
28
43
* Resets the extra config object
29
44
* @returns {void }
@@ -53,9 +68,15 @@ function resetExtra(): void {
53
68
*/
54
69
function getASTFromProject ( code : string , options : ParserOptions ) {
55
70
return util . firstDefined (
56
- calculateProjectParserOptions ( code , options . filePath , extra ) ,
71
+ calculateProjectParserOptions (
72
+ code ,
73
+ options . filePath || getFileName ( options ) ,
74
+ extra
75
+ ) ,
57
76
( currentProgram : ts . Program ) => {
58
- const ast = currentProgram . getSourceFile ( options . filePath ) ;
77
+ const ast = currentProgram . getSourceFile (
78
+ options . filePath || getFileName ( options )
79
+ ) ;
59
80
return ast && { ast, program : currentProgram } ;
60
81
}
61
82
) ;
@@ -68,7 +89,7 @@ function getASTFromProject(code: string, options: ParserOptions) {
68
89
function createNewProgram ( code : string ) {
69
90
// Even if jsx option is set in typescript compiler, filename still has to
70
91
// contain .tsx file extension
71
- const FILENAME = extra . jsx ? 'estree.tsx' : 'estree.ts' ;
92
+ const FILENAME = getFileName ( extra ) ;
72
93
73
94
const compilerHost = {
74
95
fileExists ( ) {
@@ -141,18 +162,37 @@ function getProgramAndAST(
141
162
// Parser
142
163
//------------------------------------------------------------------------------
143
164
165
+ type AST < T extends ParserOptions > = Program &
166
+ ( T [ 'range' ] extends true ? { range : [ number , number ] } : { } ) &
167
+ ( T [ 'tokens' ] extends true ? { tokens : ESTreeToken [ ] } : { } ) &
168
+ ( T [ 'comment' ] extends true ? { comments : ESTreeComment [ ] } : { } ) ;
169
+
144
170
/**
145
171
* Parses the given source code to produce a valid AST
146
172
* @param {string } code TypeScript code
147
173
* @param {boolean } shouldGenerateServices Flag determining whether to generate ast maps and program or not
148
174
* @param {ParserOptions } options configuration object for the parser
149
175
* @returns {Object } the AST
150
176
*/
151
- function generateAST (
177
+ function generateAST < T extends ParserOptions = ParserOptions > (
152
178
code : string ,
153
- options : ParserOptions ,
179
+ options : T = { } as T ,
154
180
shouldGenerateServices = false
155
- ) : any {
181
+ ) : {
182
+ estree : AST < T > ;
183
+ program : typeof shouldGenerateServices extends true
184
+ ? ts . Program
185
+ : ( ts . Program | undefined ) ;
186
+ astMaps : typeof shouldGenerateServices extends true
187
+ ? {
188
+ esTreeNodeToTSNodeMap : WeakMap < object , any > ;
189
+ tsNodeToESTreeNodeMap : WeakMap < object , any > ;
190
+ }
191
+ : {
192
+ esTreeNodeToTSNodeMap ?: WeakMap < object , any > ;
193
+ tsNodeToESTreeNodeMap ?: WeakMap < object , any > ;
194
+ } ;
195
+ } {
156
196
const toString = String ;
157
197
158
198
if ( typeof code !== 'string' && ! ( ( code as any ) instanceof String ) ) {
@@ -245,7 +285,7 @@ function generateAST(
245
285
estree,
246
286
program : shouldProvideParserServices ? program : undefined ,
247
287
astMaps : shouldProvideParserServices
248
- ? astMaps
288
+ ? astMaps !
249
289
: { esTreeNodeToTSNodeMap : undefined , tsNodeToESTreeNodeMap : undefined }
250
290
} ;
251
291
}
@@ -259,8 +299,11 @@ export { version };
259
299
260
300
const version = packageJSON . version ;
261
301
262
- export function parse ( code : string , options : ParserOptions ) {
263
- return generateAST ( code , options ) . estree ;
302
+ export function parse < T extends ParserOptions = ParserOptions > (
303
+ code : string ,
304
+ options ?: T
305
+ ) {
306
+ return generateAST < T > ( code , options ) . estree ;
264
307
}
265
308
266
309
export function parseAndGenerateServices ( code : string , options : ParserOptions ) {
0 commit comments