Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/__tests__/data/OnlyDefaultExportUnion.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as React from 'react';

export interface OnlyDefaultExportUnionProps {
/** The content */
content: string;
}

/** OnlyDefaultExportUnion description */
const OnlyDefaultExportUnion: React.FC<OnlyDefaultExportUnionProps> & {
handledProps: Array<keyof OnlyDefaultExportUnionProps>;
} = props => <div />;

OnlyDefaultExportUnion.handledProps = ['content'];

export default OnlyDefaultExportUnion;
8 changes: 8 additions & 0 deletions src/__tests__/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
87 changes: 49 additions & 38 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}
}
}

Expand Down Expand Up @@ -683,9 +689,9 @@ export class Parser {
let propMap = {};

if (properties) {
propMap = this.getPropMap(
properties as ts.NodeArray<ts.PropertyAssignment>
);
propMap = this.getPropMap(properties as ts.NodeArray<
ts.PropertyAssignment
>);
}

return {
Expand All @@ -699,9 +705,9 @@ export class Parser {
if (right) {
const { properties } = right as ts.ObjectLiteralExpression;
if (properties) {
propMap = this.getPropMap(
properties as ts.NodeArray<ts.PropertyAssignment>
);
propMap = this.getPropMap(properties as ts.NodeArray<
ts.PropertyAssignment
>);
}
}
});
Expand Down Expand Up @@ -788,26 +794,31 @@ export class Parser {
public getPropMap(
properties: ts.NodeArray<ts.PropertyAssignment | ts.BindingElement>
): StringIndexedObject<string | boolean | number | null> {
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<string | boolean | number | null>);
return acc;
},
{} as StringIndexedObject<string | boolean | number | null>
);

return propMap;
}
Expand Down