Skip to content

Commit

Permalink
Merge pull request #21452 from storybookjs/21441-fix-missing-of
Browse files Browse the repository at this point in the history
Docs: Don't allow passing `of={}` with undefined prop
  • Loading branch information
tmeasday authored Mar 8, 2023
2 parents bef03d9 + ef7418c commit 0a8dbb8
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 9 deletions.
11 changes: 11 additions & 0 deletions code/ui/blocks/src/blocks/ArgTypes.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react';
import type { Meta, StoryObj } from '@storybook/react';

import { ArgTypes } from './ArgTypes';
Expand Down Expand Up @@ -35,6 +36,16 @@ export const OfStory: Story = {
},
};

export const OfUndefined: Story = {
args: {
// @ts-expect-error this is supposed to be undefined
// eslint-disable-next-line import/namespace
of: ExampleStories.NotDefined,
},
parameters: { chromatic: { disableSnapshot: true } },
decorators: [(s) => (window?.navigator.userAgent.match(/StorybookTestRunner/) ? <div /> : s())],
};

// NOTE: this will throw with no of prop
export const OfStoryUnattached: Story = {
parameters: { attached: false },
Expand Down
3 changes: 3 additions & 0 deletions code/ui/blocks/src/blocks/ArgTypes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ function getArgTypesFromResolved(resolved: ReturnType<typeof useOf>, props: ArgT

export const ArgTypes: FC<ArgTypesProps> = (props) => {
const { of } = props;
if ('of' in props && of === undefined) {
throw new Error('Unexpected `of={undefined}`, did you mistype a CSF file reference?');
}
const resolved = useOf(of || 'meta');
const { argTypes, parameters } = getArgTypesFromResolved(resolved, props);
const argTypesParameters = parameters.docs?.argTypes || ({} as ArgTypesParameters);
Expand Down
3 changes: 3 additions & 0 deletions code/ui/blocks/src/blocks/ArgsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ export const ArgsTable: FC<ArgsTableProps> = (props) => {
({ parameters, component, subcomponents } = context.storyById());
} catch (err) {
const { of } = props as OfProps;
if ('of' in props && of === undefined) {
throw new Error('Unexpected `of={undefined}`, did you mistype a CSF file reference?');
}
({
projectAnnotations: { parameters },
} = context.resolveOf(of, ['component']));
Expand Down
18 changes: 14 additions & 4 deletions code/ui/blocks/src/blocks/Canvas.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,36 @@ export default meta;

type Story = StoryObj<typeof meta>;

export const DefaultAttached: Story = {
export const OfAttached: Story = {
args: {
of: ButtonStories.Primary,
},
};

export const DefaultUnattached: Story = {
export const OfUnattached: Story = {
args: {
of: ButtonStories.Primary,
},
parameters: { attached: false },
};

export const DefaultError: Story = {
export const OfError: Story = {
args: {
of: ButtonStories.ErrorStory,
},
};

export const UndefinedOf: Story = {};
export const DefaultAttached: Story = {};

export const OfUndefined: Story = {
args: {
// @ts-expect-error this is supposed to be undefined
// eslint-disable-next-line import/namespace
of: ButtonStories.NotDefined,
},
parameters: { chromatic: { disableSnapshot: true } },
decorators: [(s) => (window?.navigator.userAgent.match(/StorybookTestRunner/) ? <div /> : s())],
};

export const PropWithToolbar: Story = {
name: 'Prop withToolbar = true',
Expand Down
11 changes: 9 additions & 2 deletions code/ui/blocks/src/blocks/Canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ const useDeprecatedPreviewProps = (
const stories = useStories(storyIds, docsContext);
const isLoading = stories.some((s) => !s);
const sourceProps = useSourceProps(
mdxSource ? { code: decodeURI(mdxSource), of: props.of } : { ids: storyIds, of: props.of },
{
...(mdxSource ? { code: decodeURI(mdxSource) } : { ids: storyIds }),
...(props.of && { of: props.of }),
},
docsContext,
sourceContext
);
Expand Down Expand Up @@ -155,6 +158,10 @@ export const Canvas: FC<CanvasProps & DeprecatedCanvasProps> = (props) => {
const docsContext = useContext(DocsContext);
const sourceContext = useContext(SourceContext);
const { children, of, source } = props;
if ('of' in props && of === undefined) {
throw new Error('Unexpected `of={undefined}`, did you mistype a CSF file reference?');
}

const { isLoading, previewProps } = useDeprecatedPreviewProps(props, docsContext, sourceContext);

let story: PreparedStory;
Expand All @@ -175,7 +182,7 @@ export const Canvas: FC<CanvasProps & DeprecatedCanvasProps> = (props) => {
}
}
try {
sourceProps = useSourceProps({ ...source, of }, docsContext, sourceContext);
sourceProps = useSourceProps({ ...source, ...(of && { of }) }, docsContext, sourceContext);
} catch (error) {
if (!children) {
hookError = error;
Expand Down
11 changes: 11 additions & 0 deletions code/ui/blocks/src/blocks/Controls.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react';
import type { Meta, StoryObj } from '@storybook/react';

import { Controls } from './Controls';
Expand Down Expand Up @@ -30,6 +31,16 @@ export const OfStoryUnattached: Story = {
},
};

export const OfUndefined: Story = {
args: {
// @ts-expect-error this is supposed to be undefined
// eslint-disable-next-line import/namespace
of: ExampleStories.NotDefined,
},
parameters: { chromatic: { disableSnapshot: true } },
decorators: [(s) => (window?.navigator.userAgent.match(/StorybookTestRunner/) ? <div /> : s())],
};

export const IncludeProp: Story = {
args: {
of: ExampleStories.NoParameters,
Expand Down
4 changes: 4 additions & 0 deletions code/ui/blocks/src/blocks/Controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ type ControlsProps = ControlsParameters & {

export const Controls: FC<ControlsProps> = (props) => {
const { of } = props;
if ('of' in props && of === undefined) {
throw new Error('Unexpected `of={undefined}`, did you mistype a CSF file reference?');
}

const context = useContext(DocsContext);
const { story } = context.resolveOf(of || 'story', ['story']);
const { parameters, argTypes } = story;
Expand Down
16 changes: 15 additions & 1 deletion code/ui/blocks/src/blocks/Description.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react';
import type { Meta, StoryObj } from '@storybook/react';
import { Description } from './Description';
import { Button as ButtonComponent } from '../examples/Button';
Expand Down Expand Up @@ -107,9 +108,22 @@ export const OfStoryAsStoryCommentAndParameter: Story = {
},
parameters: { relativeCsfPaths: ['../examples/Button.stories'] },
};
export const OfUndefinedAttached: Story = {
export const DefaultAttached: Story = {
parameters: { relativeCsfPaths: ['../examples/Button.stories'], attached: true },
};
export const OfUndefinedAttached: Story = {
args: {
// @ts-expect-error this is supposed to be undefined
// eslint-disable-next-line import/namespace
of: DefaultButtonStories.NotDefined,
},
parameters: {
chromatic: { disableSnapshot: true },
relativeCsfPaths: ['../examples/Button.stories'],
attached: true,
},
decorators: [(s) => (window?.navigator.userAgent.match(/StorybookTestRunner/) ? <div /> : s())],
};
export const OfStringComponentAttached: Story = {
name: 'Of "component" Attached',
args: {
Expand Down
4 changes: 4 additions & 0 deletions code/ui/blocks/src/blocks/Description.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ const getDescriptionFromDeprecatedProps = (

const DescriptionContainer: FC<DescriptionProps> = (props) => {
const { of, type, markdown: markdownProp, children } = props;

if ('of' in props && of === undefined) {
throw new Error('Unexpected `of={undefined}`, did you mistype a CSF file reference?');
}
const context = useContext(DocsContext);
const resolvedOf = useOf(of || 'meta');
let markdown;
Expand Down
10 changes: 10 additions & 0 deletions code/ui/blocks/src/blocks/Source.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ export const Of: Story = {
},
};

export const OfUndefined: Story = {
args: {
// @ts-expect-error this is supposed to be undefined
// eslint-disable-next-line import/namespace
of: ParametersStories.NotDefined,
},
parameters: { chromatic: { disableSnapshot: true } },
decorators: [(s) => (window?.navigator.userAgent.match(/StorybookTestRunner/) ? <div /> : s())],
};

export const OfTypeProp: Story = {
args: {
of: ParametersStories.NoParameters,
Expand Down
9 changes: 7 additions & 2 deletions code/ui/blocks/src/blocks/Source.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,13 @@ export const useSourceProps = (

// The check didn't actually change the type.
let stories: PreparedStory[] = storiesFromIds as PreparedStory[];
if (props.of) {
const resolved = docsContext.resolveOf(props.of, ['story']);
const { of } = props;
if ('of' in props && of === undefined) {
throw new Error('Unexpected `of={undefined}`, did you mistype a CSF file reference?');
}

if (of) {
const resolved = docsContext.resolveOf(of, ['story']);
stories = [resolved.story];
} else if (stories.length === 0) {
try {
Expand Down
11 changes: 11 additions & 0 deletions code/ui/blocks/src/blocks/Story.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react';
import type { Meta, StoryObj } from '@storybook/react';

import { Story as StoryBlock } from './Story';
Expand Down Expand Up @@ -45,6 +46,16 @@ export const OfError: Story = {
},
};

export const OfUndefined: Story = {
args: {
// @ts-expect-error this is supposed to be undefined
// eslint-disable-next-line import/namespace
of: ButtonStories.NotDefined,
},
parameters: { chromatic: { disableSnapshot: true } },
decorators: [(s) => (window?.navigator.userAgent.match(/StorybookTestRunner/) ? <div /> : s())],
};

export const Inline: Story = {
args: {
of: StoryParametersStories.NoParameters,
Expand Down
4 changes: 4 additions & 0 deletions code/ui/blocks/src/blocks/Story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ export type StoryProps = (StoryDefProps | StoryRefProps) & StoryParameters;

export const getStoryId = (props: StoryProps, context: DocsContextProps): StoryId => {
const { id, of, meta, story } = props as StoryRefProps;
if ('of' in props && of === undefined) {
throw new Error('Unexpected `of={undefined}`, did you mistype a CSF file reference?');
}

if (id) {
deprecate(dedent`Referencing stories by \`id\` is deprecated, please use \`of\` instead.
Expand Down

0 comments on commit 0a8dbb8

Please sign in to comment.