@@ -3,22 +3,23 @@ import * as babylon from 'babylon';
33import * as dom from 'dts-dom' ;
44
55export interface InstanceOfResolver {
6- ( name : string ) : string ;
6+ ( name : string ) : string | undefined ;
77} ;
88
99export interface IOptions {
10+
1011 /**
1112 * Resolves type names to import paths.
1213 *
1314 * @return Path to given name if resolveable, undefined otherwise
1415 */
1516 instanceOfResolver ?: InstanceOfResolver ;
17+
1618 /**
1719 * The Generator generating .d.ts code with.
1820 *
1921 * This option is deprecated with 0.13 and is not supported anymore.
2022 * any new feature will not work with the deprecated Generator interface.
21-
2223 * @deprecated
2324 */
2425 generator ?: Generator ;
@@ -71,11 +72,11 @@ export function cli(options: any): void {
7172 } ) ;
7273}
7374
74- export function generateFromFile ( moduleName : string , path : string , options ?: IOptions ) : string {
75+ export function generateFromFile ( moduleName : string | null , path : string , options ?: IOptions ) : string {
7576 return generateFromSource ( moduleName , fs . readFileSync ( path ) . toString ( ) , options ) ;
7677}
7778
78- export function generateFromSource ( moduleName : string , code : string , options : IOptions = { } ) : string {
79+ export function generateFromSource ( moduleName : string | null , code : string , options : IOptions = { } ) : string {
7980 const ast = babylon . parse ( code , {
8081 sourceType : 'module' ,
8182 allowReturnOutsideFunction : true ,
@@ -101,9 +102,9 @@ export function generateFromSource(moduleName: string, code: string, options: IO
101102 return generateFromAst ( moduleName , ast , options ) ;
102103}
103104
104- const defaultInstanceOfResolver : InstanceOfResolver = ( name : string ) : string => undefined ;
105+ const defaultInstanceOfResolver : InstanceOfResolver = ( _name : string ) : undefined => undefined ;
105106
106- export function generateFromAst ( moduleName : string , ast : any , options : IOptions = { } ) : string {
107+ export function generateFromAst ( moduleName : string | null , ast : any , options : IOptions = { } ) : string {
107108 const parsingResult = parseAst ( ast , options . instanceOfResolver ) ;
108109 if ( options . generator ) {
109110 return deprecatedGenerator ( options . generator , moduleName , parsingResult ) ;
@@ -117,7 +118,7 @@ export function generateFromAst(moduleName: string, ast: any, options: IOptions
117118 if ( propTypes ) {
118119 Object . keys ( propTypes ) . forEach ( propName => {
119120 const prop = propTypes [ propName ] ;
120- if ( prop . importPath ) {
121+ if ( prop . importType && prop . importPath ) {
121122 code += dom . emit ( dom . create . importDefault ( prop . importType , prop . importPath ) ) ;
122123 }
123124 } ) ;
@@ -135,7 +136,7 @@ export function generateFromAst(moduleName: string, ast: any, options: IOptions
135136 if ( propTypes ) {
136137 Object . keys ( propTypes ) . forEach ( propName => {
137138 const prop = propTypes [ propName ] ;
138- if ( prop . importPath ) {
139+ if ( prop . importType && prop . importPath ) {
139140 m . members . push ( dom . create . importDefault ( prop . importType , prop . importPath ) ) ;
140141 }
141142 } ) ;
@@ -190,14 +191,14 @@ function createReactClassDeclaration(classname: string, exportType: ExportType,
190191 return classDecl ;
191192}
192193
193- function deprecatedGenerator ( generator : Generator , moduleName : string ,
194+ function deprecatedGenerator ( generator : Generator , moduleName : string | null ,
194195 { exportType, classname, propTypes} : IParsingResult ) : string {
195196 const generateTypings = ( ) => {
196197 generator . import ( '* as React' , 'react' ) ;
197198 if ( propTypes ) {
198199 Object . keys ( propTypes ) . forEach ( propName => {
199200 const prop = propTypes [ propName ] ;
200- if ( prop . importPath ) {
201+ if ( prop . importType && prop . importPath ) {
201202 generator . import ( prop . importType , prop . importPath ) ;
202203 }
203204 } ) ;
@@ -229,15 +230,15 @@ interface IParsingResult {
229230 propTypes : IPropTypes ;
230231}
231232
232- function parseAst ( ast : any , instanceOfResolver : InstanceOfResolver ) : IParsingResult {
233- let exportType : ExportType ;
234- let classname : string ;
233+ function parseAst ( ast : any , instanceOfResolver ? : InstanceOfResolver ) : IParsingResult {
234+ let exportType : ExportType | undefined ;
235+ let classname : string | undefined ;
235236 let propTypes : IPropTypes = { } ;
236237 walk ( ast . program , {
237- 'ExportNamedDeclaration' : exportNode => {
238+ 'ExportNamedDeclaration' : ( ) => {
238239 exportType = ExportType . named ;
239240 } ,
240- 'ExportDefaultDeclaration' : exportNode => {
241+ 'ExportDefaultDeclaration' : ( ) => {
241242 exportType = ExportType . default ;
242243 } ,
243244 'ClassDeclaration' : classNode => {
@@ -275,14 +276,20 @@ function parseAst(ast: any, instanceOfResolver: InstanceOfResolver): IParsingRes
275276 }
276277 } ) ;
277278 }
279+ if ( exportType === undefined ) {
280+ throw new Error ( 'No exported class found' ) ;
281+ }
282+ if ( ! classname ) {
283+ throw new Error ( 'Anonymous classes are not supported' ) ;
284+ }
278285 return {
279286 exportType,
280287 classname,
281288 propTypes
282289 } ;
283290}
284291
285- function parsePropTypes ( node : any , instanceOfResolver : InstanceOfResolver ) : IPropTypes {
292+ function parsePropTypes ( node : any , instanceOfResolver ? : InstanceOfResolver ) : IPropTypes {
286293 let propTypes : IPropTypes = { } ;
287294 walk ( node , {
288295 'ObjectProperty' : propertyNode => {
@@ -369,7 +376,7 @@ function isRequiredPropType(node: any, instanceOfResolver: InstanceOfResolver):
369376}
370377
371378/**
372- * This is for internal use only
379+ * @ internal
373380 */
374381export function getTypeFromPropType ( node : IASTNode , instanceOfResolver = defaultInstanceOfResolver ) : IProp {
375382 const result : IProp = {
0 commit comments