@@ -3,21 +3,34 @@ import * as dom from 'dts-dom';
33import { InstanceOfResolver } from './index' ;
44import * as types from './types' ;
55
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 ,
712 instanceOfResolver : InstanceOfResolver | undefined ) : string {
813 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 ) ;
1326 const componentNames = getUniqueNames ( [
14- ...getComponentNamesByPropTypeAssignment ( astq , ast ) ,
15- ...getComponentNamesByStaticPropTypeAttribute ( astq , ast ) ,
16- ...getComponentNamesByJsxInBody ( astq , ast )
27+ ...getComponentNamesByPropTypeAssignment ( ast ) ,
28+ ...getComponentNamesByStaticPropTypeAttribute ( ast ) ,
29+ ...getComponentNamesByJsxInBody ( ast )
1730 ] ) ;
1831
1932 const m = dom . create . module ( moduleName || 'moduleName' ) ;
20- if ( hasReactClass ( astq , ast , reactComponentName ) ) {
33+ if ( hasReactClass ( ast , reactComponentName ) ) {
2134 m . members . push ( dom . create . importAll ( 'React' , 'react' ) ) ;
2235 }
2336 if ( importStatements . length > 0 ) {
@@ -30,11 +43,11 @@ export function createTypings(moduleName: string|null, ast: any,
3043 } ) ;
3144 }
3245 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 ) ;
3649 if ( exportType ) {
37- createExportedTypes ( m , astq , ast , componentName , reactComponentName , propTypes , propTypesName , exportType ) ;
50+ createExportedTypes ( m , ast , componentName , reactComponentName , propTypes , propTypesName , exportType ) ;
3851 }
3952 } ) ;
4053
@@ -47,15 +60,15 @@ export function createTypings(moduleName: string|null, ast: any,
4760 }
4861} ;
4962
50- function createExportedTypes ( m : dom . ModuleDeclaration , astq : ASTQ , ast : any , componentName : string ,
63+ function createExportedTypes ( m : dom . ModuleDeclaration , ast : AstQuery , componentName : string ,
5164 reactComponentName : string | undefined , propTypes : any , propTypesName : string | undefined ,
5265 exportType : dom . DeclarationFlags ) : void {
53- const classComponent = isClassComponent ( astq , ast , componentName , reactComponentName ) ;
66+ const classComponent = isClassComponent ( ast , componentName , reactComponentName ) ;
5467
5568 const interf = dom . create . interface ( `${ componentName } Props` ) ;
5669 interf . flags = dom . DeclarationFlags . Export ;
5770 if ( propTypes ) {
58- createPropTypeTypings ( interf , astq , propTypes , propTypesName ) ;
71+ createPropTypeTypings ( interf , ast , propTypes , propTypesName ) ;
5972 }
6073 if ( propTypes || classComponent ) {
6174 m . members . push ( interf ) ;
@@ -74,13 +87,13 @@ function createExportedTypes(m: dom.ModuleDeclaration, astq: ASTQ, ast: any, com
7487 }
7588}
7689
77- function createPropTypeTypings ( interf : dom . InterfaceDeclaration , astq : ASTQ , propTypes : any ,
90+ function createPropTypeTypings ( interf : dom . InterfaceDeclaration , ast : AstQuery , propTypes : any ,
7891 propTypesName : string | undefined ) : void {
79- const res = astq . query ( propTypes , `
92+ const res = ast . querySubtree ( propTypes , `
8093 / ObjectProperty
8194 ` ) ;
8295 res . forEach ( propertyAst => {
83- const typeDecl = types . get ( astq , propertyAst . value , propTypesName ) ;
96+ const typeDecl = types . get ( ast , propertyAst . value , propTypesName ) ;
8497 const property = dom . create . property ( propertyAst . key . name || propertyAst . key . value , typeDecl . type ,
8598 typeDecl . optional ? dom . DeclarationFlags . Optional : 0 ) ;
8699 if ( propertyAst . leadingComments && propertyAst . leadingComments [ 0 ] . type === 'CommentBlock' ) {
@@ -122,8 +135,8 @@ export function propTypeQueryExpression(propTypesName: string|undefined): string
122135 ` ;
123136}
124137
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 ( `
127140 // ImportDeclaration[
128141 /:source StringLiteral[@value == 'react']
129142 ]
@@ -138,8 +151,8 @@ function getReactComponentName(astq: ASTQ, ast: any): string|undefined {
138151 return undefined ;
139152}
140153
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 ( `
143156 // ImportDeclaration[
144157 /:source StringLiteral[@value == 'react']
145158 ]
@@ -154,8 +167,8 @@ function getPropTypesName(astq: ASTQ, ast: any): string|undefined {
154167 return undefined ;
155168}
156169
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 ( `
159172 // ClassDeclaration[
160173 '${ reactComponentName } ' == 'undefined'
161174 ?
@@ -184,8 +197,8 @@ function hasReactClass(astq: ASTQ, ast: any, reactComponentName: string|undefine
184197 return res . length > 0 ;
185198}
186199
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 ( `
189202 // CallExpression[
190203 /:callee MemberExpression[
191204 (${ propTypeQueryExpression ( propTypesName ) } )
@@ -203,10 +216,10 @@ interface ImportStatement {
203216 local : string ;
204217 path : string ;
205218}
206- function getImportStatements ( astq : ASTQ , ast : any , typeNames : string [ ] ,
219+ function getImportStatements ( ast : AstQuery , typeNames : string [ ] ,
207220 instanceOfResolver : InstanceOfResolver | undefined ) : ImportStatement [ ] {
208221 return typeNames . map ( name => {
209- const res = astq . query ( ast , `
222+ const res = ast . query ( `
210223 // ImportDeclaration[
211224 /:specifiers * /:local Identifier[@name == '${ name } ']
212225 ]
@@ -233,8 +246,8 @@ function getImportStatements(astq: ASTQ, ast: any, typeNames: string[],
233246 . filter ( importStatement => Boolean ( importStatement . path ) ) ;
234247}
235248
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 ( `
238251 //AssignmentExpression
239252 /:left MemberExpression[
240253 /:object Identifier &&
@@ -247,8 +260,8 @@ function getComponentNamesByPropTypeAssignment(astq: ASTQ, ast: any): string[] {
247260 return [ ] ;
248261}
249262
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 ( `
252265 //ClassDeclaration[
253266 /:body * //ClassProperty /:key Identifier[@name == 'propTypes']
254267 ]
@@ -259,8 +272,8 @@ function getComponentNamesByStaticPropTypeAttribute(astq: ASTQ, ast: any): strin
259272 return [ ] ;
260273}
261274
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 ( `
264277 // ClassDeclaration[
265278 /:body * //JSXElement
266279 ],
@@ -278,8 +291,8 @@ function getComponentNamesByJsxInBody(astq: ASTQ, ast: any): string[] {
278291 return [ ] ;
279292}
280293
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 ( `
283296 //AssignmentExpression[
284297 /:left MemberExpression[
285298 /:object Identifier[@name == '${ componentName } '] &&
@@ -293,8 +306,8 @@ function getPropTypesFromAssignment(astq: ASTQ, ast: any, componentName: string)
293306 return undefined ;
294307}
295308
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 ( `
298311 //ClassDeclaration[
299312 /:id Identifier[@name == '${ componentName } ']
300313 ]
@@ -310,8 +323,8 @@ function getPropTypesFromStaticAttribute(astq: ASTQ, ast: any, componentName: st
310323 return undefined ;
311324}
312325
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 ( `
315328 // ExportDefaultDeclaration[
316329 // ClassDeclaration
317330 /:id Identifier[@name == '${ componentName } ']
@@ -337,7 +350,7 @@ function getComponentExportType(astq: ASTQ, ast: any, componentName: string): do
337350 if ( res . length > 0 ) {
338351 return dom . DeclarationFlags . ExportDefault ;
339352 }
340- res = astq . query ( ast , `
353+ res = ast . query ( `
341354 // ExportNamedDeclaration[
342355 // ClassDeclaration
343356 /:id Identifier[@name == '${ componentName } ']
@@ -356,9 +369,9 @@ function getComponentExportType(astq: ASTQ, ast: any, componentName: string): do
356369 return undefined ;
357370}
358371
359- function isClassComponent ( astq : ASTQ , ast : any , componentName : string ,
372+ function isClassComponent ( ast : AstQuery , componentName : string ,
360373 reactComponentName : string | undefined ) : boolean {
361- const res = astq . query ( ast , `
374+ const res = ast . query ( `
362375 // ClassDeclaration
363376 /:id Identifier[@name == '${ componentName } ']
364377 ,
0 commit comments