@@ -3,21 +3,34 @@ import * as dom from 'dts-dom';
3
3
import { InstanceOfResolver } from './index' ;
4
4
import * as types from './types' ;
5
5
6
- export function createTypings ( moduleName : string | null , ast : any ,
6
+ export interface AstQuery {
7
+ query ( query : string ) : any [ ] ;
8
+ querySubtree ( subtree : any , query : string ) : any [ ] ;
9
+ }
10
+
11
+ export function createTypings ( moduleName : string | null , programAst : any ,
7
12
instanceOfResolver : InstanceOfResolver | undefined ) : string {
8
13
const astq = new ASTQ ( ) ;
9
- const reactComponentName = getReactComponentName ( astq , ast ) ;
10
- const propTypesName = getPropTypesName ( astq , ast ) ;
11
- const importedTypes = getInstanceOfPropTypes ( astq , ast , propTypesName ) ;
12
- const importStatements = getImportStatements ( astq , ast , importedTypes , instanceOfResolver ) ;
14
+ const ast = {
15
+ query ( query : string ) : any [ ] {
16
+ return astq . query ( programAst , query ) ;
17
+ } ,
18
+ querySubtree ( subtree : any , query : string ) : any [ ] {
19
+ return astq . query ( subtree , query ) ;
20
+ }
21
+ } ;
22
+ const reactComponentName = getReactComponentName ( ast ) ;
23
+ const propTypesName = getPropTypesName ( ast ) ;
24
+ const importedTypes = getInstanceOfPropTypes ( ast , propTypesName ) ;
25
+ const importStatements = getImportStatements ( ast , importedTypes , instanceOfResolver ) ;
13
26
const componentNames = getUniqueNames ( [
14
- ...getComponentNamesByPropTypeAssignment ( astq , ast ) ,
15
- ...getComponentNamesByStaticPropTypeAttribute ( astq , ast ) ,
16
- ...getComponentNamesByJsxInBody ( astq , ast )
27
+ ...getComponentNamesByPropTypeAssignment ( ast ) ,
28
+ ...getComponentNamesByStaticPropTypeAttribute ( ast ) ,
29
+ ...getComponentNamesByJsxInBody ( ast )
17
30
] ) ;
18
31
19
32
const m = dom . create . module ( moduleName || 'moduleName' ) ;
20
- if ( hasReactClass ( astq , ast , reactComponentName ) ) {
33
+ if ( hasReactClass ( ast , reactComponentName ) ) {
21
34
m . members . push ( dom . create . importAll ( 'React' , 'react' ) ) ;
22
35
}
23
36
if ( importStatements . length > 0 ) {
@@ -30,11 +43,11 @@ export function createTypings(moduleName: string|null, ast: any,
30
43
} ) ;
31
44
}
32
45
componentNames . forEach ( componentName => {
33
- const exportType = getComponentExportType ( astq , ast , componentName ) ;
34
- const propTypes = getPropTypesFromAssignment ( astq , ast , componentName ) ||
35
- getPropTypesFromStaticAttribute ( astq , ast , componentName ) ;
46
+ const exportType = getComponentExportType ( ast , componentName ) ;
47
+ const propTypes = getPropTypesFromAssignment ( ast , componentName ) ||
48
+ getPropTypesFromStaticAttribute ( ast , componentName ) ;
36
49
if ( exportType ) {
37
- createExportedTypes ( m , astq , ast , componentName , reactComponentName , propTypes , propTypesName , exportType ) ;
50
+ createExportedTypes ( m , ast , componentName , reactComponentName , propTypes , propTypesName , exportType ) ;
38
51
}
39
52
} ) ;
40
53
@@ -47,15 +60,15 @@ export function createTypings(moduleName: string|null, ast: any,
47
60
}
48
61
} ;
49
62
50
- function createExportedTypes ( m : dom . ModuleDeclaration , astq : ASTQ , ast : any , componentName : string ,
63
+ function createExportedTypes ( m : dom . ModuleDeclaration , ast : AstQuery , componentName : string ,
51
64
reactComponentName : string | undefined , propTypes : any , propTypesName : string | undefined ,
52
65
exportType : dom . DeclarationFlags ) : void {
53
- const classComponent = isClassComponent ( astq , ast , componentName , reactComponentName ) ;
66
+ const classComponent = isClassComponent ( ast , componentName , reactComponentName ) ;
54
67
55
68
const interf = dom . create . interface ( `${ componentName } Props` ) ;
56
69
interf . flags = dom . DeclarationFlags . Export ;
57
70
if ( propTypes ) {
58
- createPropTypeTypings ( interf , astq , propTypes , propTypesName ) ;
71
+ createPropTypeTypings ( interf , ast , propTypes , propTypesName ) ;
59
72
}
60
73
if ( propTypes || classComponent ) {
61
74
m . members . push ( interf ) ;
@@ -74,13 +87,13 @@ function createExportedTypes(m: dom.ModuleDeclaration, astq: ASTQ, ast: any, com
74
87
}
75
88
}
76
89
77
- function createPropTypeTypings ( interf : dom . InterfaceDeclaration , astq : ASTQ , propTypes : any ,
90
+ function createPropTypeTypings ( interf : dom . InterfaceDeclaration , ast : AstQuery , propTypes : any ,
78
91
propTypesName : string | undefined ) : void {
79
- const res = astq . query ( propTypes , `
92
+ const res = ast . querySubtree ( propTypes , `
80
93
/ ObjectProperty
81
94
` ) ;
82
95
res . forEach ( propertyAst => {
83
- const typeDecl = types . get ( astq , propertyAst . value , propTypesName ) ;
96
+ const typeDecl = types . get ( ast , propertyAst . value , propTypesName ) ;
84
97
const property = dom . create . property ( propertyAst . key . name || propertyAst . key . value , typeDecl . type ,
85
98
typeDecl . optional ? dom . DeclarationFlags . Optional : 0 ) ;
86
99
if ( propertyAst . leadingComments && propertyAst . leadingComments [ 0 ] . type === 'CommentBlock' ) {
@@ -122,8 +135,8 @@ export function propTypeQueryExpression(propTypesName: string|undefined): string
122
135
` ;
123
136
}
124
137
125
- function getReactComponentName ( astq : ASTQ , ast : any ) : string | undefined {
126
- const res = astq . query ( ast , `
138
+ function getReactComponentName ( ast : AstQuery ) : string | undefined {
139
+ const res = ast . query ( `
127
140
// ImportDeclaration[
128
141
/:source StringLiteral[@value == 'react']
129
142
]
@@ -138,8 +151,8 @@ function getReactComponentName(astq: ASTQ, ast: any): string|undefined {
138
151
return undefined ;
139
152
}
140
153
141
- function getPropTypesName ( astq : ASTQ , ast : any ) : string | undefined {
142
- const res = astq . query ( ast , `
154
+ function getPropTypesName ( ast : AstQuery ) : string | undefined {
155
+ const res = ast . query ( `
143
156
// ImportDeclaration[
144
157
/:source StringLiteral[@value == 'react']
145
158
]
@@ -154,8 +167,8 @@ function getPropTypesName(astq: ASTQ, ast: any): string|undefined {
154
167
return undefined ;
155
168
}
156
169
157
- function hasReactClass ( astq : ASTQ , ast : any , reactComponentName : string | undefined ) : boolean {
158
- const res = astq . query ( ast , `
170
+ function hasReactClass ( ast : AstQuery , reactComponentName : string | undefined ) : boolean {
171
+ const res = ast . query ( `
159
172
// ClassDeclaration[
160
173
'${ reactComponentName } ' == 'undefined'
161
174
?
@@ -184,8 +197,8 @@ function hasReactClass(astq: ASTQ, ast: any, reactComponentName: string|undefine
184
197
return res . length > 0 ;
185
198
}
186
199
187
- function getInstanceOfPropTypes ( astq : ASTQ , ast : any , propTypesName : string | undefined ) : string [ ] {
188
- const res = astq . query ( ast , `
200
+ function getInstanceOfPropTypes ( ast : AstQuery , propTypesName : string | undefined ) : string [ ] {
201
+ const res = ast . query ( `
189
202
// CallExpression[
190
203
/:callee MemberExpression[
191
204
(${ propTypeQueryExpression ( propTypesName ) } )
@@ -203,10 +216,10 @@ interface ImportStatement {
203
216
local : string ;
204
217
path : string ;
205
218
}
206
- function getImportStatements ( astq : ASTQ , ast : any , typeNames : string [ ] ,
219
+ function getImportStatements ( ast : AstQuery , typeNames : string [ ] ,
207
220
instanceOfResolver : InstanceOfResolver | undefined ) : ImportStatement [ ] {
208
221
return typeNames . map ( name => {
209
- const res = astq . query ( ast , `
222
+ const res = ast . query ( `
210
223
// ImportDeclaration[
211
224
/:specifiers * /:local Identifier[@name == '${ name } ']
212
225
]
@@ -233,8 +246,8 @@ function getImportStatements(astq: ASTQ, ast: any, typeNames: string[],
233
246
. filter ( importStatement => Boolean ( importStatement . path ) ) ;
234
247
}
235
248
236
- function getComponentNamesByPropTypeAssignment ( astq : ASTQ , ast : any ) : string [ ] {
237
- const res = astq . query ( ast , `
249
+ function getComponentNamesByPropTypeAssignment ( ast : AstQuery ) : string [ ] {
250
+ const res = ast . query ( `
238
251
//AssignmentExpression
239
252
/:left MemberExpression[
240
253
/:object Identifier &&
@@ -247,8 +260,8 @@ function getComponentNamesByPropTypeAssignment(astq: ASTQ, ast: any): string[] {
247
260
return [ ] ;
248
261
}
249
262
250
- function getComponentNamesByStaticPropTypeAttribute ( astq : ASTQ , ast : any ) : string [ ] {
251
- const res = astq . query ( ast , `
263
+ function getComponentNamesByStaticPropTypeAttribute ( ast : AstQuery ) : string [ ] {
264
+ const res = ast . query ( `
252
265
//ClassDeclaration[
253
266
/:body * //ClassProperty /:key Identifier[@name == 'propTypes']
254
267
]
@@ -259,8 +272,8 @@ function getComponentNamesByStaticPropTypeAttribute(astq: ASTQ, ast: any): strin
259
272
return [ ] ;
260
273
}
261
274
262
- function getComponentNamesByJsxInBody ( astq : ASTQ , ast : any ) : string [ ] {
263
- const res = astq . query ( ast , `
275
+ function getComponentNamesByJsxInBody ( ast : AstQuery ) : string [ ] {
276
+ const res = ast . query ( `
264
277
// ClassDeclaration[
265
278
/:body * //JSXElement
266
279
],
@@ -278,8 +291,8 @@ function getComponentNamesByJsxInBody(astq: ASTQ, ast: any): string[] {
278
291
return [ ] ;
279
292
}
280
293
281
- function getPropTypesFromAssignment ( astq : ASTQ , ast : any , componentName : string ) : any | undefined {
282
- const res = astq . query ( ast , `
294
+ function getPropTypesFromAssignment ( ast : AstQuery , componentName : string ) : any | undefined {
295
+ const res = ast . query ( `
283
296
//AssignmentExpression[
284
297
/:left MemberExpression[
285
298
/:object Identifier[@name == '${ componentName } '] &&
@@ -293,8 +306,8 @@ function getPropTypesFromAssignment(astq: ASTQ, ast: any, componentName: string)
293
306
return undefined ;
294
307
}
295
308
296
- function getPropTypesFromStaticAttribute ( astq : ASTQ , ast : any , componentName : string ) : any | undefined {
297
- const res = astq . query ( ast , `
309
+ function getPropTypesFromStaticAttribute ( ast : AstQuery , componentName : string ) : any | undefined {
310
+ const res = ast . query ( `
298
311
//ClassDeclaration[
299
312
/:id Identifier[@name == '${ componentName } ']
300
313
]
@@ -310,8 +323,8 @@ function getPropTypesFromStaticAttribute(astq: ASTQ, ast: any, componentName: st
310
323
return undefined ;
311
324
}
312
325
313
- function getComponentExportType ( astq : ASTQ , ast : any , componentName : string ) : dom . DeclarationFlags | undefined {
314
- let res = astq . query ( ast , `
326
+ function getComponentExportType ( ast : AstQuery , componentName : string ) : dom . DeclarationFlags | undefined {
327
+ let res = ast . query ( `
315
328
// ExportDefaultDeclaration[
316
329
// ClassDeclaration
317
330
/:id Identifier[@name == '${ componentName } ']
@@ -337,7 +350,7 @@ function getComponentExportType(astq: ASTQ, ast: any, componentName: string): do
337
350
if ( res . length > 0 ) {
338
351
return dom . DeclarationFlags . ExportDefault ;
339
352
}
340
- res = astq . query ( ast , `
353
+ res = ast . query ( `
341
354
// ExportNamedDeclaration[
342
355
// ClassDeclaration
343
356
/:id Identifier[@name == '${ componentName } ']
@@ -356,9 +369,9 @@ function getComponentExportType(astq: ASTQ, ast: any, componentName: string): do
356
369
return undefined ;
357
370
}
358
371
359
- function isClassComponent ( astq : ASTQ , ast : any , componentName : string ,
372
+ function isClassComponent ( ast : AstQuery , componentName : string ,
360
373
reactComponentName : string | undefined ) : boolean {
361
- const res = astq . query ( ast , `
374
+ const res = ast . query ( `
362
375
// ClassDeclaration
363
376
/:id Identifier[@name == '${ componentName } ']
364
377
,
0 commit comments