From b5d1e3ee27c70ebde7e6aa2e831cb674b69db693 Mon Sep 17 00:00:00 2001 From: Diego Landa Date: Tue, 25 Apr 2017 01:10:40 -0400 Subject: [PATCH] PureComponent Support --- src/__tests__/data/PureRow.tsx | 29 ++++++++++++++++++++++++++++ src/__tests__/parser.spec.ts | 35 ++++++++++++++++++++++++++++++++++ src/parser.ts | 15 +++++++++++++-- 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 src/__tests__/data/PureRow.tsx diff --git a/src/__tests__/data/PureRow.tsx b/src/__tests__/data/PureRow.tsx new file mode 100644 index 00000000..eaa075e8 --- /dev/null +++ b/src/__tests__/data/PureRow.tsx @@ -0,0 +1,29 @@ +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"; +} + +/** + * Form row. + */ +export class Row extends React.PureComponent { + + render() { + return
Test
; + } +}; + +export default Row; \ No newline at end of file diff --git a/src/__tests__/parser.spec.ts b/src/__tests__/parser.spec.ts index 953f69e5..af4ad216 100644 --- a/src/__tests__/parser.spec.ts +++ b/src/__tests__/parser.spec.ts @@ -85,4 +85,39 @@ describe('parser', () => { assert.equal(true, i.members[3].isRequired); }); + 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); + assert.equal(1, result.interfaces.length); + + const c = result.classes[0]; + assert.equal('Row', c.name); + assert.equal('Form row.', c.comment); + assert.equal('PureComponent', 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 927d0a20..0700456a 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -79,12 +79,23 @@ export function getDocumentation(fileName: string, options: ts.CompilerOptions = .map((i: ts.Identifier) => i.text); const componentIndex = list.indexOf('Component'); - const propsIndex = componentIndex === -1 ? -1 : (componentIndex + 1); + let propsIndex = -1; + let extendsClass; + + if (componentIndex > -1) { + propsIndex = componentIndex + 1; + extendsClass = 'Component'; + } else { + const pureIndex = list.indexOf('PureComponent'); + + propsIndex = pureIndex === -1 ? -1 : (pureIndex + 1); + extendsClass = pureIndex === -1 ? null : 'PureComponent'; + } classes.push({ name: symbol.name, comment: ts.displayPartsToString(symbol.getDocumentationComment()), - extends: list.length > 0 && componentIndex > -1 ? 'Component' : null, + extends: list.length > 0 ? extendsClass : null, propInterface: list.length > propsIndex ? list[propsIndex] : null, }); }