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
16 changes: 16 additions & 0 deletions src/__tests__/data/FunctionDeclarationDefaultProps.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as React from 'react';

interface FunctionDeclarationDefaultPropsProps {
id?: number;
}

/** FunctionDeclarationDefaultProps description */
export const FunctionDeclarationDefaultProps = (
props: FunctionDeclarationDefaultPropsProps
) => {
return <div>Hello World</div>;
};

FunctionDeclarationDefaultProps.defaultProps = {
id: 1
};
13 changes: 13 additions & 0 deletions src/__tests__/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,19 @@ describe('parser', () => {
});
});

it('should parse react stateless component default props when declared as a normal function', () => {
check('FunctionDeclarationDefaultProps', {
FunctionDeclarationDefaultProps: {
id: {
defaultValue: 1,
description: '',
required: false,
type: 'number'
}
}
});
});

it('should parse react stateless component with external intersection props', () => {
check('StatelessIntersectionExternalProps', {
StatelessIntersectionExternalProps: {
Expand Down
87 changes: 46 additions & 41 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 @@ -631,23 +633,20 @@ export class Parser {
symbol: ts.Symbol,
source: ts.SourceFile
) {
let possibleStatements = source.statements
// ensure that name property is available
.filter(stmt => !!(stmt as ts.ClassDeclaration).name)
.filter(
stmt =>
this.checker.getSymbolAtLocation(
(stmt as ts.ClassDeclaration).name!
) === symbol
);

if (!possibleStatements.length) {
// if no class declaration is found, try to find a
// expression statement used in a React.StatelessComponent
possibleStatements = source.statements.filter(
let possibleStatements = [
...source.statements
// ensure that name property is available
.filter(stmt => !!(stmt as ts.ClassDeclaration).name)
.filter(
stmt =>
this.checker.getSymbolAtLocation(
(stmt as ts.ClassDeclaration).name!
) === symbol
),
...source.statements.filter(
stmt => ts.isExpressionStatement(stmt) || ts.isVariableStatement(stmt)
);
}
)
];

return possibleStatements.reduce((res, statement) => {
if (statementIsClassDeclaration(statement) && statement.members.length) {
Expand Down Expand Up @@ -683,9 +682,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,16 +698,17 @@ 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
>);
}
}
});
return {
...res,
...propMap
};
} else {
}

const functionStatement = this.getFunctionStatement(statement);
Expand Down Expand Up @@ -788,26 +788,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