Skip to content

Commit

Permalink
CsfFile: Fix case with no meta export, no title
Browse files Browse the repository at this point in the history
  • Loading branch information
shilman committed May 18, 2021
1 parent 8911a59 commit 02e7491
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
30 changes: 17 additions & 13 deletions lib/csf-tools/src/CsfFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as t from '@babel/types';
import traverse, { Node } from '@babel/traverse';
import { toId, isExportStory } from '@storybook/csf';

const logger = console;
interface Meta {
title?: string;
includeStories?: string[] | RegExp;
Expand Down Expand Up @@ -34,6 +35,12 @@ function parseIncludeExclude(prop: Node) {
throw new Error(`Unknown include/exclude: ${prop}`);
}

const parseTitle = (value: any) => {
if (t.isStringLiteral(value)) return value.value;
logger.warn(`Unexpected meta.title: ${JSON.stringify(value)}`);
return undefined;
};

const parseMeta = (declaration: t.ObjectExpression): Meta => {
const meta: Meta = {};
declaration.properties.forEach((p: Node) => {
Expand All @@ -48,12 +55,6 @@ const parseMeta = (declaration: t.ObjectExpression): Meta => {
});
return meta;
};

const parseTitle = (value: any) => {
if (t.isStringLiteral(value)) return value.value;
return undefined;
};

export class CsfFile {
_ast: Node;

Expand Down Expand Up @@ -128,13 +129,16 @@ export class CsfFile {
});

// default export can come at any point in the file, so we do this post processing last
self._stories = Object.entries(self._stories).reduce((acc, [name, story]) => {
if (isExportStory(name, self._meta)) {
const id = toId(self._meta.title, name);
acc[name] = { ...story, id, parameters: { ...story.parameters, __id: id } };
}
return acc;
}, {} as Record<string, Story>);
self._stories =
self._meta && self._meta.title
? Object.entries(self._stories).reduce((acc, [name, story]) => {
if (isExportStory(name, self._meta)) {
const id = toId(self._meta.title, name);
acc[name] = { ...story, id, parameters: { ...story.parameters, __id: id } };
}
return acc;
}, {} as Record<string, Story>)
: {}; // no meta = no stories
return self;
}

Expand Down
7 changes: 7 additions & 0 deletions lib/csf-tools/src/__testfixtures__/nometa.snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`csf extract nometa 1`] = `
{
"stories": []
}
`;
8 changes: 8 additions & 0 deletions lib/csf-tools/src/__testfixtures__/nometa.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from 'react';
import base from 'paths.macro';

export const story1 = () => <>story1</>;
story1.storyName = 'story 1';

export const story2 = () => <>story2</>;
story2.storyName = 'story 2';

0 comments on commit 02e7491

Please sign in to comment.