Skip to content

Commit 7ff295c

Browse files
authored
Merge pull request #187 from storybookjs/feat/support-csf4-style-in-default-exports-rule
default-exports: Support CSF factory style meta
2 parents d8acf1f + 1af857a commit 7ff295c

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

lib/rules/default-exports.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export = createStorybookRule({
6262
//----------------------------------------------------------------------
6363

6464
let hasDefaultExport = false
65+
let isCsf4Style = false
6566
let hasStoriesOfImport = false
6667

6768
return {
@@ -70,14 +71,35 @@ export = createStorybookRule({
7071
hasStoriesOfImport = true
7172
}
7273
},
74+
VariableDeclaration(node) {
75+
// we check for variables declared at the root in a CSF4 style
76+
// e.g. const meta = config.meta({})
77+
if (node.parent.type === 'Program') {
78+
node.declarations.forEach((declaration) => {
79+
const init = declaration.init
80+
81+
if (init && init.type === 'CallExpression') {
82+
const callee = init.callee
83+
84+
if (
85+
callee.type === 'MemberExpression' &&
86+
callee.property.type === 'Identifier' &&
87+
callee.property.name === 'meta'
88+
) {
89+
isCsf4Style = true
90+
}
91+
}
92+
})
93+
}
94+
},
7395
ExportDefaultSpecifier: function () {
7496
hasDefaultExport = true
7597
},
7698
ExportDefaultDeclaration: function () {
7799
hasDefaultExport = true
78100
},
79101
'Program:exit': function (program: TSESTree.Program) {
80-
if (!hasDefaultExport && !hasStoriesOfImport) {
102+
if (!isCsf4Style && !hasDefaultExport && !hasStoriesOfImport) {
81103
const componentName = getComponentName(program, context.getFilename())
82104
const firstNonImportStatement = program.body.find((n) => !isImportDeclaration(n))
83105
const node = firstNonImportStatement || program.body[0] || program

tests/lib/rules/default-exports.test.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ ruleTester.run('default-exports', rule, {
2626
export default meta
2727
`,
2828
`
29-
const meta: ComponentMeta<typeof Button> = { title: 'Button', component: Button }
30-
export default meta
29+
const meta: ComponentMeta<typeof Button> = { title: 'Button', component: Button }
30+
export default meta
31+
`,
32+
`
33+
import { config } from '#.storybook/preview'
34+
const meta = config.meta({})
3135
`,
3236
`
3337
import { storiesOf } from '@storybook/react'

0 commit comments

Comments
 (0)