Skip to content

Commit

Permalink
Merge pull request #69 from storybookjs/fix/no-redundant-story-name-c…
Browse files Browse the repository at this point in the history
…sf-tool

no-redundant-story-name: use same naming algorithm as storybook
  • Loading branch information
yannbf authored Dec 20, 2021
2 parents 83d00fa + 6d185a7 commit bec472c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
21 changes: 4 additions & 17 deletions lib/rules/no-redundant-story-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* @author Yann Braga
*/

import { storyNameFromExport } from '@storybook/csf'

import {
isExpressionStatement,
isLiteral,
Expand Down Expand Up @@ -39,21 +41,6 @@ export = createStorybookRule({
},

create(context: any) {
// variables should be defined here

//----------------------------------------------------------------------
// Helpers
//----------------------------------------------------------------------

//@TODO use the correct name resolver (equivalent to lodash.startcase used in @storybook/csf)
const resolveStoryName = (str: any) =>
str
.replace(/([A-Z]{1,})/g, ' $1')
.replace(/(^\w|\s\w)/g, (m: any) => m.toUpperCase())
.split(' ')
.filter(Boolean)
.join(' ')

//----------------------------------------------------------------------
// Public
//----------------------------------------------------------------------
Expand All @@ -80,7 +67,7 @@ export = createStorybookRule({
}

const { name } = id
const resolvedStoryName = resolveStoryName(name)
const resolvedStoryName = storyNameFromExport(name)

//@ts-ignore
if (isLiteral(storyNameNode.value) && storyNameNode.value.value === resolvedStoryName) {
Expand Down Expand Up @@ -108,7 +95,7 @@ export = createStorybookRule({
if (isIdentifier(left.property) && left.property.name === 'storyName') {
const propertyName = left.object.name
const propertyValue = right.value
const resolvedStoryName = resolveStoryName(propertyName)
const resolvedStoryName = storyNameFromExport(propertyName)

if (propertyValue === resolvedStoryName) {
context.report({
Expand Down
6 changes: 6 additions & 0 deletions tests/lib/rules/no-redundant-story-name.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ ruleTester.run('no-redundant-story-name', rule, {
}
PrimaryButton.storyName = 'The Primary Button'
`,
`
export function H1 () {
return <h1>Hello</h1>
}
H1.storyName = 'H1'
`,
],

invalid: [
Expand Down

0 comments on commit bec472c

Please sign in to comment.