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 { DocgenButton } from '../../components/DocgenButton';
const docsTitle = title => `Docs/${title}`;
export default {
title: 'Addons/Docs/stories',
title: `Addons/${docsTitle('stories')}`,
component: DocgenButton,
};

View File

@ -96,31 +96,40 @@ export function findExportsMap(ast) {
if (!titleProperty) {
return { addsMap, idsToFrameworks };
}
const title = titleProperty.value.value;
estraverse.traverse(ast, {
fallback: 'iteration',
enter: node => {
patchNode(node);
if (
node.type === 'ExportNamedDeclaration' &&
node.declaration &&
node.declaration.declarations &&
node.declaration.declarations.length === 1 &&
node.declaration.declarations[0].type === 'VariableDeclarator' &&
node.declaration.declarations[0].id &&
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);
}
},
});
const titleValue = titleProperty.value;
let title;
if (titleValue.type === 'TemplateLiteral' && titleValue.quasis.length > 0) {
// if a tagged template string.
title = titleValue.quasis[0].value.raw;
} else {
// if title is string: 'StringLiteral'
title = titleProperty.value.value;
}
if (title) {
estraverse.traverse(ast, {
fallback: 'iteration',
enter: node => {
patchNode(node);
if (
node.type === 'ExportNamedDeclaration' &&
node.declaration &&
node.declaration.declarations &&
node.declaration.declarations.length === 1 &&
node.declaration.declarations[0].type === 'VariableDeclarator' &&
node.declaration.declarations[0].id &&
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 };
}