@@ -165,7 +165,11 @@ export function parseFromProgram(
165165 type . aliasTypeArguments &&
166166 checker . getFullyQualifiedName ( type . aliasSymbol ) === 'React.ComponentType'
167167 ) {
168- parsePropsType ( variableNode . name . getText ( ) , type . aliasTypeArguments [ 0 ] ) ;
168+ parsePropsType (
169+ variableNode . name . getText ( ) ,
170+ type . aliasTypeArguments [ 0 ] ,
171+ node . getSourceFile ( )
172+ ) ;
169173 }
170174 } else if (
171175 ( ts . isArrowFunction ( variableNode . initializer ) ||
@@ -190,7 +194,8 @@ export function parseFromProgram(
190194 if ( symbol ) {
191195 parsePropsType (
192196 variableNode . name . getText ( ) ,
193- checker . getTypeOfSymbolAtLocation ( symbol , symbol . valueDeclaration )
197+ checker . getTypeOfSymbolAtLocation ( symbol , symbol . valueDeclaration ) ,
198+ node . getSourceFile ( )
194199 ) ;
195200 }
196201 }
@@ -210,7 +215,11 @@ export function parseFromProgram(
210215 if ( ! arg . typeArguments ) return ;
211216
212217 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+ ) ;
214223 }
215224 }
216225 }
@@ -251,21 +260,24 @@ export function parseFromProgram(
251260 signature . parameters [ 0 ] . valueDeclaration
252261 ) ;
253262
254- parsePropsType ( node . name . getText ( ) , type ) ;
263+ parsePropsType ( node . name . getText ( ) , type , node . getSourceFile ( ) ) ;
255264 }
256265
257- function parsePropsType ( name : string , type : ts . Type ) {
266+ function parsePropsType ( name : string , type : ts . Type , sourceFile : ts . SourceFile | undefined ) {
258267 const properties = type
259268 . getProperties ( )
260269 . filter ( ( symbol ) => shouldInclude ( { name : symbol . getName ( ) , depth : 1 } ) ) ;
261270 if ( properties . length === 0 ) {
262271 return ;
263272 }
264273
274+ const propsFilename = sourceFile !== undefined ? sourceFile . fileName : undefined ;
275+
265276 programNode . body . push (
266277 t . componentNode (
267278 name ,
268- properties . map ( ( x ) => checkSymbol ( x , [ ( type as any ) . id ] ) )
279+ properties . map ( ( x ) => checkSymbol ( x , [ ( type as any ) . id ] ) ) ,
280+ propsFilename
269281 )
270282 ) ;
271283 }
@@ -274,6 +286,8 @@ export function parseFromProgram(
274286 const declarations = symbol . getDeclarations ( ) ;
275287 const declaration = declarations && declarations [ 0 ] ;
276288
289+ const symbolFilenames = getSymbolFileNames ( symbol ) ;
290+
277291 // TypeChecker keeps the name for
278292 // { a: React.ElementType, b: React.ReactElement | boolean }
279293 // but not
@@ -298,7 +312,8 @@ export function parseFromProgram(
298312 return t . propTypeNode (
299313 symbol . getName ( ) ,
300314 getDocumentation ( symbol ) ,
301- declaration . questionToken ? t . unionNode ( [ t . undefinedNode ( ) , elementNode ] ) : elementNode
315+ declaration . questionToken ? t . unionNode ( [ t . undefinedNode ( ) , elementNode ] ) : elementNode ,
316+ symbolFilenames
302317 ) ;
303318 }
304319 }
@@ -330,7 +345,7 @@ export function parseFromProgram(
330345 parsedType = checkType ( type , typeStack , symbol . getName ( ) ) ;
331346 }
332347
333- return t . propTypeNode ( symbol . getName ( ) , getDocumentation ( symbol ) , parsedType ) ;
348+ return t . propTypeNode ( symbol . getName ( ) , getDocumentation ( symbol ) , parsedType , symbolFilenames ) ;
334349 }
335350
336351 function checkType ( type : ts . Type , typeStack : number [ ] , name : string ) : t . Node {
@@ -468,4 +483,10 @@ export function parseFromProgram(
468483 const comment = ts . displayPartsToString ( symbol . getDocumentationComment ( checker ) ) ;
469484 return comment ? comment : undefined ;
470485 }
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+ }
471492}
0 commit comments