@@ -3,8 +3,6 @@ import * as path from 'path';
33import * as ts from 'typescript' ;
44
55import { buildFilter } from './buildFilter' ;
6- import { symbol } from 'prop-types' ;
7- import { check } from './__tests__/testUtils' ;
86
97// We'll use the currentDirectoryName to trim parent fileNames
108const currentDirectoryPath = process . cwd ( ) ;
@@ -32,6 +30,7 @@ export interface PropItem {
3230 description : string ;
3331 defaultValue : any ;
3432 parent ?: ParentType ;
33+ parents ?: ParentType [ ] ;
3534}
3635
3736export interface Method {
@@ -643,6 +642,7 @@ export class Parser {
643642 }
644643
645644 const parent = getParentType ( prop ) ;
645+ const parents = getParentsType ( prop ) ;
646646 const declarations = prop . declarations || [ ] ;
647647 const baseProp = baseProps . find ( p => p . getName ( ) === propName ) ;
648648
@@ -665,6 +665,7 @@ export class Parser {
665665 description : jsDocComment . fullComment ,
666666 name : propName ,
667667 parent,
668+ parents,
668669 required,
669670 type
670671 } ;
@@ -1137,23 +1138,48 @@ export function getDefaultExportForFile(source: ts.SourceFile) {
11371138 return identifier . length ? identifier : 'DefaultName' ;
11381139}
11391140
1140- function getParentType ( prop : ts . Symbol ) : ParentType | undefined {
1141+ function isTypeLiteral ( node : ts . Node ) : node is ts . TypeLiteralNode {
1142+ return node . kind === ts . SyntaxKind . TypeLiteral ;
1143+ }
1144+
1145+ function getParentsType ( prop : ts . Symbol ) : ParentType [ ] | undefined {
11411146 const declarations = prop . getDeclarations ( ) ;
11421147
1143- if ( declarations == null || declarations . length === 0 ) {
1148+ if ( declarations === undefined || declarations . length === 0 ) {
11441149 return undefined ;
11451150 }
11461151
1147- // Props can be declared only in one place
1148- const { parent } = declarations [ 0 ] ;
1152+ const parents : ParentType [ ] = [ ] ;
11491153
1150- if ( ! isInterfaceOrTypeAliasDeclaration ( parent ) ) {
1151- return undefined ;
1154+ for ( let declaration of declarations ) {
1155+ const { parent } = declaration ;
1156+
1157+ if ( ! isTypeLiteral ( parent ) && ! isInterfaceOrTypeAliasDeclaration ( parent ) ) {
1158+ continue ;
1159+ }
1160+
1161+ type InterfaceOrTypeAlias =
1162+ | ts . TypeAliasDeclaration
1163+ | ts . InterfaceDeclaration ;
1164+
1165+ const parentName =
1166+ 'name' in parent
1167+ ? ( parent as InterfaceOrTypeAlias ) . name . text
1168+ : 'TypeLiteral' ;
1169+ const { fileName } = ( parent as
1170+ | InterfaceOrTypeAlias
1171+ | ts . TypeLiteralNode ) . getSourceFile ( ) ;
1172+
1173+ parents . push ( {
1174+ fileName : trimFileName ( fileName ) ,
1175+ name : parentName
1176+ } ) ;
11521177 }
11531178
1154- const parentName = parent . name . text ;
1155- const { fileName } = parent . getSourceFile ( ) ;
1179+ return parents ;
1180+ }
11561181
1182+ function trimFileName ( fileName : string ) {
11571183 const fileNameParts = fileName . split ( '/' ) ;
11581184 const trimmedFileNameParts = fileNameParts . slice ( ) ;
11591185
@@ -1170,8 +1196,31 @@ function getParentType(prop: ts.Symbol): ParentType | undefined {
11701196 trimmedFileName = fileName ;
11711197 }
11721198
1199+ return trimmedFileName ;
1200+ }
1201+
1202+ /**
1203+ * @deprecated
1204+ */
1205+ function getParentType ( prop : ts . Symbol ) : ParentType | undefined {
1206+ const declarations = prop . getDeclarations ( ) ;
1207+
1208+ if ( declarations == null || declarations . length === 0 ) {
1209+ return undefined ;
1210+ }
1211+
1212+ // Props can be declared only in one place
1213+ const { parent } = declarations [ 0 ] ;
1214+
1215+ if ( ! isInterfaceOrTypeAliasDeclaration ( parent ) ) {
1216+ return undefined ;
1217+ }
1218+
1219+ const parentName = parent . name . text ;
1220+ const { fileName } = parent . getSourceFile ( ) ;
1221+
11731222 return {
1174- fileName : trimmedFileName ,
1223+ fileName : trimFileName ( fileName ) ,
11751224 name : parentName
11761225 } ;
11771226}
0 commit comments