@@ -2,7 +2,7 @@ import astToCode from 'babel-generator';
22import chalk from 'chalk' ;
33import * as dom from 'dts-dom' ;
44import { IOptions } from './index' ;
5- import { propTypeQueryExpression , AstQuery } from './typings' ;
5+ import { propTypeQueryExpression , AstQuery , ImportedPropTypes } from './typings' ;
66
77export interface TypeDeclaration {
88 type : any ;
@@ -16,14 +16,14 @@ function getTypeDeclaration(type: any, optional: boolean): TypeDeclaration {
1616 } ;
1717}
1818
19- export function get ( ast : AstQuery , propertyAst : any , propTypesName : string | undefined ,
19+ export function get ( ast : AstQuery , propertyAst : any , importedPropTypes : ImportedPropTypes ,
2020 options : IOptions ) : TypeDeclaration {
2121 try {
22- const simpleType = getSimpleType ( ast , propertyAst , propTypesName ) ;
22+ const simpleType = getSimpleType ( ast , propertyAst , importedPropTypes ) ;
2323 if ( simpleType ) {
2424 return simpleType ;
2525 }
26- const complexType = getComplexType ( ast , propertyAst , propTypesName , options ) ;
26+ const complexType = getComplexType ( ast , propertyAst , importedPropTypes , options ) ;
2727 if ( complexType ) {
2828 return complexType ;
2929 }
@@ -43,8 +43,8 @@ export function get(ast: AstQuery, propertyAst: any, propTypesName: string|undef
4343
4444// tslint:disable:next-line cyclomatic-complexity
4545function getSimpleType ( ast : AstQuery , propertyAst : any ,
46- propTypesName : string | undefined ) : TypeDeclaration | undefined {
47- const [ required , simpleTypeName ] = getSimpleTypeName ( ast , propertyAst , propTypesName ) ;
46+ importedPropTypes : ImportedPropTypes ) : TypeDeclaration | undefined {
47+ const [ required , simpleTypeName ] = getSimpleTypeName ( ast , propertyAst , importedPropTypes ) ;
4848 switch ( simpleTypeName ) {
4949 case 'any' :
5050 return getTypeDeclaration ( 'any' , ! required ) ;
@@ -75,18 +75,18 @@ function getSimpleType(ast: AstQuery, propertyAst: any,
7575}
7676
7777function getComplexType ( ast : AstQuery , propertyAst : any ,
78- propTypesName : string | undefined , options : IOptions ) : TypeDeclaration | undefined {
79- const [ required , complexTypeName , typeAst ] = getComplexTypeName ( ast , propertyAst , propTypesName ) ;
78+ importedPropTypes : ImportedPropTypes , options : IOptions ) : TypeDeclaration | undefined {
79+ const [ required , complexTypeName , typeAst ] = getComplexTypeName ( ast , propertyAst , importedPropTypes ) ;
8080 switch ( complexTypeName ) {
8181 case 'instanceOf' :
8282 return getTypeDeclaration ( dom . create . typeof (
8383 dom . create . namedTypeReference ( typeAst . arguments [ 0 ] . name ) ) , ! required ) ;
8484 case 'oneOfType' :
8585 const typeDecls = typeAst . arguments [ 0 ] . elements
86- . map ( ( subtree : any ) => get ( ast , subtree , propTypesName , options ) ) as TypeDeclaration [ ] ;
86+ . map ( ( subtree : any ) => get ( ast , subtree , importedPropTypes , options ) ) as TypeDeclaration [ ] ;
8787 return getTypeDeclaration ( dom . create . union ( typeDecls . map ( type => type . type ) ) , ! required ) ;
8888 case 'arrayOf' :
89- const typeDecl = get ( ast , typeAst . arguments [ 0 ] , propTypesName , options ) ;
89+ const typeDecl = get ( ast , typeAst . arguments [ 0 ] , importedPropTypes , options ) ;
9090 return getTypeDeclaration ( dom . create . array ( typeDecl . type ) , ! required ) ;
9191 case 'oneOf' :
9292 // tslint:disable:next-line comment-format
@@ -95,7 +95,7 @@ function getComplexType(ast: AstQuery, propertyAst: any,
9595 return getTypeDeclaration ( dom . create . union ( enumEntries as dom . Type [ ] ) , ! required ) ;
9696 case 'shape' :
9797 const entries = getShapeProperties ( ast , typeAst . arguments [ 0 ] ) . map ( ( entry : any ) => {
98- const typeDecl = get ( ast , entry . value , propTypesName , options ) ;
98+ const typeDecl = get ( ast , entry . value , importedPropTypes , options ) ;
9999 return dom . create . property ( entry . key . name , typeDecl . type ,
100100 typeDecl . optional ? dom . DeclarationFlags . Optional : dom . DeclarationFlags . None ) ;
101101 } ) ;
@@ -115,23 +115,32 @@ function isRequired(ast: AstQuery, propertyAst: any): [boolean, any] {
115115}
116116
117117function getSimpleTypeName ( ast : AstQuery , propertyAst : any ,
118- propTypesName : string | undefined ) : [ boolean , string | undefined ] {
118+ importedPropTypes : ImportedPropTypes ) : [ boolean , string | undefined ] {
119+ const { propTypesName, propTypes} = importedPropTypes ;
119120 const [ required , typeAst ] = isRequired ( ast , propertyAst ) ;
121+
122+ if ( ! propTypesName && typeAst . type === 'Identifier' ) {
123+ const propType = propTypes . find ( ( { localName} ) => localName === typeAst . name ) ;
124+ return [ required , propType ? propType . importedName : undefined ] ;
125+ }
126+
120127 const res = ast . querySubtree ( typeAst , `
121128 MemberExpression[
122129 (${ propTypeQueryExpression ( propTypesName ) } )
123130 &&
124131 /:property Identifier
125132 ]
133+ /:property Identifier
126134 ` ) ;
127- return [ required , res . length > 0 ? res [ 0 ] . property . name : undefined ] ;
135+
136+ return [ required , res . length > 0 ? res [ 0 ] . name : undefined ] ;
128137}
129138
130139function getComplexTypeName ( ast : AstQuery , propertyAst : any ,
131- propTypesName : string | undefined ) : [ boolean , string | undefined , any ] {
140+ importedPropTypes : ImportedPropTypes ) : [ boolean , string | undefined , any ] {
132141 const [ required , typeAst ] = isRequired ( ast , propertyAst ) ;
133142 if ( typeAst . type === 'CallExpression' ) {
134- const [ , simpleTypeName ] = getSimpleTypeName ( ast , typeAst . callee , propTypesName ) ;
143+ const [ , simpleTypeName ] = getSimpleTypeName ( ast , typeAst . callee , importedPropTypes ) ;
135144 return [ required , simpleTypeName , typeAst ] ;
136145 }
137146 return [ required , undefined , typeAst ] ;
0 commit comments