Skip to content
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

extend story-exports rule to support export lists, fixes #57 #58

Merged
merged 2 commits into from
Dec 3, 2021
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
28 changes: 10 additions & 18 deletions lib/rules/story-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
* @author Yann Braga
*/

import { isExportStory } from '@storybook/csf'

import { createStorybookRule } from '../utils/create-storybook-rule'
import { CategoryId } from '../utils/constants'
import { getDescriptor, getMetaObjectExpression } from '../utils'
import { isIdentifier, isVariableDeclaration } from '../utils/ast'
import {
getAllNamedExports,
getDescriptor,
getMetaObjectExpression,
isValidStoryExport,
} from '../utils'

//------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -39,9 +41,6 @@ export = createStorybookRule({
// Helpers
//----------------------------------------------------------------------

const isValidStoryExport = (node) =>
isExportStory(node.name, nonStoryExportsConfig) && node.name !== '__namedExportsOrder'

//----------------------------------------------------------------------
// Public
//----------------------------------------------------------------------
Expand All @@ -67,23 +66,16 @@ export = createStorybookRule({
}
},
ExportNamedDeclaration: function (node) {
// if there are specifiers, node.declaration should be null
if (!node.declaration) return

const decl = node.declaration
if (isVariableDeclaration(decl)) {
const { id } = decl.declarations[0]
if (isIdentifier(id)) {
namedExports.push(id)
}
}
namedExports.push(...getAllNamedExports(node))
},
'Program:exit': function (node) {
if (hasStoriesOfImport || !meta) {
return
}

const storyExports = namedExports.filter(isValidStoryExport)
const storyExports = namedExports.filter((exp) =>
isValidStoryExport(exp, nonStoryExportsConfig)
)

if (storyExports.length) {
return
Expand Down
37 changes: 35 additions & 2 deletions lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { ExportDefaultDeclaration, Node } from '@typescript-eslint/types/dist/ast-spec'
import { isExportStory } from '@storybook/csf'
import { ExportDefaultDeclaration } from '@typescript-eslint/types/dist/ast-spec'
import { findVariable } from '@typescript-eslint/experimental-utils/dist/ast-utils'
import { isIdentifier, isObjectExpression, isTSAsExpression, isVariableDeclarator } from './ast'
import {
isIdentifier,
isObjectExpression,
isTSAsExpression,
isVariableDeclaration,
isVariableDeclarator,
} from './ast'

export const docsUrl = (ruleName: any) =>
`https://github.com/storybookjs/eslint-plugin-storybook/blob/main/docs/rules/${ruleName}.md`
Expand Down Expand Up @@ -50,3 +57,29 @@ export const getDescriptor = (metaDeclaration, propertyName) => {
throw new Error(`Unexpected descriptor: ${type}`)
}
}

export const isValidStoryExport = (node, nonStoryExportsConfig) =>
isExportStory(node.name, nonStoryExportsConfig) && node.name !== '__namedExportsOrder'

export const getAllNamedExports = (node) => {
// e.g. export { MyStory }
if (!node.declaration && node.specifiers) {
return node.specifiers.reduce((acc, specifier) => {
if (isIdentifier(specifier.exported)) {
acc.push(specifier.exported)
}
return acc
}, [])
}

const decl = node.declaration
if (isVariableDeclaration(decl)) {
const { id } = decl.declarations[0]
// e.g. export const MyStory
if (isIdentifier(id)) {
return [id]
}
}

return []
}
33 changes: 33 additions & 0 deletions tests/lib/rules/story-exports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ ruleTester.run('story-exports', rule, {
import { storiesOf } from '@storybook/react'
storiesOf('MyComponent', module)
`,
`
const Primary = {}
const Secondary = {}
export default {}
export { Primary, Secondary }
`,
dedent`
export default {
excludeStories: /.*Data$/,
}

const mockData = {}
const Primary = {}

export { mockData, Primary }
`
],
invalid: [
{
Expand Down Expand Up @@ -73,5 +89,22 @@ ruleTester.run('story-exports', rule, {
},
],
},
{
code: dedent`
export default {
excludeStories: /.*Data$/,
}

const mockData = {}
const Primary = {}

export { mockData }
`,
errors: [
{
messageId: 'shouldHaveStoryExport',
},
],
},
],
})