Skip to content

Commit

Permalink
fix order issues
Browse files Browse the repository at this point in the history
  • Loading branch information
xeho91 committed Oct 22, 2024
1 parent 881a820 commit 292516a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/compiler/pre-transform/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ describe(codemodLegacyNodes.name, () => {

expect(print(transformed)).toMatchInlineSnapshot(`
"<script context="module">
import Button from "./Button.svelte";
import { defineMeta } from "@storybook/addon-svelte-csf";
import Button from "./Button.svelte";
Expand Down Expand Up @@ -273,6 +272,7 @@ describe(codemodLegacyNodes.name, () => {

expect(print(transformed)).toMatchInlineSnapshot(`
"<script context="module" lang="ts">
import { defineMeta } from "@storybook/addon-svelte-csf";
import Button from "./Button.svelte";
const { Story } = defineMeta({ component: Button });
Expand Down
32 changes: 9 additions & 23 deletions src/compiler/pre-transform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,33 +202,11 @@ export async function codemodLegacyNodes(params: Params): Promise<SvelteAST.Root
instance = instance ? (visit(instance, state) as SvelteAST.Script) : null;

if (!module) {
const {
pkgImportDeclaration,
defineMetaFromExportConstMeta,
storiesComponentImportDeclaration,
} = state;

let body: ESTreeAST.Program['body'] = [];

// WARN: This scope is bug prone

if (pkgImportDeclaration) {
body.push(pkgImportDeclaration);
}

if (storiesComponentImportDeclaration) {
body.push(storiesComponentImportDeclaration);
}

if (defineMetaFromExportConstMeta) {
body.push(defineMetaFromExportConstMeta);
}

module = createASTScript({
module: true,
content: {
type: 'Program',
body,
body: [],
sourceType: 'module',
},
});
Expand Down Expand Up @@ -303,6 +281,14 @@ export async function codemodLegacyNodes(params: Params): Promise<SvelteAST.Root
node.body.unshift(state.storiesComponentImportDeclaration);
}

if (state.pkgImportDeclaration) {
node.body.unshift(state.pkgImportDeclaration);
}

if (state.defineMetaFromExportConstMeta) {
node.body.push(state.defineMetaFromExportConstMeta);
}

if (state.defineMetaFromComponentMeta) {
node.body.push(state.defineMetaFromComponentMeta);
}
Expand Down

0 comments on commit 292516a

Please sign in to comment.