Source-loader: Handle template strings in CSF title (#8995)

Source-loader: Handle template strings in CSF title
This commit is contained in:
Michael Shilman 2019-12-02 11:11:59 +08:00 committed by GitHub
commit 1e53a3ae7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 26 deletions

View File

@ -3,8 +3,10 @@ import notes from '../notes/notes.md';
import mdxNotes from '../notes/notes.mdx'; import mdxNotes from '../notes/notes.mdx';
import { DocgenButton } from '../../components/DocgenButton'; import { DocgenButton } from '../../components/DocgenButton';
const docsTitle = title => `Docs/${title}`;
export default { export default {
title: 'Addons/Docs/stories', title: `Addons/${docsTitle('stories')}`,
component: DocgenButton, component: DocgenButton,
}; };

View File

@ -96,31 +96,40 @@ export function findExportsMap(ast) {
if (!titleProperty) { if (!titleProperty) {
return { addsMap, idsToFrameworks }; return { addsMap, idsToFrameworks };
} }
const title = titleProperty.value.value; const titleValue = titleProperty.value;
let title;
estraverse.traverse(ast, { if (titleValue.type === 'TemplateLiteral' && titleValue.quasis.length > 0) {
fallback: 'iteration', // if a tagged template string.
enter: node => { title = titleValue.quasis[0].value.raw;
patchNode(node); } else {
if ( // if title is string: 'StringLiteral'
node.type === 'ExportNamedDeclaration' && title = titleProperty.value.value;
node.declaration && }
node.declaration.declarations && if (title) {
node.declaration.declarations.length === 1 && estraverse.traverse(ast, {
node.declaration.declarations[0].type === 'VariableDeclarator' && fallback: 'iteration',
node.declaration.declarations[0].id && enter: node => {
node.declaration.declarations[0].id.name && patchNode(node);
node.declaration.declarations[0].init && if (
['CallExpression', 'ArrowFunctionExpression', 'FunctionExpression'].includes( node.type === 'ExportNamedDeclaration' &&
node.declaration.declarations[0].init.type node.declaration &&
) node.declaration.declarations &&
) { node.declaration.declarations.length === 1 &&
const storyName = storyNameFromExport(node.declaration.declarations[0].id.name); node.declaration.declarations[0].type === 'VariableDeclarator' &&
const toAdd = handleExportedName(title, storyName, node.declaration.declarations[0].init); node.declaration.declarations[0].id &&
Object.assign(addsMap, toAdd); node.declaration.declarations[0].id.name &&
} node.declaration.declarations[0].init &&
}, ['CallExpression', 'ArrowFunctionExpression', 'FunctionExpression'].includes(
}); node.declaration.declarations[0].init.type
)
) {
const storyName = storyNameFromExport(node.declaration.declarations[0].id.name);
const toAdd = handleExportedName(title, storyName, node.declaration.declarations[0].init);
Object.assign(addsMap, toAdd);
}
},
});
}
return { addsMap, idsToFrameworks }; return { addsMap, idsToFrameworks };
} }