fileoverview author Yann Braga
  import { storyNameFromExport } from '@storybook/csf' ; import { isExpressionStatement , isLiteral , isIdentifier , isObjectExpression , isProperty , isVariableDeclaration , } from '../utils/ast' ; import { CategoryId } from '../utils/constants' ; import { createStorybookRule } from '../utils/create-storybook-rule' ; export = createStorybookRule ( { name : 'no-redundant-story-name' , defaultOptions : [ ] , meta : { type : 'suggestion' , fixable : 'code' , hasSuggestions : true , docs : { description : 'A story should not have a redundant name property' , categories : [ CategoryId . CSF , CategoryId . RECOMMENDED ] , recommended : 'warn' , } , messages : { removeRedundantName : 'Remove redundant name' , storyNameIsRedundant : 'Named exports should not use the name annotation if it is redundant to the name that would be generated by the export name' , } , schema : [ ] , } , create ( context ) { return { ExportNamedDeclaration : function ( node ) { if ( ! node . declaration ) return ; const decl = node . declaration ; if ( isVariableDeclaration ( decl ) ) { const { id , init } = decl . declarations [ 0 ] ; if ( isIdentifier ( id ) && isObjectExpression ( init ) ) { const storyNameNode = init . properties . find ( ( prop ) => isProperty ( prop ) && isIdentifier ( prop . key ) && ( prop . key ?. name === 'name' || prop . key ?. name === 'storyName' ) ) ; if ( ! storyNameNode ) { return ; } const { name } = id ; const resolvedStoryName = storyNameFromExport ( name ) ; if ( isLiteral ( storyNameNode . value ) && storyNameNode . value . value === resolvedStoryName ) { context . report ( { node : storyNameNode , messageId : 'storyNameIsRedundant' , suggest : [ { messageId : 'removeRedundantName' , fix : function ( fixer ) { return fixer . remove ( storyNameNode ) ; } , } , ] , } ) ; } } } } , AssignmentExpression : function ( node ) { if ( ! isExpressionStatement ( node . parent ) ) return ; const { left , right } = node ; if ( isIdentifier ( left . property ) && left . property . name === 'storyName' ) { const propertyName = left . object . name ; const propertyValue = right . value ; const resolvedStoryName = storyNameFromExport ( propertyName ) ; if ( propertyValue === resolvedStoryName ) { context . report ( { node : node , messageId : 'storyNameIsRedundant' , suggest : [ { messageId : 'removeRedundantName' , fix : function ( fixer ) { return fixer . remove ( node ) ; } , } , ] , } ) ; } } } , } ; } , } ) ;