Skip to content

Commit 737de76

Browse files
authored
Merge pull request #33 from pvasek/fix/parse_always_returns_component
If there are no component found the default is returned from parse
2 parents b677c88 + 9acb9a2 commit 737de76

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-docgen-typescript",
3-
"version": "0.0.13",
3+
"version": "0.0.14",
44
"description": "",
55
"main": "lib/index.js",
66
"scripts": {

src/__tests__/docgenConverter.spec.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { StyleguidistComponent } from '../propTypesParser';
66

77
describe('docgenConverter', () => {
88
it('Should work with class Component', () => {
9-
const result = convertToDocgen({
9+
const result = convertToDocgen('', {
1010
components: [
1111
{
1212
name: 'name1',
@@ -34,7 +34,7 @@ describe('docgenConverter', () => {
3434
});
3535

3636
it('Should work with functional StatelessComponent', () => {
37-
const result = convertToDocgen({
37+
const result = convertToDocgen('', {
3838
components: [
3939
{
4040
name: 'name1',
@@ -66,7 +66,7 @@ describe('docgenConverter', () => {
6666
let warnCallCount = 0;
6767
console.warn = () => warnCallCount++;
6868
try {
69-
result = convertToDocgen({
69+
result = convertToDocgen('', {
7070
components: [
7171
{
7272
name: 'name1',
@@ -91,7 +91,7 @@ describe('docgenConverter', () => {
9191
let warnCallCount = 0;
9292
console.warn = () => warnCallCount++;
9393
try {
94-
result = convertToDocgen({
94+
result = convertToDocgen('', {
9595
components: [
9696
{
9797
name: 'name1',
@@ -109,4 +109,10 @@ describe('docgenConverter', () => {
109109
assert.equal('name1', result.displayName);
110110
assert.equal('comment1', result.description);
111111
});
112+
113+
it('Should return empty object if there are no components', () => {
114+
const result = convertToDocgen('/root/MyComponent.tsx', { components: [] });
115+
assert.isOk(result);
116+
assert.equal(result.displayName, 'MyComponent');
117+
})
112118
});

src/docgenConverter.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
import * as path from 'path';
12
import { FileDoc, InterfaceDoc, MemberDoc } from './model';
23
import { StyleguidistComponent, StyleguidistProps, PropItem } from './propTypesParser';
34

4-
export function convertToDocgen(doc: FileDoc): StyleguidistComponent | null {
5+
export function convertToDocgen(filePath: string, doc: FileDoc): StyleguidistComponent {
56
const components = doc.components;
67

78
if (components.length === 0) {
8-
return null;
9+
return {
10+
displayName: path.basename(filePath, path.extname(filePath)),
11+
description: '',
12+
props: {},
13+
};
914
}
1015
const comp = components[0];
1116

src/propTypesParser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ export interface PropItemType {
2828
*/
2929
export function parse(filePath: string): StyleguidistComponent {
3030
const doc = getFileDocumentation(filePath);
31-
return convertToDocgen(doc);
31+
return convertToDocgen(filePath, doc);
3232
}

0 commit comments

Comments
 (0)