@@ -165,7 +165,11 @@ export function parseFromProgram(
165
165
type . aliasTypeArguments &&
166
166
checker . getFullyQualifiedName ( type . aliasSymbol ) === 'React.ComponentType'
167
167
) {
168
- parsePropsType ( variableNode . name . getText ( ) , type . aliasTypeArguments [ 0 ] ) ;
168
+ parsePropsType (
169
+ variableNode . name . getText ( ) ,
170
+ type . aliasTypeArguments [ 0 ] ,
171
+ node . getSourceFile ( )
172
+ ) ;
169
173
}
170
174
} else if (
171
175
( ts . isArrowFunction ( variableNode . initializer ) ||
@@ -190,7 +194,8 @@ export function parseFromProgram(
190
194
if ( symbol ) {
191
195
parsePropsType (
192
196
variableNode . name . getText ( ) ,
193
- checker . getTypeOfSymbolAtLocation ( symbol , symbol . valueDeclaration )
197
+ checker . getTypeOfSymbolAtLocation ( symbol , symbol . valueDeclaration ) ,
198
+ node . getSourceFile ( )
194
199
) ;
195
200
}
196
201
}
@@ -210,7 +215,11 @@ export function parseFromProgram(
210
215
if ( ! arg . typeArguments ) return ;
211
216
212
217
if ( reactImports . includes ( arg . expression . getText ( ) ) ) {
213
- parsePropsType ( node . name . getText ( ) , checker . getTypeAtLocation ( arg . typeArguments [ 0 ] ) ) ;
218
+ parsePropsType (
219
+ node . name . getText ( ) ,
220
+ checker . getTypeAtLocation ( arg . typeArguments [ 0 ] ) ,
221
+ node . getSourceFile ( )
222
+ ) ;
214
223
}
215
224
}
216
225
}
@@ -251,21 +260,24 @@ export function parseFromProgram(
251
260
signature . parameters [ 0 ] . valueDeclaration
252
261
) ;
253
262
254
- parsePropsType ( node . name . getText ( ) , type ) ;
263
+ parsePropsType ( node . name . getText ( ) , type , node . getSourceFile ( ) ) ;
255
264
}
256
265
257
- function parsePropsType ( name : string , type : ts . Type ) {
266
+ function parsePropsType ( name : string , type : ts . Type , sourceFile : ts . SourceFile | undefined ) {
258
267
const properties = type
259
268
. getProperties ( )
260
269
. filter ( ( symbol ) => shouldInclude ( { name : symbol . getName ( ) , depth : 1 } ) ) ;
261
270
if ( properties . length === 0 ) {
262
271
return ;
263
272
}
264
273
274
+ const propsFilename = sourceFile !== undefined ? sourceFile . fileName : undefined ;
275
+
265
276
programNode . body . push (
266
277
t . componentNode (
267
278
name ,
268
- properties . map ( ( x ) => checkSymbol ( x , [ ( type as any ) . id ] ) )
279
+ properties . map ( ( x ) => checkSymbol ( x , [ ( type as any ) . id ] ) ) ,
280
+ propsFilename
269
281
)
270
282
) ;
271
283
}
@@ -274,6 +286,8 @@ export function parseFromProgram(
274
286
const declarations = symbol . getDeclarations ( ) ;
275
287
const declaration = declarations && declarations [ 0 ] ;
276
288
289
+ const symbolFilenames = getSymbolFileNames ( symbol ) ;
290
+
277
291
// TypeChecker keeps the name for
278
292
// { a: React.ElementType, b: React.ReactElement | boolean }
279
293
// but not
@@ -298,7 +312,8 @@ export function parseFromProgram(
298
312
return t . propTypeNode (
299
313
symbol . getName ( ) ,
300
314
getDocumentation ( symbol ) ,
301
- declaration . questionToken ? t . unionNode ( [ t . undefinedNode ( ) , elementNode ] ) : elementNode
315
+ declaration . questionToken ? t . unionNode ( [ t . undefinedNode ( ) , elementNode ] ) : elementNode ,
316
+ symbolFilenames
302
317
) ;
303
318
}
304
319
}
@@ -330,7 +345,7 @@ export function parseFromProgram(
330
345
parsedType = checkType ( type , typeStack , symbol . getName ( ) ) ;
331
346
}
332
347
333
- return t . propTypeNode ( symbol . getName ( ) , getDocumentation ( symbol ) , parsedType ) ;
348
+ return t . propTypeNode ( symbol . getName ( ) , getDocumentation ( symbol ) , parsedType , symbolFilenames ) ;
334
349
}
335
350
336
351
function checkType ( type : ts . Type , typeStack : number [ ] , name : string ) : t . Node {
@@ -468,4 +483,10 @@ export function parseFromProgram(
468
483
const comment = ts . displayPartsToString ( symbol . getDocumentationComment ( checker ) ) ;
469
484
return comment ? comment : undefined ;
470
485
}
486
+
487
+ function getSymbolFileNames ( symbol : ts . Symbol ) : Set < string > {
488
+ const declarations = symbol . getDeclarations ( ) || [ ] ;
489
+
490
+ return new Set ( declarations . map ( ( declaration ) => declaration . getSourceFile ( ) . fileName ) ) ;
491
+ }
471
492
}
0 commit comments