Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw descriptive error when 'of' prop is detected #37

Merged
merged 2 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,34 @@ describe('docs-mdx-compiler-plugin', () => {
).rejects.toThrow('Expected a Story name, id, or story attribute');
});

it("errors on story 'of' prop", async () => {
await expect(async () =>
clean(dedent`
import * as MyStories from './My.stories';
import { Story, Meta } from '@storybook/addon-docs';
<Meta title="Button" />
# Bad story
<Story of={MyStories.Primary} />
`)
).rejects.toThrow(`The 'of' prop is not supported in .stories.mdx files, only .mdx files.
See https://storybook.js.org/docs/7.0/react/writing-docs/mdx on how to write MDX files and stories separately.`);
});

it("errors on meta 'of' prop", async () => {
await expect(async () =>
clean(dedent`
import * as MyStories from './My.stories';
import { Meta } from '@storybook/addon-docs';
<Meta title="Button" of={MyStories} />
`)
).rejects.toThrow(`The 'of' prop is not supported in .stories.mdx files, only .mdx files.
See https://storybook.js.org/docs/7.0/react/writing-docs/mdx on how to write MDX files and stories separately.`);
});

describe('csf3', () => {
it('auto-title-docs-only.mdx', () => {
expect(
Expand Down
10 changes: 10 additions & 0 deletions src/sb-mdx-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ const expressionOrNull = (attr: t.JSXAttribute['value']) =>
t.isJSXExpressionContainer(attr) ? attr.expression : null;

export function genStoryExport(ast: t.JSXElement, context: Context) {
if (getAttr(ast.openingElement, 'of')) {
throw new Error(`The 'of' prop is not supported in .stories.mdx files, only .mdx files.
See https://storybook.js.org/docs/7.0/react/writing-docs/mdx on how to write MDX files and stories separately.`);
}

const storyName = idOrNull(getAttr(ast.openingElement, 'name'));
const storyId = idOrNull(getAttr(ast.openingElement, 'id'));
const storyRef = getAttr(ast.openingElement, 'story') as t.JSXExpressionContainer;
Expand Down Expand Up @@ -264,6 +269,11 @@ export function genCanvasExports(ast: t.JSXElement, context: Context) {
}

export function genMeta(ast: t.JSXElement, options: CompilerOptions) {
if (getAttr(ast.openingElement, 'of')) {
throw new Error(`The 'of' prop is not supported in .stories.mdx files, only .mdx files.
See https://storybook.js.org/docs/7.0/react/writing-docs/mdx on how to write MDX files and stories separately.`);
}

const titleAttr = getAttr(ast.openingElement, 'title');
const idAttr = getAttr(ast.openingElement, 'id');
let title = null;
Expand Down