-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
ESLint: add rule only-csf3 #31965
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
Closed
Closed
ESLint: add rule only-csf3 #31965
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2da8296
feat: add rule only-csf3
cylewaitforit 9efb0c3
fix: ai agent review suggestions
cylewaitforit 5c6b792
test: update default render tests
cylewaitforit ef404ed
test: update complex test to show property order is preserved
cylewaitforit 8168e85
refactor: simplify property ordering
cylewaitforit 194a9ff
Merge branch 'next' into rule-only-csf3
cylewaitforit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| # Enforce CSF3 format for stories (only-csf3) | ||
|
|
||
| [Component Story Format 3.0 (CSF3)](https://storybook.js.org/blog/component-story-format-3-0/) is the latest iteration of Storybook's story format, offering a simpler and more maintainable way to write stories. This rule enforces the use of CSF3 by identifying and reporting CSF2 patterns. | ||
|
|
||
| <!-- RULE-CATEGORIES:START --> | ||
|
|
||
| **Included in these configurations**: N/A | ||
|
|
||
| <!-- RULE-CATEGORIES:END --> | ||
|
|
||
| ## Rule Details | ||
|
|
||
| This rule aims to prevent the use of CSF2 patterns in story files and encourage migration to CSF3. | ||
|
|
||
| Examples of **incorrect** code: | ||
|
|
||
| ```js | ||
| // ❌ CSF2: Using Template.bind({}) | ||
| const Template = (args) => <Button {...args} />; | ||
| export const Primary = Template.bind({}); | ||
| Primary.args = { label: 'Primary' }; | ||
|
|
||
| // ❌ CSF2: Story function declaration | ||
| export function Secondary(args) { | ||
| return <Button {...args} />; | ||
| } | ||
|
|
||
| // ❌ CSF2: Story arrow function | ||
| export const Tertiary = () => <Button>Click me</Button>; | ||
|
|
||
| // ❌ CSF2: Story with property assignments | ||
| export const WithArgs = Template.bind({}); | ||
| WithArgs.args = { label: 'With Args' }; | ||
| WithArgs.parameters = { layout: 'centered' }; | ||
|
|
||
| // ❌ CSF2: Template.bind({}) with multiple stories | ||
| const Template = (args) => <Button {...args} />; | ||
|
|
||
| export const Primary = Template.bind({}); | ||
| Primary.args = { label: 'Primary', variant: 'primary' }; | ||
| Primary.parameters = { backgrounds: { default: 'light' } }; | ||
|
|
||
| export const Secondary = Template.bind({}); | ||
| Secondary.args = { label: 'Secondary', variant: 'secondary' }; | ||
| Secondary.parameters = { backgrounds: { default: 'dark' } }; | ||
| ``` | ||
|
|
||
| Examples of **correct** code: | ||
|
|
||
| ```js | ||
| // ✅ CSF3: Object literal with args | ||
| export const Primary = { | ||
| args: { | ||
| label: 'Primary', | ||
| }, | ||
| }; | ||
jonniebigodes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // ✅ CSF3: Object literal with render function | ||
| export const Secondary = { | ||
| render: (args) => <Button {...args}>Secondary</Button>, | ||
| }; | ||
|
|
||
| // ✅ CSF3: Multiple stories sharing render logic | ||
| const render = (args) => <Button {...args} />; | ||
|
|
||
| export const Primary = { | ||
| render, | ||
| args: { label: 'Primary', variant: 'primary' }, | ||
| parameters: { backgrounds: { default: 'light' } }, | ||
| }; | ||
|
|
||
| export const Secondary = { | ||
| render, | ||
| args: { label: 'Secondary', variant: 'secondary' }, | ||
| parameters: { backgrounds: { default: 'dark' } }, | ||
| }; | ||
| ``` | ||
|
|
||
| ## When Not To Use It | ||
|
|
||
| If you're maintaining a legacy Storybook project that extensively uses CSF2 patterns and cannot migrate to CSF3 yet, you might want to disable this rule. | ||
|
|
||
| ## Migration Examples | ||
|
|
||
| Here are examples of how to migrate common CSF2 patterns to CSF3: | ||
|
|
||
| 1. Template.bind({}) with args: | ||
|
|
||
| ```js | ||
| // ❌ CSF2 | ||
| const Template = (args) => <Button {...args} />; | ||
| export const Primary = Template.bind({}); | ||
| Primary.args = { label: 'Primary' }; | ||
|
|
||
| // ✅ CSF3 | ||
| export const Primary = { | ||
| render: (args) => <Button {...args} />, | ||
| args: { label: 'Primary' }, | ||
| }; | ||
| ``` | ||
|
|
||
| 2. Function declaration stories: | ||
|
|
||
| ```js | ||
| // ❌ CSF2 | ||
| export function Primary(args) { | ||
| return <Button {...args}>Primary</Button>; | ||
| } | ||
|
|
||
| // ✅ CSF3 | ||
| export const Primary = { | ||
| render: (args) => <Button {...args}>Primary</Button>, | ||
| }; | ||
| ``` | ||
|
|
||
| 3. Story with multiple properties: | ||
|
|
||
| ```js | ||
| // ❌ CSF2 | ||
| export const Primary = Template.bind({}); | ||
| Primary.args = { label: 'Primary' }; | ||
| Primary.parameters = { layout: 'centered' }; | ||
| Primary.decorators = [ | ||
| (Story) => ( | ||
| <div style={{ padding: '1rem' }}> | ||
| <Story /> | ||
| </div> | ||
| ), | ||
| ]; | ||
|
|
||
| // ✅ CSF3 | ||
| export const Primary = { | ||
| render: (args) => <Button {...args} />, | ||
| args: { label: 'Primary' }, | ||
| parameters: { layout: 'centered' }, | ||
| decorators: [ | ||
| (Story) => ( | ||
| <div style={{ padding: '1rem' }}> | ||
| <Story /> | ||
| </div> | ||
| ), | ||
| ], | ||
| }; | ||
| ``` | ||
|
|
||
| ## Further Reading | ||
|
|
||
| - [Component Story Format 3.0](https://storybook.js.org/blog/component-story-format-3-0/) | ||
| - [Migrating to CSF3](https://storybook.js.org/docs/migration-guide/from-older-version#csf-2-to-csf-3) | ||
| - [Upgrading from CSF 2.0 to 3.0](https://storybook.js.org/docs/api/csf/index#upgrading-from-csf-2-to-csf-3) | ||
| - [Writing Stories in Storybook](https://storybook.js.org/docs/writing-stories#component-story-format) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not initially including in any config. If the csf-strict config were to be fixed by #31963 merging, I think this rule would fit well in that config.