From 8359235d3c19e14f08b58faaf49e0b1bd67c1329 Mon Sep 17 00:00:00 2001 From: Diego Landa Date: Wed, 26 Apr 2017 22:45:40 -0400 Subject: [PATCH 1/3] Added support to avoid parsing const exported objects --- src/__tests__/data/ConstExport.tsx | 48 ++++++++++++++++++++++++++++++ src/__tests__/parser.spec.ts | 35 +++++++++++++++++++++- src/parser.ts | 5 ++++ 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 src/__tests__/data/ConstExport.tsx diff --git a/src/__tests__/data/ConstExport.tsx b/src/__tests__/data/ConstExport.tsx new file mode 100644 index 00000000..91d69695 --- /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; \ No newline at end of file diff --git a/src/__tests__/parser.spec.ts b/src/__tests__/parser.spec.ts index af4ad216..a5392001 100644 --- a/src/__tests__/parser.spec.ts +++ b/src/__tests__/parser.spec.ts @@ -88,7 +88,6 @@ describe('parser', () => { it('Should parse class-based pure components', () => { const fileName = path.join(__dirname, '../../src/__tests__/data/PureRow.tsx'); // it's running in ./temp const result = getDocumentation(fileName); - assert.ok(result.classes); assert.ok(result.interfaces); assert.equal(1, result.classes.length); @@ -120,4 +119,38 @@ describe('parser', () => { assert.equal(true, i.members[3].isRequired); }); + it('Should should avoid exported objects', () => { + 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); + }); + }); \ No newline at end of file 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 = { From 945de50bac81c09db32ff03906866212d1cc3ace Mon Sep 17 00:00:00 2001 From: Diego Landa Date: Wed, 26 Apr 2017 22:47:35 -0400 Subject: [PATCH 2/3] Code style fixes --- src/__tests__/data/ConstExport.tsx | 2 +- src/__tests__/parser.spec.ts | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/__tests__/data/ConstExport.tsx b/src/__tests__/data/ConstExport.tsx index 91d69695..a99c98fa 100644 --- a/src/__tests__/data/ConstExport.tsx +++ b/src/__tests__/data/ConstExport.tsx @@ -45,4 +45,4 @@ const nonExportedFunc = (props: IRowProps) => { return
No Export
}; -export default Row; \ No newline at end of file +export default Row; diff --git a/src/__tests__/parser.spec.ts b/src/__tests__/parser.spec.ts index a5392001..7aa146b8 100644 --- a/src/__tests__/parser.spec.ts +++ b/src/__tests__/parser.spec.ts @@ -88,6 +88,7 @@ describe('parser', () => { it('Should parse class-based pure components', () => { const fileName = path.join(__dirname, '../../src/__tests__/data/PureRow.tsx'); // it's running in ./temp const result = getDocumentation(fileName); + assert.ok(result.classes); assert.ok(result.interfaces); assert.equal(1, result.classes.length); @@ -153,4 +154,4 @@ describe('parser', () => { assert.equal(true, i.members[3].isRequired); }); -}); \ No newline at end of file +}); From 217d3e59c46ac20873d311c36caf7ca695f7b353 Mon Sep 17 00:00:00 2001 From: Diego Landa Date: Thu, 27 Apr 2017 10:48:15 -0400 Subject: [PATCH 3/3] Fixed test name --- src/__tests__/parser.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/__tests__/parser.spec.ts b/src/__tests__/parser.spec.ts index 7aa146b8..e038a197 100644 --- a/src/__tests__/parser.spec.ts +++ b/src/__tests__/parser.spec.ts @@ -120,7 +120,7 @@ describe('parser', () => { assert.equal(true, i.members[3].isRequired); }); - it('Should should avoid exported objects', () => { + 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);