Skip to content

Commit

Permalink
PureComponent Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Landa committed Apr 25, 2017
1 parent 3f94005 commit b5d1e3e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
29 changes: 29 additions & 0 deletions src/__tests__/data/PureRow.tsx
Original file line number Diff line number Diff line change
@@ -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<IRowProps, {}> {

render() {
return <div>Test</div>;
}
};

export default Row;
35 changes: 35 additions & 0 deletions src/__tests__/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

});
15 changes: 13 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
Expand Down

0 comments on commit b5d1e3e

Please sign in to comment.