diff --git a/code/lib/csf-tools/src/CsfFile.ts b/code/lib/csf-tools/src/CsfFile.ts index 075aa83588b5..53da26d6680f 100644 --- a/code/lib/csf-tools/src/CsfFile.ts +++ b/code/lib/csf-tools/src/CsfFile.ts @@ -169,6 +169,10 @@ export class CsfFile { _storyExports: Record = {}; + _metaStatement: t.Statement | undefined; + + _metaNode: t.Expression | undefined; + _storyStatements: Record = {}; _storyAnnotations: Record> = {}; @@ -259,10 +263,27 @@ export class CsfFile { ExportDefaultDeclaration: { enter({ node, parent }) { let metaNode: t.ObjectExpression; - const decl = - t.isIdentifier(node.declaration) && t.isProgram(parent) - ? findVarInitialization(node.declaration.name, parent) - : node.declaration; + const isVariableReference = t.isIdentifier(node.declaration) && t.isProgram(parent); + let decl; + if (isVariableReference) { + // const meta = { ... }; + // export default meta; + const variableName = (node.declaration as t.Identifier).name; + const isVariableDeclarator = (declaration: t.VariableDeclarator) => + t.isIdentifier(declaration.id) && declaration.id.name === variableName; + + self._metaStatement = self._ast.program.body.find( + (topLevelNode) => + t.isVariableDeclaration(topLevelNode) && + topLevelNode.declarations.find(isVariableDeclarator) + ); + decl = (self._metaStatement as t.VariableDeclaration).declarations.find( + isVariableDeclarator + ).init; + } else { + self._metaStatement = node; + decl = node.declaration; + } if (t.isObjectExpression(decl)) { // export default { ... }; @@ -276,6 +297,7 @@ export class CsfFile { } if (!self._meta && metaNode && t.isProgram(parent)) { + self._metaNode = metaNode; self._parseMeta(metaNode, parent); } }, diff --git a/code/lib/csf-tools/src/enrichCsf.test.ts b/code/lib/csf-tools/src/enrichCsf.test.ts index a0bb3af2db25..f9db130ed6a2 100644 --- a/code/lib/csf-tools/src/enrichCsf.test.ts +++ b/code/lib/csf-tools/src/enrichCsf.test.ts @@ -132,7 +132,7 @@ describe('enrichCsf', () => { }); }); - describe('descriptions', () => { + describe('story descriptions', () => { it('skips inline comments', () => { expect( enrich(dedent` @@ -296,6 +296,283 @@ describe('enrichCsf', () => { }); }); + describe('meta descriptions', () => { + it('skips inline comments', () => { + expect( + enrich(dedent` + // The most basic button + export default { + title: 'Button', + } + export const Basic = () =>