CsfPlugin: Fix options handling, add tests

This commit is contained in:
Michael Shilman 2022-11-01 00:12:52 +08:00
parent ec5dc1c821
commit ffc95536ad
3 changed files with 121 additions and 29 deletions

View File

@ -1,11 +1,9 @@
import { createUnplugin } from 'unplugin';
import fs from 'fs/promises';
import { loadCsf, enrichCsf, formatCsf } from '@storybook/csf-tools';
import type { EnrichCsfOptions } from '@storybook/csf-tools';
export interface CsfPluginOptions {
source?: boolean;
description?: boolean;
}
export type CsfPluginOptions = EnrichCsfOptions;
const STORIES_REGEX = /\.(story|stories)\.[tj]sx?$/;

View File

@ -4,17 +4,18 @@
import { dedent } from 'ts-dedent';
import { loadCsf, formatCsf } from './CsfFile';
import { enrichCsf, extractSource } from './enrichCsf';
import type { EnrichCsfOptions } from './enrichCsf';
expect.addSnapshotSerializer({
print: (val: any) => val,
test: (val) => true,
});
const enrich = (code: string) => {
const enrich = (code: string, options?: EnrichCsfOptions) => {
// we don't actually care about the title
const csf = loadCsf(code, { makeTitle: (userTitle) => userTitle || 'default' }).parse();
enrichCsf(csf);
enrichCsf(csf, options);
return formatCsf(csf);
};
@ -294,6 +295,88 @@ describe('enrichCsf', () => {
`);
});
});
describe('options', () => {
it('disableSource', () => {
expect(
enrich(
dedent`
export default {
title: 'Button',
}
/** The most basic button */
export const Basic = () => <Button />
`,
{ disableSource: true }
)
).toMatchInlineSnapshot(`
export default {
title: 'Button'
};
/** The most basic button */
export const Basic = () => <Button />;
Basic.parameters = {
...Basic.parameters,
docs: {
...Basic.parameters?.docs,
description: {
story: "The most basic button",
...Basic.parameters?.docs?.description
}
}
};
`);
});
it('disableDescription', () => {
expect(
enrich(
dedent`
export default {
title: 'Button',
}
/** The most basic button */
export const Basic = () => <Button />
`,
{ disableDescription: true }
)
).toMatchInlineSnapshot(`
export default {
title: 'Button'
};
/** The most basic button */
export const Basic = () => <Button />;
Basic.parameters = {
...Basic.parameters,
storySource: {
source: "() => <Button />",
...Basic.parameters?.storySource
}
};
`);
});
it('disable all', () => {
expect(
enrich(
dedent`
export default {
title: 'Button',
}
/** The most basic button */
export const Basic = () => <Button />
`,
{ disableSource: true, disableDescription: true }
)
).toMatchInlineSnapshot(`
export default {
title: 'Button'
};
/** The most basic button */
export const Basic = () => <Button />;
`);
});
});
});
const source = (csfExport: string) => {

View File

@ -4,32 +4,41 @@ import * as t from '@babel/types';
import * as generate from '@babel/generator';
import type { CsfFile } from './CsfFile';
export const enrichCsf = (csf: CsfFile) => {
Object.keys(csf._storyExports).forEach((key) => {
export interface EnrichCsfOptions {
disableSource?: boolean;
disableDescription?: boolean;
}
export const enrichCsf = (csf: CsfFile, options?: EnrichCsfOptions) => {
const enablesSource = Object.keys(csf._storyExports).forEach((key) => {
const storyExport = csf.getStoryExport(key);
const source = extractSource(storyExport);
const description = extractDescription(csf._storyStatements[key]);
const source = !options?.disableSource && extractSource(storyExport);
const description =
!options?.disableDescription && extractDescription(csf._storyStatements[key]);
const parameters = [];
// storySource: { source: %%source%% },
const originalParameters = t.memberExpression(t.identifier(key), t.identifier('parameters'));
parameters.push(t.spreadElement(originalParameters));
const optionalStorySource = t.optionalMemberExpression(
originalParameters,
t.identifier('storySource'),
false,
true
);
parameters.push(
t.objectProperty(
if (source) {
const optionalStorySource = t.optionalMemberExpression(
originalParameters,
t.identifier('storySource'),
t.objectExpression([
t.objectProperty(t.identifier('source'), t.stringLiteral(source)),
t.spreadElement(optionalStorySource),
])
)
);
false,
true
);
parameters.push(
t.objectProperty(
t.identifier('storySource'),
t.objectExpression([
t.objectProperty(t.identifier('source'), t.stringLiteral(source)),
t.spreadElement(optionalStorySource),
])
)
);
}
// docs: { description: { story: %%description%% } },
if (description) {
const optionalDocs = t.optionalMemberExpression(
@ -62,10 +71,12 @@ export const enrichCsf = (csf: CsfFile) => {
)
);
}
const addParameter = t.expressionStatement(
t.assignmentExpression('=', originalParameters, t.objectExpression(parameters))
);
csf._ast.program.body.push(addParameter);
if (parameters.length > 1) {
const addParameter = t.expressionStatement(
t.assignmentExpression('=', originalParameters, t.objectExpression(parameters))
);
csf._ast.program.body.push(addParameter);
}
});
};