Skip to content

Commit

Permalink
Merge pull request #33 from pvasek/fix/parse_always_returns_component
Browse files Browse the repository at this point in the history
If there are no component found the default is returned from parse
  • Loading branch information
pvasek authored Jun 19, 2017
2 parents b677c88 + 9acb9a2 commit 737de76
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-docgen-typescript",
"version": "0.0.13",
"version": "0.0.14",
"description": "",
"main": "lib/index.js",
"scripts": {
Expand Down
14 changes: 10 additions & 4 deletions src/__tests__/docgenConverter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { StyleguidistComponent } from '../propTypesParser';

describe('docgenConverter', () => {
it('Should work with class Component', () => {
const result = convertToDocgen({
const result = convertToDocgen('', {
components: [
{
name: 'name1',
Expand Down Expand Up @@ -34,7 +34,7 @@ describe('docgenConverter', () => {
});

it('Should work with functional StatelessComponent', () => {
const result = convertToDocgen({
const result = convertToDocgen('', {
components: [
{
name: 'name1',
Expand Down Expand Up @@ -66,7 +66,7 @@ describe('docgenConverter', () => {
let warnCallCount = 0;
console.warn = () => warnCallCount++;
try {
result = convertToDocgen({
result = convertToDocgen('', {
components: [
{
name: 'name1',
Expand All @@ -91,7 +91,7 @@ describe('docgenConverter', () => {
let warnCallCount = 0;
console.warn = () => warnCallCount++;
try {
result = convertToDocgen({
result = convertToDocgen('', {
components: [
{
name: 'name1',
Expand All @@ -109,4 +109,10 @@ describe('docgenConverter', () => {
assert.equal('name1', result.displayName);
assert.equal('comment1', result.description);
});

it('Should return empty object if there are no components', () => {
const result = convertToDocgen('/root/MyComponent.tsx', { components: [] });
assert.isOk(result);
assert.equal(result.displayName, 'MyComponent');
})
});
9 changes: 7 additions & 2 deletions src/docgenConverter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import * as path from 'path';
import { FileDoc, InterfaceDoc, MemberDoc } from './model';
import { StyleguidistComponent, StyleguidistProps, PropItem } from './propTypesParser';

export function convertToDocgen(doc: FileDoc): StyleguidistComponent | null {
export function convertToDocgen(filePath: string, doc: FileDoc): StyleguidistComponent {
const components = doc.components;

if (components.length === 0) {
return null;
return {
displayName: path.basename(filePath, path.extname(filePath)),
description: '',
props: {},
};
}
const comp = components[0];

Expand Down
2 changes: 1 addition & 1 deletion src/propTypesParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ export interface PropItemType {
*/
export function parse(filePath: string): StyleguidistComponent {
const doc = getFileDocumentation(filePath);
return convertToDocgen(doc);
return convertToDocgen(filePath, doc);
}

0 comments on commit 737de76

Please sign in to comment.