Skip to content

Commit 0bcd18e

Browse files
authored
Merge pull request #218 from storybookjs/fix-pre-transform
fix(pre-transform): Move stories target component import declaration from instance to module tag
2 parents 37013cf + 292516a commit 0bcd18e

File tree

2 files changed

+61
-50
lines changed

2 files changed

+61
-50
lines changed

src/compiler/pre-transform/index.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,33 @@ describe(codemodLegacyNodes.name, () => {
250250
]
251251
`);
252252
});
253+
254+
it('moves import declaration of stories target component from instance tag to module tag', async ({
255+
expect,
256+
}) => {
257+
const code = `
258+
<script context="module" lang="ts">
259+
export const meta: Meta<Button> = {
260+
component: Button,
261+
};
262+
</script>
263+
264+
<script>
265+
import { Story } from "${pkg.name}";
266+
import Button from "./Button.svelte";
267+
</script>
268+
`;
269+
270+
const ast = getSvelteAST({ code });
271+
const transformed = await codemodLegacyNodes({ ast });
272+
273+
expect(print(transformed)).toMatchInlineSnapshot(`
274+
"<script context="module" lang="ts">
275+
import { defineMeta } from "@storybook/addon-svelte-csf";
276+
import Button from "./Button.svelte";
277+
278+
const { Story } = defineMeta({ component: Button });
279+
</script>"
280+
`);
281+
});
253282
});

src/compiler/pre-transform/index.ts

Lines changed: 32 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -80,25 +80,23 @@ export async function codemodLegacyNodes(params: Params): Promise<SvelteAST.Root
8080
},
8181

8282
ImportDeclaration(node, context) {
83-
const { source, specifiers } = node;
8483
const { state } = context;
85-
const { value } = source;
8684

87-
if (value === pkg.name) {
88-
const { currentScript } = state;
85+
if (node.source.value === pkg.name) {
86+
state.componentIdentifierName = getComponentsIdentifiersNames(node.specifiers);
8987

90-
state.componentIdentifierName = getComponentsIdentifiersNames(specifiers);
88+
const transformed = transformImportDeclaration({ node, filename });
9189

92-
const transformedImportDeclaration = transformImportDeclaration({ node, filename });
93-
94-
if (currentScript !== 'module') {
90+
if (state.currentScript !== 'module') {
9591
// NOTE: We store current node in AST walker state.
9692
// And will remove it from instance & append it to module in the "clean-up" walk after this one.
97-
state.pkgImportDeclaration = transformedImportDeclaration;
93+
state.pkgImportDeclaration = transformed;
9894
}
9995

100-
return transformedImportDeclaration;
96+
return transformed;
10197
}
98+
99+
return node;
102100
},
103101

104102
ExportNamedDeclaration(node, context) {
@@ -204,33 +202,11 @@ export async function codemodLegacyNodes(params: Params): Promise<SvelteAST.Root
204202
instance = instance ? (visit(instance, state) as SvelteAST.Script) : null;
205203

206204
if (!module) {
207-
const {
208-
pkgImportDeclaration,
209-
defineMetaFromExportConstMeta,
210-
storiesComponentImportDeclaration,
211-
} = state;
212-
213-
let body: ESTreeAST.Program['body'] = [];
214-
215-
// WARN: This scope is bug prone
216-
217-
if (pkgImportDeclaration) {
218-
body.push(pkgImportDeclaration);
219-
}
220-
221-
if (storiesComponentImportDeclaration) {
222-
body.push(storiesComponentImportDeclaration);
223-
}
224-
225-
if (defineMetaFromExportConstMeta) {
226-
body.push(defineMetaFromExportConstMeta);
227-
}
228-
229205
module = createASTScript({
230206
module: true,
231207
content: {
232208
type: 'Program',
233-
body,
209+
body: [],
234210
sourceType: 'module',
235211
},
236212
});
@@ -264,27 +240,23 @@ export async function codemodLegacyNodes(params: Params): Promise<SvelteAST.Root
264240
return { ...rest, content, context: scriptContext };
265241
},
266242

267-
Program(node, _context) {
268-
let { body, ...rest } = node;
269-
const { currentScript, pkgImportDeclaration } = state;
270-
271-
if (pkgImportDeclaration && currentScript === 'instance') {
243+
Program(node, context) {
244+
if (context.state.pkgImportDeclaration && context.state.currentScript === 'instance') {
272245
let instanceBody: ESTreeAST.Program['body'] = [];
273246

274-
for (const declaration of body) {
247+
for (const declaration of node.body) {
275248
if (declaration.type === 'ImportDeclaration') {
276-
const { specifiers, source } = declaration;
277-
278-
if (source.value === pkg.name) {
249+
if (declaration.source.value === pkg.name) {
279250
continue;
280251
}
281252

282-
const { storiesComponentIdentifier } = state;
283-
const storiesComponentSpecifier = specifiers.find(
253+
const { storiesComponentIdentifier } = context.state;
254+
const storiesComponentSpecifier = declaration.specifiers.find(
284255
(s) => s.local.name === storiesComponentIdentifier?.name
285256
);
286257

287258
if (storiesComponentSpecifier) {
259+
// NOTE: We are storing it in the state, so later we will move from instance tag to module tag
288260
state.storiesComponentImportDeclaration = declaration;
289261
continue;
290262
}
@@ -301,18 +273,28 @@ export async function codemodLegacyNodes(params: Params): Promise<SvelteAST.Root
301273
instanceBody.push(declaration);
302274
}
303275

304-
body = instanceBody;
276+
node.body = instanceBody;
305277
}
306278

307-
if (currentScript === 'module') {
308-
const { defineMetaFromComponentMeta } = state;
279+
if (state.currentScript === 'module') {
280+
if (state.storiesComponentImportDeclaration) {
281+
node.body.unshift(state.storiesComponentImportDeclaration);
282+
}
283+
284+
if (state.pkgImportDeclaration) {
285+
node.body.unshift(state.pkgImportDeclaration);
286+
}
287+
288+
if (state.defineMetaFromExportConstMeta) {
289+
node.body.push(state.defineMetaFromExportConstMeta);
290+
}
309291

310-
if (defineMetaFromComponentMeta) {
311-
body.push(defineMetaFromComponentMeta);
292+
if (state.defineMetaFromComponentMeta) {
293+
node.body.push(state.defineMetaFromComponentMeta);
312294
}
313295
}
314296

315-
return { ...rest, body };
297+
return { ...node, body: node.body };
316298
},
317299

318300
Fragment(node, context) {

0 commit comments

Comments
 (0)