Skip to content

Commit

Permalink
Merge pull request #8 from diegolanda/ConstObjectExport
Browse files Browse the repository at this point in the history
Const object export
  • Loading branch information
pvasek authored Apr 27, 2017
2 parents d9171ad + 217d3e5 commit 34834b9
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
48 changes: 48 additions & 0 deletions src/__tests__/data/ConstExport.tsx
Original file line number Diff line number Diff line change
@@ -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 <span>Inner Func</span>
};
const innerNonExportedFunc = (props: IRowProps) => {
return <span>Inner Func</span>
};
return <div>Test</div>;
};

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

export default Row;
36 changes: 35 additions & 1 deletion src/__tests__/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,38 @@ describe('parser', () => {
assert.equal(true, i.members[3].isRequired);
});

});
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);
});

});
5 changes: 5 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 34834b9

Please sign in to comment.