Skip to content

Commit

Permalink
fix tests & indexer issues
Browse files Browse the repository at this point in the history
  • Loading branch information
xeho91 committed Jul 15, 2024
1 parent 1df5de3 commit 2c36a9e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import { transformImportDeclaration } from './import-declaration';
import { parseAndExtractSvelteNode } from '#tests/extractor';

describe(transformImportDeclaration.name, () => {
it("removes legacy components and add 'defineMEta'", async ({ expect }) => {
it("removes legacy components and add 'defineMeta'", async ({ expect }) => {
const code = `
<script context="module" lang="ts">
import { Story, Template } from "${pkg.name}";
</script>
`;
const node = await parseAndExtractSvelteNode<ImportDeclaration>(code, 'ImportDeclaration');

expect(print(transformImportDeclaration(node))).toMatchInlineSnapshot(
expect(print(transformImportDeclaration({ node }))).toMatchInlineSnapshot(
`"import { defineMeta } from "${pkg.name}";"`
);
});
Expand All @@ -29,7 +29,7 @@ describe(transformImportDeclaration.name, () => {
`;
const node = await parseAndExtractSvelteNode<ImportDeclaration>(code, 'ImportDeclaration');

expect(print(transformImportDeclaration(node))).toMatchInlineSnapshot(
expect(print(transformImportDeclaration({ node }))).toMatchInlineSnapshot(
`"import { defineMeta } from "${pkg.name}";"`
);
});
Expand Down
22 changes: 12 additions & 10 deletions src/compiler/pre-transform/codemods/legacy-story.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe(transformLegacyStory.name, () => {
`;
const node = await parseAndExtractSvelteNode<Component>(code, 'Component');

expect(print(transformLegacyStory(node))).toMatchInlineSnapshot(
expect(print(transformLegacyStory({ node }))).toMatchInlineSnapshot(
`"<Story name="Default" tags={["autodocs"]} />"`
);
});
Expand All @@ -33,7 +33,7 @@ describe(transformLegacyStory.name, () => {
`;
const node = await parseAndExtractSvelteNode<Component>(code, 'Component');

expect(print(transformLegacyStory(node))).toMatchInlineSnapshot(
expect(print(transformLegacyStory({ node }))).toMatchInlineSnapshot(
`"<Story name="Default" tags={["!dev", "autodocs"]} />"`
);
});
Expand All @@ -48,7 +48,9 @@ describe(transformLegacyStory.name, () => {
`;
const node = await parseAndExtractSvelteNode<Component>(code, 'Component');

expect(print(transformLegacyStory(node))).toMatchInlineSnapshot(`"<Story name="Default" />"`);
expect(print(transformLegacyStory({ node }))).toMatchInlineSnapshot(
`"<Story name="Default" />"`
);
});

it("'source' prop when is a text expression gets moved to 'parameters' prop", async ({
Expand All @@ -63,7 +65,7 @@ describe(transformLegacyStory.name, () => {
`;
const node = await parseAndExtractSvelteNode<Component>(code, 'Component');

expect(print(transformLegacyStory(node))).toMatchInlineSnapshot(
expect(print(transformLegacyStory({ node }))).toMatchInlineSnapshot(
`
"<Story name="Default" parameters={{
docs: { source: { code: "'<Button primary />'" } }
Expand Down Expand Up @@ -91,7 +93,7 @@ describe(transformLegacyStory.name, () => {
`;
const node = await parseAndExtractSvelteNode<Component>(code, 'Component');

expect(print(transformLegacyStory(node))).toMatchInlineSnapshot(
expect(print(transformLegacyStory({ node }))).toMatchInlineSnapshot(
`
"<Story name="Default" parameters={{
controls: { disable: true },
Expand Down Expand Up @@ -125,7 +127,7 @@ describe(transformLegacyStory.name, () => {
`;
const node = await parseAndExtractSvelteNode<Component>(code, 'Component');

expect(print(transformLegacyStory(node))).toMatchInlineSnapshot(
expect(print(transformLegacyStory({ node }))).toMatchInlineSnapshot(
`
"<Story name="Default" parameters={{
controls: { disable: true },
Expand All @@ -151,8 +153,8 @@ describe(transformLegacyStory.name, () => {
`;
const node = await parseAndExtractSvelteNode<Component>(code, 'Component');

expect(print(transformLegacyStory(node))).toMatchInlineSnapshot(
`"<Story name="Default" children={someTemplate} />"`
expect(print(transformLegacyStory({ node }))).toMatchInlineSnapshot(
`"<Story name="Default" children={SomeTemplate} />"`
);
});

Expand All @@ -171,7 +173,7 @@ describe(transformLegacyStory.name, () => {
`;
const node = await parseAndExtractSvelteNode<Component>(code, 'Component');

expect(print(transformLegacyStory(node))).toMatchInlineSnapshot(`
expect(print(transformLegacyStory({ node }))).toMatchInlineSnapshot(`
"<Story name="Default">
{#snippet children(args)}
<Button {...args} />
Expand All @@ -195,7 +197,7 @@ describe(transformLegacyStory.name, () => {
`;
const node = await parseAndExtractSvelteNode<Component>(code, 'Component');

expect(print(transformLegacyStory(node))).toMatchInlineSnapshot(`
expect(print(transformLegacyStory({ node }))).toMatchInlineSnapshot(`
"<Story name="Default">
{#snippet children(_args, context)}
<p>{context.id}</p>
Expand Down
44 changes: 22 additions & 22 deletions src/compiler/pre-transform/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe(codemodLegacyNodes.name, () => {
<Story name="Default" />
`);
const ast = getSvelteAST({ code });
const transformed = await codemodLegacyNodes(ast);
const transformed = await codemodLegacyNodes({ ast });

expect(print(transformed)).toMatchInlineSnapshot(`
"<script context="module">
Expand All @@ -49,7 +49,7 @@ describe(codemodLegacyNodes.name, () => {
<Meta title="Atoms/Button" component={Button} />
`);
const ast = getSvelteAST({ code });
const transformed = await codemodLegacyNodes(ast);
const transformed = await codemodLegacyNodes({ ast });

expect(print(transformed)).toMatchInlineSnapshot(
`
Expand Down Expand Up @@ -79,7 +79,7 @@ describe(codemodLegacyNodes.name, () => {
<Story name="Default" />
`);
const ast = getSvelteAST({ code });
const transformed = await codemodLegacyNodes(ast);
const transformed = await codemodLegacyNodes({ ast });

expect(print(transformed)).toMatchInlineSnapshot(`
"<script context="module">
Expand Down Expand Up @@ -116,28 +116,28 @@ describe(codemodLegacyNodes.name, () => {
</Story>
`);
const ast = getSvelteAST({ code });
const transformed = await codemodLegacyNodes(ast);
const transformed = await codemodLegacyNodes({ ast });

expect(print(transformed)).toMatchInlineSnapshot(`
"<script context="module">
import { defineMeta } from "@storybook/addon-svelte-csf";
"<script context="module">
import { defineMeta } from "@storybook/addon-svelte-csf";
/** This is a description for the **Button** component stories. */
const { Story } = defineMeta({ title: "Atoms/Button", component: Button });
</script>
/** This is a description for the **Button** component stories. */
const { Story } = defineMeta({ title: "Atoms/Button", component: Button });
</script>
{#snippet sample(args, context)}
<p>{context.id}</p>
<Button {...args} />
{/snippet}
<Story name="Default" children={sample} tags={["autodocs"]} parameters={{
docs: { source: { code: "<Button {...args} />" } }
}}>
{#snippet children(args, context)}
<p>{context.id}</p>
<Button {...args} />
{/snippet}
</Story>"
`);
{#snippet sample(args, context)}
<p>{context.id}</p>
<Button {...args} />
{/snippet}
<Story name="Default" children={Sample} tags={["autodocs"]} parameters={{
docs: { source: { code: "<Button {...args} />" } }
}}>
{#snippet children(args, context)}
<p>{context.id}</p>
<Button {...args} />
{/snippet}
</Story>"
`);
});
});
18 changes: 8 additions & 10 deletions src/indexer/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ export async function parseForIndexer(
const { module, fragment } = node;
const { state, visit } = context;

if (!module && !legacyTemplate) {
if (module) {
visit(module, state);
} else if (!legacyTemplate) {
throw new MissingModuleTagError(filename);
}

Expand All @@ -94,11 +96,7 @@ export async function parseForIndexer(
const { state, visit } = context;

for (const statement of body) {
if (
legacyTemplate &&
statement.type === 'ImportDeclaration' &&
statement.source.value === pkg.name
) {
if (statement.type === 'ImportDeclaration' && statement.source.value === pkg.name) {
visit(statement, state);
}

Expand Down Expand Up @@ -126,17 +124,17 @@ export async function parseForIndexer(
throw new DefaultOrNamespaceImportUsedError(filename);
}

if (legacyTemplate && specifier.import.name === 'defineMeta') {
if (specifier.imported.name === 'defineMeta') {
state.defineMetaImport = specifier;
}

// TODO: Remove it in the next major version
if (legacyTemplate && specifier.import.name === 'Meta') {
if (legacyTemplate && specifier.imported.name === 'Meta') {
state.legacyMetaImport = specifier;
}

// TODO: Remove it in the next major version
if (legacyTemplate && specifier.import.name === 'Story') {
if (legacyTemplate && specifier.imported.name === 'Story') {
state.legacyStoryImport = specifier;
}
}
Expand Down Expand Up @@ -215,7 +213,7 @@ export async function parseForIndexer(

for (const property of properties) {
if (property.type === 'Property' && property.key.type === 'Identifier') {
visit(node, state);
visit(property, state);
}
}
},
Expand Down

0 comments on commit 2c36a9e

Please sign in to comment.