Skip to content

Commit

Permalink
Use csf-tools in csf factory codemod
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbf committed Jan 17, 2025
1 parent c5e36fc commit 4f56d4f
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 179 deletions.
12 changes: 11 additions & 1 deletion code/core/src/csf-tools/CsfFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { readFile, writeFile } from 'node:fs/promises';
import {
BabelFileClass,
type GeneratorOptions,
type NodePath,
type RecastOptions,
babelParse,
generate,
Expand Down Expand Up @@ -255,10 +256,14 @@ export class CsfFile {

_storyExports: Record<string, t.VariableDeclarator | t.FunctionDeclaration> = {};

_storyPaths: Record<string, NodePath<t.ExportNamedDeclaration>> = {};

_metaStatement: t.Statement | undefined;

_metaNode: t.Expression | undefined;

_metaPath: NodePath<t.ExportDefaultDeclaration> | undefined;

_metaVariableName: string | undefined;

_metaIsFactory: boolean | undefined;
Expand Down Expand Up @@ -466,10 +471,13 @@ export class CsfFile {
self._options.fileName
);
}

self._metaPath = path;
},
},
ExportNamedDeclaration: {
enter({ node, parent }) {
enter(path) {
const { node, parent } = path;
let declarations;
if (t.isVariableDeclaration(node.declaration)) {
declarations = node.declaration.declarations.filter((d) => t.isVariableDeclarator(d));
Expand All @@ -487,6 +495,7 @@ export class CsfFile {
return;
}
self._storyExports[exportName] = decl;
self._storyPaths[exportName] = path;
self._storyStatements[exportName] = node;
let name = storyNameFromExport(exportName);
if (self._storyAnnotations[exportName]) {
Expand Down Expand Up @@ -611,6 +620,7 @@ export class CsfFile {
} else {
self._storyAnnotations[exportName] = {};
self._storyStatements[exportName] = decl;
self._storyPaths[exportName] = path;
self._stories[exportName] = {
id: 'FIXME',
name: exportName,
Expand Down
105 changes: 43 additions & 62 deletions code/lib/codemod/src/transforms/__tests__/csf-3-to-4.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ describe('csf-3-to-4', () => {
export default meta;
`)
).resolves.toMatchInlineSnapshot(`
import { config } from "#.storybook/preview";
const meta = config.meta({
title: 'Component'
});
import { config } from '#.storybook/preview';
const meta = config.meta({ title: 'Component' });
`);
});

Expand All @@ -34,9 +33,10 @@ describe('csf-3-to-4', () => {
export default { title: 'Component' };
`)
).resolves.toMatchInlineSnapshot(`
import { config } from "#.storybook/preview";
import { config } from '#.storybook/preview';
const meta = config.meta({
title: 'Component'
title: 'Component',
});
`);
});
Expand All @@ -48,10 +48,9 @@ describe('csf-3-to-4', () => {
export default componentMeta;
`)
).resolves.toMatchInlineSnapshot(`
import { config } from "#.storybook/preview";
const meta = config.meta({
title: 'Component'
});
import { config } from '#.storybook/preview';
const meta = config.meta({ title: 'Component' });
`);
});

Expand All @@ -66,15 +65,12 @@ describe('csf-3-to-4', () => {
};
`)
).resolves.toMatchInlineSnapshot(`
import { config } from "#.storybook/preview";
const meta = config.meta({
title: 'Component'
});
import { config } from '#.storybook/preview';
const meta = config.meta({ title: 'Component' });
export const A = meta.story({
args: {
primary: true
},
render: args => <Component {...args} />
args: { primary: true },
render: (args) => <Component {...args} />,
});
`);
});
Expand All @@ -91,15 +87,12 @@ describe('csf-3-to-4', () => {
};
`)
).resolves.toMatchInlineSnapshot(`
import { decorators, config } from "#.storybook/preview";
const meta = config.meta({
title: 'Component'
});
import { config, decorators } from '#.storybook/preview';
const meta = config.meta({ title: 'Component' });
export const A = meta.story({
args: {
primary: true
},
render: args => <Component {...args} />
args: { primary: true },
render: (args) => <Component {...args} />,
});
`);
});
Expand All @@ -119,17 +112,14 @@ describe('csf-3-to-4', () => {
`;
it('meta satisfies syntax', async () => {
await expect(transform(metaSatisfies)).resolves.toMatchInlineSnapshot(`
import { config } from "#.storybook/preview";
import { Meta, StoryObj as CSF3 } from '@storybook/react';
import { config } from '#.storybook/preview';
import { ComponentProps } from './Component';
const meta = config.meta({
title: 'Component',
component: Component
});
const meta = config.meta({ title: 'Component', component: Component });
export const A = meta.story({
args: {
primary: true
}
args: { primary: true },
});
`);
});
Expand All @@ -147,17 +137,14 @@ describe('csf-3-to-4', () => {
`;
it('meta as syntax', async () => {
await expect(transform(metaAs)).resolves.toMatchInlineSnapshot(`
import { config } from "#.storybook/preview";
import { Meta, StoryObj as CSF3 } from '@storybook/react';
import { config } from '#.storybook/preview';
import { ComponentProps } from './Component';
const meta = config.meta({
title: 'Component',
component: Component
});
const meta = config.meta({ title: 'Component', component: Component });
export const A = meta.story({
args: {
primary: true
}
args: { primary: true },
});
`);
});
Expand All @@ -175,17 +162,14 @@ describe('csf-3-to-4', () => {
`;
it('story satisfies syntax', async () => {
await expect(transform(storySatisfies)).resolves.toMatchInlineSnapshot(`
import { config } from "#.storybook/preview";
import { Meta, StoryObj as CSF3 } from '@storybook/react';
import { config } from '#.storybook/preview';
import { ComponentProps } from './Component';
const meta = config.meta({
title: 'Component',
component: Component
});
const meta = config.meta({ title: 'Component', component: Component });
export const A = meta.story({
args: {
primary: true
}
args: { primary: true },
});
`);
});
Expand All @@ -203,17 +187,14 @@ describe('csf-3-to-4', () => {
`;
it('story as syntax', async () => {
await expect(transform(storyAs)).resolves.toMatchInlineSnapshot(`
import { config } from "#.storybook/preview";
import { Meta, StoryObj as CSF3 } from '@storybook/react';
import { config } from '#.storybook/preview';
import { ComponentProps } from './Component';
const meta = config.meta({
title: 'Component',
component: Component
});
const meta = config.meta({ title: 'Component', component: Component });
export const A = meta.story({
args: {
primary: true
}
args: { primary: true },
});
`);
});
Expand Down
Loading

0 comments on commit 4f56d4f

Please sign in to comment.