Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ describe('preview specific functionality', () => {
});
`);
});
it('should work', async () => {
it('should wrap definePreview for mixed annotations and default export', async () => {
await expect(
transform(dedent`
export const decorators = [1]
Expand All @@ -259,4 +259,61 @@ describe('preview specific functionality', () => {
});
`);
});

it('should wrap definePreview for const defined preview with type annotations', async () => {
await expect(
transform(dedent`
import { type Preview } from '@storybook/react-vite';

const preview = {
decorators: [],

parameters: {
options: {}
}
} satisfies Preview;

export default preview;

`)
).resolves.toMatchInlineSnapshot(`
import { definePreview } from '@storybook/react-vite';

export default definePreview({
decorators: [],

parameters: {
options: {},
},
});
`);
});

it('should wrap definePreview for mixed annotations and default const export', async () => {
await expect(
transform(dedent`
import { type Preview } from '@storybook/react-vite';
export const decorators = []
const preview = {

parameters: {
options: {}
}
} satisfies Preview;

export default preview;

`)
).resolves.toMatchInlineSnapshot(`
import { definePreview } from '@storybook/react-vite';

export default definePreview({
decorators: [],

parameters: {
options: {},
},
});
`);
});
});
43 changes: 22 additions & 21 deletions code/lib/cli-storybook/src/codemod/helpers/config-to-csf-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ export async function configToCsfFactory(
const defineConfigProps = getConfigProperties(exportDecls, { configType });
const hasNamedExports = defineConfigProps.length > 0;

function findDeclarationNodeIndex(declarationName: string): number {
return programNode.body.findIndex(
(n) =>
t.isVariableDeclaration(n) &&
n.declarations.some((d) => {
let declaration = d.init;
// unwrap TS type annotations
if (t.isTSAsExpression(declaration) || t.isTSSatisfiesExpression(declaration)) {
declaration = declaration.expression;
}
return (
t.isIdentifier(d.id) &&
d.id.name === declarationName &&
t.isObjectExpression(declaration)
);
})
);
}

/**
* Scenario 1: Mixed exports
*
Expand Down Expand Up @@ -60,16 +79,7 @@ export async function configToCsfFactory(
if (t.isExportDefaultDeclaration(node) && t.isIdentifier(node.declaration)) {
const declarationName = node.declaration.name;

declarationNodeIndex = programNode.body.findIndex(
(n) =>
t.isVariableDeclaration(n) &&
n.declarations.some(
(d) =>
t.isIdentifier(d.id) &&
d.id.name === declarationName &&
t.isObjectExpression(d.init)
)
);
declarationNodeIndex = findDeclarationNodeIndex(declarationName);

if (declarationNodeIndex !== -1) {
exportDefaultNode = node;
Expand Down Expand Up @@ -97,7 +107,7 @@ export async function configToCsfFactory(
/**
* Scenario 2: Default exports
*
* - Syntax 1: `default export const config = {}; export default config;`
* - Syntax 1: `const config = {}; export default config;`
* - Syntax 2: `export default {};`
*
* Transform into: `export default defineMain({})`
Expand All @@ -112,16 +122,7 @@ export async function configToCsfFactory(
if (t.isExportDefaultDeclaration(node) && t.isIdentifier(node.declaration)) {
const declarationName = node.declaration.name;

declarationNodeIndex = programNode.body.findIndex(
(n) =>
t.isVariableDeclaration(n) &&
n.declarations.some(
(d) =>
t.isIdentifier(d.id) &&
d.id.name === declarationName &&
t.isObjectExpression(d.init)
)
);
declarationNodeIndex = findDeclarationNodeIndex(declarationName);

if (declarationNodeIndex !== -1) {
exportDefaultNode = node;
Expand Down