Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TITLE string const as fallback for meta.title default export #593

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/ladle/lib/cli/vite-plugin/parse/get-entry-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import getAst from "../get-ast.js";
import getDefaultExport from "./get-default-export.js";
import getStorynameAndMeta from "./get-storyname-and-meta.js";
import getNamedExports from "./get-named-exports.js";
import getTitleExport from "./get-title-export.js";
import { IMPORT_ROOT } from "../utils.js";
import mdxToStories from "../mdx-to-stories.js";

Expand Down Expand Up @@ -53,10 +54,15 @@ export const getSingleEntry = async (entry) => {
//@ts-ignore
const ast = getAst(code, entry);
traverse(ast, {
Program: getStorynameAndMeta.bind(this, result),
ExportDefaultDeclaration: getDefaultExport.bind(this, result),
});
if (!result.exportDefaultProps.title) {
traverse(ast, {
ExportNamedDeclaration: getTitleExport.bind(this, result),
});
}
traverse(ast, {
ExportDefaultDeclaration: getDefaultExport.bind(this, result),
Program: getStorynameAndMeta.bind(this, result),
});
traverse(ast, {
ExportNamedDeclaration: getNamedExports.bind(this, result),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const getNamedExports = (
},
astPath,
) => {
if (!astPath?.node?.declaration && !astPath?.node?.specifiers) return;

/**
* @param {any} namedExportDeclaration
* @param {string} namedExport
Expand Down Expand Up @@ -79,7 +81,10 @@ const getNamedExports = (
if (namedExportDeclaration.type === "ClassDeclaration") {
namedExport = namedExportDeclaration.id.name;
} else if (namedExportDeclaration.type === "VariableDeclaration") {
namedExport = namedExportDeclaration.declarations[0].id.name;
// Skip TITLE constant export
const declaration = namedExportDeclaration.declarations[0];
if (declaration.id.name === "TITLE") return;
namedExport = declaration.id.name;
} else if (namedExportDeclaration.type === "FunctionDeclaration") {
namedExport = namedExportDeclaration.id.name;
} else {
Expand All @@ -94,6 +99,8 @@ const getNamedExports = (
astPath.node?.specifiers.forEach(
/** type * @param {any} specifier */
(specifier) => {
// Skip TITLE export
if (specifier.exported.name === "TITLE") return;
const namedExport = specifier.exported.name;
const story = namedExportToStory(specifier, namedExport);
stories.push(story);
Expand Down
23 changes: 23 additions & 0 deletions packages/ladle/lib/cli/vite-plugin/parse/get-title-export.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @param {import('../../../shared/types').ParsedStoriesResult} result
* @param {any} astPath
*/
const getTitleExport = (result, astPath) => {
if (!astPath?.node?.declaration) return;

if (astPath.node.declaration.type === "VariableDeclaration") {
const declarations = astPath.node.declaration.declarations;

for (const declaration of declarations) {
if (declaration.id.name === "TITLE") {
if (declaration.init.type !== "StringLiteral") {
throw new Error("TITLE export must be a string literal.");
}
result.exportDefaultProps.title = declaration.init.value;
return;
}
}
}
};

export default getTitleExport;
7 changes: 7 additions & 0 deletions packages/ladle/tests/fixtures/title-constant.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from "react";

export const TITLE = "Constant Title";

export const Story = () => {
return <h1>Story</h1>;
};
7 changes: 7 additions & 0 deletions packages/ladle/tests/parse/get-entry-data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,10 @@ test("Extract and merge story meta", async () => {
);
expect(entryData).toMatchSnapshot();
});

test("TITLE constant is used when no default title exists", async () => {
const entryData = await getSingleEntry(
"tests/fixtures/title-constant.stories.tsx",
);
expect(entryData.exportDefaultProps.title).toBe("Constant Title");
});
Loading