diff --git a/src/__tests__/data/ConstExport.tsx b/src/__tests__/data/ConstExport.tsx new file mode 100644 index 00000000..a99c98fa --- /dev/null +++ b/src/__tests__/data/ConstExport.tsx @@ -0,0 +1,48 @@ +import * as React from 'react'; + +/** + * Row properties. + */ +export interface IRowProps { + /** prop1 description */ + prop1?: string; + /** prop2 description */ + prop2: number; + /** + * prop3 description + */ + prop3: () => void; + /** prop4 description */ + prop4: 'option1' | 'option2' | "option3"; +} + +/** + * test + * + */ +export const test = (one: number) => { + return one; +} + +export const myObj = { + foo: 'bar', +} + +/** + * Form row. + */ +export const Row = (props: IRowProps) => { + const innerFunc = (props: IRowProps) => { + return Inner Func + }; + const innerNonExportedFunc = (props: IRowProps) => { + return Inner Func + }; + return
Test
; +}; + +const nonExportedFunc = (props: IRowProps) => { + return
No Export
+}; + +export default Row; diff --git a/src/__tests__/parser.spec.ts b/src/__tests__/parser.spec.ts index af4ad216..e038a197 100644 --- a/src/__tests__/parser.spec.ts +++ b/src/__tests__/parser.spec.ts @@ -120,4 +120,38 @@ describe('parser', () => { assert.equal(true, i.members[3].isRequired); }); -}); \ No newline at end of file + it('Should avoid parsing exported objects as components', () => { + const fileName = path.join(__dirname, '../../src/__tests__/data/ConstExport.tsx'); // it's running in ./temp + const result = getDocumentation(fileName); + assert.ok(result.classes); + assert.ok(result.interfaces); + assert.equal(1, result.classes.length); + assert.equal(1, result.interfaces.length); + + const c = result.classes[0]; + assert.equal('Row', c.name); + assert.equal('Form row.', c.comment); + assert.equal('StatelessComponent', c.extends); + + const i = result.interfaces[0]; + assert.equal('IRowProps', i.name); + assert.equal('Row properties.', i.comment); + assert.equal(4, i.members.length); + assert.equal('prop1', i.members[0].name); + assert.equal('prop1 description', i.members[0].comment); + assert.equal(false, i.members[0].isRequired); + + assert.equal('prop2', i.members[1].name); + assert.equal('prop2 description', i.members[1].comment); + assert.equal(true, i.members[1].isRequired); + + assert.equal('prop3', i.members[2].name); + assert.equal('prop3 description', i.members[2].comment); + assert.equal(true, i.members[2].isRequired); + + assert.equal('prop4', i.members[3].name); + assert.equal('prop4 description', i.members[3].comment); + assert.equal(true, i.members[3].isRequired); + }); + +}); diff --git a/src/parser.ts b/src/parser.ts index 0700456a..0cbfbfd1 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -56,6 +56,11 @@ export function getDocumentation(fileName: string, options: ts.CompilerOptions = if (node.kind === ts.SyntaxKind.VariableStatement) { const classNode: any = (node as ts.VariableStatement).declarationList.declarations[0]; + + if (!classNode.initializer.parameters || !classNode.initializer.parameters[0].type.typeName) { + return; + } + const symbol = classNode.symbol; const intf = classNode.initializer.parameters[0].type.typeName.getText(); const classObj = {