@@ -83,7 +83,7 @@ export function generateFromSource(name: string, code: string, options: IOptions
83
83
const defaultInstanceOfResolver : InstanceOfResolver = ( name : string ) : string => undefined ;
84
84
85
85
export function generateFromAst ( name : string , ast : any , options : IOptions = { } ) : string {
86
- const { classname, propTypes} : IParsingResult = parseAst ( ast , options . instanceOfResolver ) ;
86
+ const { exportType , classname, propTypes} : IParsingResult = parseAst ( ast , options . instanceOfResolver ) ;
87
87
const writer : Writer = options . writer || new Writer ( ) ;
88
88
writer . declareModule ( name , ( ) => {
89
89
writer . import ( '* as React' , 'react' ) ;
@@ -98,46 +98,73 @@ export function generateFromAst(name: string, ast: any, options: IOptions = {}):
98
98
writer . nl ( ) ;
99
99
writer . props ( classname , propTypes ) ;
100
100
writer . nl ( ) ;
101
- writer . exportDefault ( ( ) => {
101
+ writer . exportDeclaration ( exportType , ( ) => {
102
102
writer . class ( classname , ! ! propTypes ) ;
103
103
} ) ;
104
104
} ) ;
105
105
return writer . toString ( ) ;
106
106
}
107
107
108
+ enum ExportType {
109
+ default ,
110
+ named
111
+ }
112
+
108
113
interface IParsingResult {
114
+ exportType : ExportType ;
109
115
classname : string ;
110
116
propTypes : IPropTypes ;
111
117
}
112
118
113
119
function parseAst ( ast : any , instanceOfResolver : InstanceOfResolver ) : IParsingResult {
120
+ let exportType : ExportType ;
114
121
let classname : string ;
115
- let propTypes : IPropTypes = undefined ;
122
+ let propTypes : IPropTypes = { } ;
116
123
walk ( ast . program , {
124
+ 'ExportNamedDeclaration' : ( exportNode : any ) : void => {
125
+ exportType = ExportType . named ;
126
+ } ,
127
+ 'ExportDefaultDeclaration' : ( exportNode : any ) : void => {
128
+ exportType = ExportType . default ;
129
+ } ,
117
130
'ClassDeclaration' : ( classNode : any ) : void => {
118
131
classname = classNode . id . name ;
119
132
walk ( classNode . body , {
120
133
'ClassProperty' : ( attributeNode : any ) : void => {
121
134
if ( attributeNode . key . name == 'propTypes' ) {
122
- propTypes = { } ;
123
- walk ( attributeNode . value , {
124
- 'ObjectProperty' : ( propertyNode : any ) : void => {
125
- const prop : IProp = getTypeFromPropType ( propertyNode . value , instanceOfResolver ) ;
126
- prop . documentation = getOptionalDocumentation ( propertyNode ) ;
127
- propTypes [ propertyNode . key . name ] = prop ;
128
- }
129
- } ) ;
135
+ propTypes = parsePropTypes ( attributeNode . value , instanceOfResolver ) ;
130
136
}
131
137
}
132
138
} ) ;
139
+ } ,
140
+ 'ExpressionStatement' : ( expressionNode : any ) : void => {
141
+ if ( expressionNode . expression . type == 'AssignmentExpression'
142
+ && expressionNode . expression . left . type == 'MemberExpression'
143
+ && expressionNode . expression . left . object . name == classname
144
+ && expressionNode . expression . left . property . name == 'propTypes' ) {
145
+ propTypes = parsePropTypes ( expressionNode . expression . right , instanceOfResolver ) ;
146
+ }
133
147
}
134
148
} ) ;
135
149
return {
150
+ exportType,
136
151
classname,
137
152
propTypes
138
153
} ;
139
154
}
140
155
156
+ function parsePropTypes ( node : any , instanceOfResolver : InstanceOfResolver ) : IPropTypes {
157
+ let propTypes : IPropTypes = { } ;
158
+ walk ( node , {
159
+ 'ObjectProperty' : ( propertyNode : any ) : void => {
160
+ const prop : IProp = getTypeFromPropType ( propertyNode . value , instanceOfResolver ) ;
161
+ prop . documentation = getOptionalDocumentation ( propertyNode ) ;
162
+ propTypes [ propertyNode . key . name ] = prop ;
163
+ }
164
+ } ) ;
165
+ return propTypes ;
166
+ }
167
+
141
168
function getOptionalDocumentation ( propertyNode : any ) : string {
142
169
return ( ( ( propertyNode . leadingComments || [ ] ) as any [ ] )
143
170
. filter ( ( comment : any ) => comment . type == 'CommentBlock' ) [ 0 ] || { } )
@@ -351,9 +378,12 @@ export class Writer {
351
378
this . nl ( ) ;
352
379
}
353
380
354
- public exportDefault ( fn : ( ) => void ) : void {
381
+ public exportDeclaration ( exportType : ExportType , fn : ( ) => void ) : void {
355
382
this . indent ( ) ;
356
- this . code += 'export default ' ;
383
+ this . code += 'export ' ;
384
+ if ( exportType == ExportType . default ) {
385
+ this . code += 'default ' ;
386
+ }
357
387
fn ( ) ;
358
388
}
359
389
0 commit comments