Skip to content

Commit 8ee52b9

Browse files
author
Aleksei Dmitriev
committed
chore: Change parents to declartions, add assertion test
1 parent 1bb1d07 commit 8ee52b9

File tree

2 files changed

+45
-18
lines changed

2 files changed

+45
-18
lines changed

src/__tests__/parser.ts

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -984,12 +984,41 @@ describe('parser', () => {
984984
});
985985
});
986986

987-
it('should allow filtering by parent interfaces', () => {
987+
it('should collect all `onClick prop` parent declarations', done => {
988+
assert.doesNotThrow(() => {
989+
withDefaultConfig({
990+
propFilter: prop => {
991+
if (prop.name === 'onClick') {
992+
assert.deepEqual(prop.declarations, [
993+
{
994+
fileName:
995+
'react-docgen-typescript/node_modules/@types/react/index.d.ts',
996+
name: 'DOMAttributes'
997+
},
998+
{
999+
fileName:
1000+
'react-docgen-typescript/src/__tests__/data/ButtonWithOnClickComponent.tsx',
1001+
name: 'TypeLiteral'
1002+
}
1003+
]);
1004+
1005+
done();
1006+
}
1007+
1008+
return true;
1009+
}
1010+
}).parse(fixturePath('ButtonWithOnClickComponent'));
1011+
});
1012+
});
1013+
1014+
it('should allow filtering by parent declarations', () => {
9881015
const propFilter: PropFilter = prop => {
989-
if (prop.parents !== undefined && prop.parents.length > 0) {
990-
const hasPropAdditionalDescription = prop.parents.find(parent => {
991-
return !parent.fileName.includes('@types/react');
992-
});
1016+
if (prop.declarations !== undefined && prop.declarations.length > 0) {
1017+
const hasPropAdditionalDescription = prop.declarations.find(
1018+
declaration => {
1019+
return !declaration.fileName.includes('@types/react');
1020+
}
1021+
);
9931022

9941023
return Boolean(hasPropAdditionalDescription);
9951024
}

src/parser.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ const currentDirectoryPath = process.cwd();
99
const currentDirectoryParts = currentDirectoryPath.split(path.sep);
1010
const currentDirectoryName =
1111
currentDirectoryParts[currentDirectoryParts.length - 1];
12+
13+
type InterfaceOrTypeAliasDeclaration =
14+
| ts.TypeAliasDeclaration
15+
| ts.InterfaceDeclaration;
1216
export interface StringIndexedObject<T> {
1317
[key: string]: T;
1418
}
@@ -30,7 +34,7 @@ export interface PropItem {
3034
description: string;
3135
defaultValue: any;
3236
parent?: ParentType;
33-
parents?: ParentType[];
37+
declarations?: ParentType[];
3438
}
3539

3640
export interface Method {
@@ -642,7 +646,7 @@ export class Parser {
642646
}
643647

644648
const parent = getParentType(prop);
645-
const parents = getParentsType(prop);
649+
const parents = getDeclarations(prop);
646650
const declarations = prop.declarations || [];
647651
const baseProp = baseProps.find(p => p.getName() === propName);
648652

@@ -665,7 +669,7 @@ export class Parser {
665669
description: jsDocComment.fullComment,
666670
name: propName,
667671
parent,
668-
parents,
672+
declarations: parents,
669673
required,
670674
type
671675
};
@@ -1142,7 +1146,7 @@ function isTypeLiteral(node: ts.Node): node is ts.TypeLiteralNode {
11421146
return node.kind === ts.SyntaxKind.TypeLiteral;
11431147
}
11441148

1145-
function getParentsType(prop: ts.Symbol): ParentType[] | undefined {
1149+
function getDeclarations(prop: ts.Symbol): ParentType[] | undefined {
11461150
const declarations = prop.getDeclarations();
11471151

11481152
if (declarations === undefined || declarations.length === 0) {
@@ -1158,16 +1162,13 @@ function getParentsType(prop: ts.Symbol): ParentType[] | undefined {
11581162
continue;
11591163
}
11601164

1161-
type InterfaceOrTypeAlias =
1162-
| ts.TypeAliasDeclaration
1163-
| ts.InterfaceDeclaration;
1164-
11651165
const parentName =
11661166
'name' in parent
1167-
? (parent as InterfaceOrTypeAlias).name.text
1167+
? (parent as InterfaceOrTypeAliasDeclaration).name.text
11681168
: 'TypeLiteral';
1169+
11691170
const { fileName } = (parent as
1170-
| InterfaceOrTypeAlias
1171+
| InterfaceOrTypeAliasDeclaration
11711172
| ts.TypeLiteralNode).getSourceFile();
11721173

11731174
parents.push({
@@ -1199,9 +1200,6 @@ function trimFileName(fileName: string) {
11991200
return trimmedFileName;
12001201
}
12011202

1202-
/**
1203-
* @deprecated
1204-
*/
12051203
function getParentType(prop: ts.Symbol): ParentType | undefined {
12061204
const declarations = prop.getDeclarations();
12071205

0 commit comments

Comments
 (0)