Skip to content

Commit

Permalink
Adding support for default exports (#58)
Browse files Browse the repository at this point in the history
* Default export does not work - fixed
  • Loading branch information
pvasek authored Jan 2, 2018
1 parent cf90374 commit fd7f4d3
Show file tree
Hide file tree
Showing 15 changed files with 154 additions and 26 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-docgen-typescript",
"version": "1.1.0",
"version": "1.2.0",
"description": "",
"main": "lib/index.js",
"scripts": {
Expand All @@ -11,7 +11,6 @@
"print": "npm run tsc && node ./lib/print.js",
"print:sample1": "npm run tsc && node ./lib/print.js ./src/__tests__/data/ColumnHigherOrderComponent.tsx simple"
},
"author": "pvasek",
"license": "MIT",
"dependencies": {},
"devDependencies": {
Expand Down
31 changes: 31 additions & 0 deletions src/__tests__/data/ColumnWithDefaultAnonymousExportOnly.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as React from 'react';

/**
* Column properties.
*/
export interface IColumnProps {
/** prop1 description */
prop1?: string;
/** prop2 description */
prop2: number;
/**
* prop3 description
*/
prop3: () => void;
/** prop4 description */
prop4: 'option1' | 'option2' | "option3";
}

/**
* ColumnWithDefaultAnonymousExportOnly description
*/
export default class extends React.Component<IColumnProps, {}> {
public static defaultProps: Partial<IColumnProps> = {
prop1: 'prop1'
};

render() {
const {prop1} = this.props;
return <div>{prop1}</div>;
}
}
33 changes: 33 additions & 0 deletions src/__tests__/data/ColumnWithDefaultExport.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as React from 'react';

/**
* Column properties.
*/
export interface IColumnProps {
/** prop1 description */
prop1?: string;
/** prop2 description */
prop2: number;
/**
* prop3 description
*/
prop3: () => void;
/** prop4 description */
prop4: 'option1' | 'option2' | "option3";
}

/**
* Column description
*/
class Column extends React.Component<IColumnProps, {}> {
public static defaultProps: Partial<IColumnProps> = {
prop1: 'prop1'
};

render() {
const {prop1} = this.props;
return <div>{prop1}</div>;
}
}

export default Column;
31 changes: 31 additions & 0 deletions src/__tests__/data/ColumnWithDefaultExportOnly.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as React from 'react';

/**
* Column properties.
*/
export interface IColumnProps {
/** prop1 description */
prop1?: string;
/** prop2 description */
prop2: number;
/**
* prop3 description
*/
prop3: () => void;
/** prop4 description */
prop4: 'option1' | 'option2' | "option3";
}

/**
* ColumnWithDefaultExportOnly description
*/
export default class Column extends React.Component<IColumnProps, {}> {
public static defaultProps: Partial<IColumnProps> = {
prop1: 'prop1'
};

render() {
const {prop1} = this.props;
return <div>{prop1}</div>;
}
}
4 changes: 1 addition & 3 deletions src/__tests__/data/ColumnWithHtmlAttributes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,4 @@ export class Column extends React.Component<IColumnProps, {}> {
const {prop1} = this.props;
return <div>{prop1}</div>;
}
}

export default Column;
}
4 changes: 1 addition & 3 deletions src/__tests__/data/ColumnWithPick.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,4 @@ export class Column extends React.Component<ColumnProps, {}> {
const {prop1} = this.props;
return <div>{prop1}</div>;
}
}

export default Column;
}
4 changes: 1 addition & 3 deletions src/__tests__/data/ColumnWithoutExportedProps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,4 @@ export class Column extends React.Component<IColumnProps, {}> {
const {prop1} = this.props;
return <div>{prop1}</div>;
}
}

export default Column;
}
4 changes: 1 addition & 3 deletions src/__tests__/data/ConstExport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,4 @@ export const Row = (props: IRowProps) => {

const nonExportedFunc = (props: IRowProps) => {
return <div>No Export</div>
};

export default Row;
};
4 changes: 1 addition & 3 deletions src/__tests__/data/FlippableImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,4 @@ export const FlippableImage = (props: Props) => {
{...rest}
/>
);
};

export default FlippableImage;
};
2 changes: 0 additions & 2 deletions src/__tests__/data/PureRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,3 @@ export class Row extends React.PureComponent<IRowProps, {}> {
return <div>Test</div>;
}
};

export default Row;
2 changes: 0 additions & 2 deletions src/__tests__/data/Regression_v0_0_12.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,3 @@ export class Zoomable extends React.PureComponent<Props, State> {
);
}
}

export default Zoomable;
2 changes: 0 additions & 2 deletions src/__tests__/data/Stateless.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,3 @@ export const Stateless: React.StatelessComponent<StatelessProps> = props =>
<div>
My Property = {props.myProp}
</div>;

export default Stateless;
1 change: 0 additions & 1 deletion src/__tests__/data/StatelessWithDefaultProps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ StatelessWithDefaultProps.defaultProps = {
sampleProp: 'test'
};

export default StatelessWithDefaultProps;
37 changes: 37 additions & 0 deletions src/__tests__/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,43 @@ describe('parser', () => {
});
});


it('should parse simple react class component as default export', function() {
check('ColumnWithDefaultExport', {
Column: {
children,
prop1: { type: 'string', required: false },
prop2: { type: 'number' },
prop3: { type: '() => void'},
prop4: { type: '"option1" | "option2" | "option3"' },
}
});
});

it('should parse simple react class component as default export only', function() {
check('ColumnWithDefaultExportOnly', {
ColumnWithDefaultExportOnly: {
children,
prop1: { type: 'string', required: false },
prop2: { type: 'number' },
prop3: { type: '() => void'},
prop4: { type: '"option1" | "option2" | "option3"' },
}
});
});

it('should parse simple react class component as default anonymous export', function() {
check('ColumnWithDefaultAnonymousExportOnly', {
ColumnWithDefaultAnonymousExportOnly: {
children,
prop1: { type: 'string', required: false },
prop2: { type: 'number' },
prop3: { type: '() => void'},
prop4: { type: '"option1" | "option2" | "option3"' },
}
});
});

it('should parse simple react class component with state', () => {
check('AppMenu', {
AppMenu: {
Expand Down
18 changes: 16 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,17 @@ export function withCompilerOptions(compilerOptions: ts.CompilerOptions): FilePa
.map(exp => parser.getComponentInfo(exp, sourceFile))
.filter(comp => comp);

return components;
// this should filter out components with the same name as default export
const filteredComponents = components
.filter((comp, index) => {
const isUnique = components
.slice(index + 1)
.filter(i => i.displayName === comp.displayName)
.length === 0;
return isUnique;
});

return filteredComponents;
}
};
}
Expand All @@ -114,7 +124,11 @@ class Parser {
}

public getComponentInfo(exp: ts.Symbol, source: ts.SourceFile): ComponentDoc {
const type = this.checker.getTypeOfSymbolAtLocation(exp, exp.valueDeclaration);
const type = this.checker.getTypeOfSymbolAtLocation(exp, exp.valueDeclaration || exp.declarations[0]);
if (!exp.valueDeclaration) {
exp = type.symbol;
}

let propsType = this.extractPropsFromTypeIfStatelessComponent(type);
if (!propsType) {
propsType = this.extractPropsFromTypeIfStatefulComponent(type);
Expand Down

0 comments on commit fd7f4d3

Please sign in to comment.