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

fix: Support for legacy source prop when value is TemplateLiteral #245

Merged
merged 3 commits into from
Dec 18, 2024
Merged
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
39 changes: 36 additions & 3 deletions src/compiler/pre-transform/codemods/legacy-story.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,7 @@ describe(transformLegacyStory.name, () => {
`);
});

it("leaves existing Story parameters untouched", async ({
expect,
}) => {
it('leaves existing Story parameters untouched', async ({ expect }) => {
const code = `
<script context="module">
import { Story } from "@storybook/addon-svelte-csf";
Expand Down Expand Up @@ -334,4 +332,39 @@ describe(transformLegacyStory.name, () => {
</Story>"
`);
});

it('legacy `source` prop with template literal value is supported _(moved to parameters)_', async ({
expect,
}) => {
const code = `
<script context="module">
import { Story } from "@storybook/addon-svelte-csf";
</script>

<Story
name="Default"
source={\`
<Foo bar />
\`}
>
<h1>{"Test"}</h1>
</Story>
`;
const component = await parseAndExtractSvelteNode<SvelteAST.Component>(code, 'Component');

expect(
print(
transformLegacyStory({
component,
state: { componentIdentifierName: {} },
})
)
).toMatchInlineSnapshot(`
"<Story name="Default" parameters={{
docs: { source: { code: "\\n <Foo bar />\\n " } }
}}>
<h1>{"Test"}</h1>
</Story>"
`);
});
});
10 changes: 8 additions & 2 deletions src/compiler/pre-transform/codemods/legacy-story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,14 @@ function getSourceValue(attribute: SvelteAST.Attribute): string | undefined {
return;
}

if (!Array.isArray(value) && value.expression.type === 'Literal') {
return value.expression.value as string;
if (!Array.isArray(value)) {
if (value.expression.type === 'Literal' && typeof value.expression.value === 'string') {
return value.expression.value;
}

if (value.expression.type === 'TemplateLiteral') {
return value.expression.quasis.map((q) => q.value.cooked).join('');
}
}

if (value[0].type === 'Text') {
Expand Down
10 changes: 10 additions & 0 deletions tests/stories/LegacyStory.stories.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,13 @@
<Story name="Square" source args={{ rounded: false }}>
{'Test'}
</Story>

<Story
name="TemplateLiterals"
source={`
<LegacyStory rounded={false} />
`}
args={{ rounded: false }}
>
{'Test'}
</Story>
Loading