11import astToCode from 'babel-generator' ;
2+ import * as chalk from 'chalk' ;
23import * as dom from 'dts-dom' ;
4+ import { IOptions } from './index' ;
35import { propTypeQueryExpression , AstQuery } from './typings' ;
46
57export interface TypeDeclaration {
@@ -14,22 +16,22 @@ function getTypeDeclaration(type: any, optional: boolean): TypeDeclaration {
1416 } ;
1517}
1618
17- export function get ( ast : AstQuery , propertyAst : any , propTypesName : string | undefined ) : TypeDeclaration {
19+ export function get ( ast : AstQuery , propertyAst : any , propTypesName : string | undefined ,
20+ options : IOptions ) : TypeDeclaration {
1821 try {
1922 const simpleType = getSimpleType ( ast , propertyAst , propTypesName ) ;
2023 if ( simpleType ) {
2124 return simpleType ;
2225 }
23- const complexType = getComplexType ( ast , propertyAst , propTypesName ) ;
26+ const complexType = getComplexType ( ast , propertyAst , propTypesName , options ) ;
2427 if ( complexType ) {
2528 return complexType ;
2629 }
2730 } catch ( e ) {
28- console . error ( 'Failed to infer PropType; Fallback to any' ) ;
2931 if ( e . loc ) {
30- const src = astToCode ( ast . ast ) . code ;
31- console . error ( `Line ${ e . loc . start . line } : ${ src . split ( '\n' ) [ e . loc . start . line - 1 ] } ` ) ;
32+ printErrorWithContext ( e , ast . ast , options ) ;
3233 } else {
34+ console . error ( 'Failed to infer PropType; Fallback to any' ) ;
3335 console . error ( e . stack ) ;
3436 }
3537 }
@@ -73,18 +75,18 @@ function getSimpleType(ast: AstQuery, propertyAst: any,
7375}
7476
7577function getComplexType ( ast : AstQuery , propertyAst : any ,
76- propTypesName : string | undefined ) : TypeDeclaration | undefined {
78+ propTypesName : string | undefined , options : IOptions ) : TypeDeclaration | undefined {
7779 const [ required , complexTypeName , typeAst ] = getComplexTypeName ( ast , propertyAst , propTypesName ) ;
7880 switch ( complexTypeName ) {
7981 case 'instanceOf' :
8082 return getTypeDeclaration ( dom . create . typeof (
8183 dom . create . namedTypeReference ( typeAst . arguments [ 0 ] . name ) ) , ! required ) ;
8284 case 'oneOfType' :
8385 const typeDecls = typeAst . arguments [ 0 ] . elements
84- . map ( ( subtree : any ) => get ( ast , subtree , propTypesName ) ) as TypeDeclaration [ ] ;
86+ . map ( ( subtree : any ) => get ( ast , subtree , propTypesName , options ) ) as TypeDeclaration [ ] ;
8587 return getTypeDeclaration ( dom . create . union ( typeDecls . map ( type => type . type ) ) , ! required ) ;
8688 case 'arrayOf' :
87- const typeDecl = get ( ast , typeAst . arguments [ 0 ] , propTypesName ) ;
89+ const typeDecl = get ( ast , typeAst . arguments [ 0 ] , propTypesName , options ) ;
8890 return getTypeDeclaration ( dom . create . array ( typeDecl . type ) , ! required ) ;
8991 case 'oneOf' :
9092 // tslint:disable:next-line comment-format
@@ -93,7 +95,7 @@ function getComplexType(ast: AstQuery, propertyAst: any,
9395 return getTypeDeclaration ( dom . create . union ( enumEntries as dom . Type [ ] ) , ! required ) ;
9496 case 'shape' :
9597 const entries = getShapeProperties ( ast , typeAst . arguments [ 0 ] ) . map ( ( entry : any ) => {
96- const typeDecl = get ( ast , entry . value , propTypesName ) ;
98+ const typeDecl = get ( ast , entry . value , propTypesName , options ) ;
9799 return dom . create . property ( entry . key . name , typeDecl . type ,
98100 typeDecl . optional ? dom . DeclarationFlags . Optional : dom . DeclarationFlags . None ) ;
99101 } ) ;
@@ -144,16 +146,12 @@ function getEnumValues(ast: AstQuery, oneOfTypes: any): any[] {
144146 /:init *
145147 ` ) ;
146148 if ( ! res [ 0 ] ) {
147- const error = new Error ( 'Failed to lookup enum values' ) ;
148- ( error as any ) . loc = oneOfTypes . loc ;
149- throw error ;
149+ throwWithLocation ( 'Failed to lookup enum values' , oneOfTypes ) ;
150150 }
151151 oneOfTypes = res [ 0 ] ;
152152 }
153153 if ( ! oneOfTypes . elements ) {
154- const error = new Error ( 'Failed to lookup enum values' ) ;
155- ( error as any ) . loc = oneOfTypes . loc ;
156- throw error ;
154+ throwWithLocation ( 'Failed to lookup enum values' , oneOfTypes ) ;
157155 }
158156 return ( oneOfTypes . elements as any [ ] ) . map ( ( element : any ) => {
159157 // fixme: This are not named references!
@@ -178,14 +176,33 @@ function getShapeProperties(ast: AstQuery, input: any): any[] {
178176 if ( res [ 0 ] ) {
179177 return res [ 0 ] . properties ;
180178 }
181- const error = new Error ( 'Failed to lookup shape properties' ) ;
182- ( error as any ) . loc = input . loc ;
183- throw error ;
179+ throwWithLocation ( 'Failed to lookup shape properties' , input ) ;
184180 }
185181 if ( ! input . properties ) {
186- const error = new Error ( 'Failed to lookup shape properties' ) ;
187- ( error as any ) . loc = input . loc ;
188- throw error ;
182+ throwWithLocation ( 'Failed to lookup shape properties' , input ) ;
189183 }
190184 return input . properties ;
191185}
186+
187+ function throwWithLocation ( message : string , ast : any ) : never {
188+ const error = new Error ( message ) ;
189+ ( error as any ) . loc = ast . loc ;
190+ ( error as any ) . start = ast . start ;
191+ ( error as any ) . end = ast . end ;
192+ throw error ;
193+ }
194+
195+ function printErrorWithContext ( e : any , ast : any , options : IOptions ) : void {
196+ console . error ( `${ ( options . filename || '' ) } ${ e . message } ` ) ;
197+ const src = options . source || astToCode ( ast . ast ) . code ;
198+ // console.log(src.substring(e.start, e.end));
199+ const lines = src . split ( '\n' ) ;
200+ const errorLine = lines [ e . loc . start . line - 1 ] ;
201+
202+ console . error ( `Line ${ e . loc . start . line - 1 } : ${ lines [ e . loc . start . line - 2 ] } ` ) ;
203+ console . error ( `Line ${ e . loc . start . line } : ` + errorLine . substring ( 0 , e . loc . start . column )
204+ + chalk . red ( errorLine . substring ( e . loc . start . column , e . loc . end . column ) )
205+ + errorLine . substring ( e . loc . end . column ) ) ;
206+ console . error ( `Line ${ e . loc . start . line + 1 } : ${ lines [ e . loc . start . line ] } ` ) ;
207+ console . error ( ) ;
208+ }
0 commit comments