Skip to content

default-exports: Support CSF factory style meta #187

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

Merged
merged 1 commit into from
Feb 5, 2025
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
24 changes: 23 additions & 1 deletion lib/rules/default-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export = createStorybookRule({
//----------------------------------------------------------------------

let hasDefaultExport = false
let isCsf4Style = false
let hasStoriesOfImport = false

return {
Expand All @@ -70,14 +71,35 @@ export = createStorybookRule({
hasStoriesOfImport = true
}
},
VariableDeclaration(node) {
// we check for variables declared at the root in a CSF4 style
// e.g. const meta = config.meta({})
if (node.parent.type === 'Program') {
node.declarations.forEach((declaration) => {
const init = declaration.init

if (init && init.type === 'CallExpression') {
const callee = init.callee

if (
callee.type === 'MemberExpression' &&
callee.property.type === 'Identifier' &&
callee.property.name === 'meta'
) {
isCsf4Style = true
}
}
})
}
},
ExportDefaultSpecifier: function () {
hasDefaultExport = true
},
ExportDefaultDeclaration: function () {
hasDefaultExport = true
},
'Program:exit': function (program: TSESTree.Program) {
if (!hasDefaultExport && !hasStoriesOfImport) {
if (!isCsf4Style && !hasDefaultExport && !hasStoriesOfImport) {
const componentName = getComponentName(program, context.getFilename())
const firstNonImportStatement = program.body.find((n) => !isImportDeclaration(n))
const node = firstNonImportStatement || program.body[0] || program
Expand Down
8 changes: 6 additions & 2 deletions tests/lib/rules/default-exports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ ruleTester.run('default-exports', rule, {
export default meta
`,
`
const meta: ComponentMeta<typeof Button> = { title: 'Button', component: Button }
export default meta
const meta: ComponentMeta<typeof Button> = { title: 'Button', component: Button }
export default meta
`,
`
import { config } from '#.storybook/preview'
const meta = config.meta({})
`,
`
import { storiesOf } from '@storybook/react'
Expand Down
Loading