From 1af857a35fee2f31823d1caf532750513e34ad17 Mon Sep 17 00:00:00 2001 From: Yann Braga Date: Fri, 10 Jan 2025 14:34:09 +0100 Subject: [PATCH] default-exports: Support CSF4 style meta --- lib/rules/default-exports.ts | 24 +++++++++++++++++++++++- tests/lib/rules/default-exports.test.ts | 8 ++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/rules/default-exports.ts b/lib/rules/default-exports.ts index e4c18ba..ed4b57e 100644 --- a/lib/rules/default-exports.ts +++ b/lib/rules/default-exports.ts @@ -62,6 +62,7 @@ export = createStorybookRule({ //---------------------------------------------------------------------- let hasDefaultExport = false + let isCsf4Style = false let hasStoriesOfImport = false return { @@ -70,6 +71,27 @@ 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 }, @@ -77,7 +99,7 @@ export = createStorybookRule({ 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 diff --git a/tests/lib/rules/default-exports.test.ts b/tests/lib/rules/default-exports.test.ts index c1d1c2b..57d5c64 100644 --- a/tests/lib/rules/default-exports.test.ts +++ b/tests/lib/rules/default-exports.test.ts @@ -26,8 +26,12 @@ ruleTester.run('default-exports', rule, { export default meta `, ` - const meta: ComponentMeta = { title: 'Button', component: Button } - export default meta + const meta: ComponentMeta = { title: 'Button', component: Button } + export default meta + `, + ` + import { config } from '#.storybook/preview' + const meta = config.meta({}) `, ` import { storiesOf } from '@storybook/react'