diff --git a/src/__tests__/data/OnlyDefaultExportUnion.tsx b/src/__tests__/data/OnlyDefaultExportUnion.tsx new file mode 100644 index 00000000..7cb2b394 --- /dev/null +++ b/src/__tests__/data/OnlyDefaultExportUnion.tsx @@ -0,0 +1,15 @@ +import * as React from 'react'; + +export interface OnlyDefaultExportUnionProps { + /** The content */ + content: string; +} + +/** OnlyDefaultExportUnion description */ +const OnlyDefaultExportUnion: React.FC & { + handledProps: Array; +} = props =>
; + +OnlyDefaultExportUnion.handledProps = ['content']; + +export default OnlyDefaultExportUnion; diff --git a/src/__tests__/parser.ts b/src/__tests__/parser.ts index cd93c394..018bb464 100644 --- a/src/__tests__/parser.ts +++ b/src/__tests__/parser.ts @@ -476,6 +476,14 @@ describe('parser', () => { }); }); + it('should parse components with unioned types', () => { + check('OnlyDefaultExportUnion', { + OnlyDefaultExportUnion: { + content: { description: 'The content', type: 'string' } + } + }); + }); + it('should parse jsdocs with the @default tag and no description', () => { check('StatelessWithDefaultOnlyJsDoc', { StatelessWithDefaultOnlyJsDoc: { diff --git a/src/parser.ts b/src/parser.ts index 0e39d1a9..510ba059 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -137,7 +137,9 @@ export function withCustomConfig( if (error !== undefined) { // tslint:disable-next-line: max-line-length - const errorText = `Cannot load custom tsconfig.json from provided path: ${tsconfigPath}, with error code: ${error.code}, message: ${error.messageText}`; + const errorText = `Cannot load custom tsconfig.json from provided path: ${tsconfigPath}, with error code: ${ + error.code + }, message: ${error.messageText}`; throw new Error(errorText); } @@ -227,21 +229,25 @@ export class Parser { const typeSymbol = type.symbol || type.aliasSymbol; if (!exp.valueDeclaration) { - if (!typeSymbol) { - return null; - } - exp = typeSymbol; - const expName = exp.getName(); - if ( - expName === 'StatelessComponent' || - expName === 'Stateless' || - expName === 'StyledComponentClass' || - expName === 'StyledComponent' || - expName === 'FunctionComponent' - ) { + if (exp.getName() === 'default' && !typeSymbol) { commentSource = this.checker.getAliasedSymbol(commentSource); + } else if (!typeSymbol) { + return null; } else { - commentSource = exp; + exp = typeSymbol; + const expName = exp.getName(); + + if ( + expName === 'StatelessComponent' || + expName === 'Stateless' || + expName === 'StyledComponentClass' || + expName === 'StyledComponent' || + expName === 'FunctionComponent' + ) { + commentSource = this.checker.getAliasedSymbol(commentSource); + } else { + commentSource = exp; + } } } @@ -683,9 +689,9 @@ export class Parser { let propMap = {}; if (properties) { - propMap = this.getPropMap( - properties as ts.NodeArray - ); + propMap = this.getPropMap(properties as ts.NodeArray< + ts.PropertyAssignment + >); } return { @@ -699,9 +705,9 @@ export class Parser { if (right) { const { properties } = right as ts.ObjectLiteralExpression; if (properties) { - propMap = this.getPropMap( - properties as ts.NodeArray - ); + propMap = this.getPropMap(properties as ts.NodeArray< + ts.PropertyAssignment + >); } } }); @@ -788,26 +794,31 @@ export class Parser { public getPropMap( properties: ts.NodeArray ): StringIndexedObject { - const propMap = properties.reduce((acc, property) => { - if (ts.isSpreadAssignment(property) || !property.name) { - return acc; - } - - const literalValue = this.getLiteralValueFromPropertyAssignment(property); - const propertyName = getPropertyName(property.name); + const propMap = properties.reduce( + (acc, property) => { + if (ts.isSpreadAssignment(property) || !property.name) { + return acc; + } - if ( - (typeof literalValue === 'string' || - typeof literalValue === 'number' || - typeof literalValue === 'boolean' || - literalValue === null) && - propertyName !== null - ) { - acc[propertyName] = literalValue; - } + const literalValue = this.getLiteralValueFromPropertyAssignment( + property + ); + const propertyName = getPropertyName(property.name); + + if ( + (typeof literalValue === 'string' || + typeof literalValue === 'number' || + typeof literalValue === 'boolean' || + literalValue === null) && + propertyName !== null + ) { + acc[propertyName] = literalValue; + } - return acc; - }, {} as StringIndexedObject); + return acc; + }, + {} as StringIndexedObject + ); return propMap; }