Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Csf-plugin: Support meta description comments #20632

Merged
merged 2 commits into from
Jan 18, 2023
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
30 changes: 26 additions & 4 deletions code/lib/csf-tools/src/CsfFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ export class CsfFile {

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

_metaStatement: t.Statement | undefined;

_metaNode: t.Expression | undefined;

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

_storyAnnotations: Record<string, Record<string, t.Node>> = {};
Expand Down Expand Up @@ -259,10 +263,27 @@ export class CsfFile {
ExportDefaultDeclaration: {
enter({ node, parent }) {
let metaNode: t.ObjectExpression;
const decl =
t.isIdentifier(node.declaration) && t.isProgram(parent)
? findVarInitialization(node.declaration.name, parent)
: node.declaration;
const isVariableReference = t.isIdentifier(node.declaration) && t.isProgram(parent);
let decl;
if (isVariableReference) {
// const meta = { ... };
// export default meta;
const variableName = (node.declaration as t.Identifier).name;
const isVariableDeclarator = (declaration: t.VariableDeclarator) =>
t.isIdentifier(declaration.id) && declaration.id.name === variableName;

self._metaStatement = self._ast.program.body.find(
(topLevelNode) =>
t.isVariableDeclaration(topLevelNode) &&
topLevelNode.declarations.find(isVariableDeclarator)
);
decl = (self._metaStatement as t.VariableDeclaration).declarations.find(
isVariableDeclarator
).init;
} else {
self._metaStatement = node;
decl = node.declaration;
}

if (t.isObjectExpression(decl)) {
// export default { ... };
Expand All @@ -276,6 +297,7 @@ export class CsfFile {
}

if (!self._meta && metaNode && t.isProgram(parent)) {
self._metaNode = metaNode;
self._parseMeta(metaNode, parent);
}
},
Expand Down
279 changes: 278 additions & 1 deletion code/lib/csf-tools/src/enrichCsf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ describe('enrichCsf', () => {
});
});

describe('descriptions', () => {
describe('story descriptions', () => {
it('skips inline comments', () => {
expect(
enrich(dedent`
Expand Down Expand Up @@ -296,6 +296,283 @@ describe('enrichCsf', () => {
});
});

describe('meta descriptions', () => {
it('skips inline comments', () => {
expect(
enrich(dedent`
// The most basic button
export default {
title: 'Button',
}
export const Basic = () => <Button />
`)
).toMatchInlineSnapshot(`
// The most basic button
export default {
title: 'Button'
};
export const Basic = () => <Button />;
Basic.parameters = {
...Basic.parameters,
storySource: {
source: "() => <Button />",
...Basic.parameters?.storySource
}
};
`);
});

it('skips blocks without jsdoc', () => {
expect(
enrich(dedent`
/* The most basic button */
export default {
title: 'Button',
}
export const Basic = () => <Button />
`)
).toMatchInlineSnapshot(`
/* The most basic button */
export default {
title: 'Button'
};
export const Basic = () => <Button />;
Basic.parameters = {
...Basic.parameters,
storySource: {
source: "() => <Button />",
...Basic.parameters?.storySource
}
};
`);
});

it('JSDoc single-line', () => {
expect(
enrich(dedent`
/** The most basic button */
export default {
title: 'Button'
}
export const Basic = () => <Button />
`)
).toMatchInlineSnapshot(`
/** The most basic button */
export default {
title: 'Button',
parameters: {
docs: {
description: {
component: "The most basic button"
}
}
}
};
export const Basic = () => <Button />;
Basic.parameters = {
...Basic.parameters,
storySource: {
source: "() => <Button />",
...Basic.parameters?.storySource
}
};
`);
});

it('JSDoc multi-line', () => {
expect(
enrich(dedent`
/**
* The most basic button
*
* In a block!
*/
export default {
title: 'Button',
}
export const Basic = () => <Button />
`)
).toMatchInlineSnapshot(`
/**
* The most basic button
*
* In a block!
*/
export default {
title: 'Button',
parameters: {
docs: {
description: {
component: "The most basic button\\n\\nIn a block!"
}
}
}
};
export const Basic = () => <Button />;
Basic.parameters = {
...Basic.parameters,
storySource: {
source: "() => <Button />",
...Basic.parameters?.storySource
}
};
`);
});

it('preserves indentation', () => {
expect(
enrich(dedent`
/**
* - A bullet list
* - A sub-bullet
* - A second bullet
*/
export default {
title: 'Button',
}
export const Basic = () => <Button />
`)
).toMatchInlineSnapshot(`
/**
* - A bullet list
* - A sub-bullet
* - A second bullet
*/
export default {
title: 'Button',
parameters: {
docs: {
description: {
component: "- A bullet list\\n - A sub-bullet\\n- A second bullet"
}
}
}
};
export const Basic = () => <Button />;
Basic.parameters = {
...Basic.parameters,
storySource: {
source: "() => <Button />",
...Basic.parameters?.storySource
}
};
`);
});

it('correctly interleaves parameters', () => {
expect(
enrich(dedent`
/** The most basic button */
export default {
title: 'Button',
parameters: {
foo: 'bar',
docs: { inlineStories: true }
}
}
export const Basic = () => <Button />
`)
).toMatchInlineSnapshot(`
/** The most basic button */
export default {
title: 'Button',
parameters: {
foo: 'bar',
docs: {
inlineStories: true,
description: {
component: "The most basic button"
}
}
}
};
export const Basic = () => <Button />;
Basic.parameters = {
...Basic.parameters,
storySource: {
source: "() => <Button />",
...Basic.parameters?.storySource
}
};
`);
});

it('respects user component description', () => {
expect(
enrich(dedent`
/** The most basic button */
export default {
title: 'Button',
parameters: {
docs: {
description: {
component: 'hahaha'
}
}
}
}
export const Basic = () => <Button />
`)
).toMatchInlineSnapshot(`
/** The most basic button */
export default {
title: 'Button',
parameters: {
docs: {
description: {
component: 'hahaha'
}
}
}
};
export const Basic = () => <Button />;
Basic.parameters = {
...Basic.parameters,
storySource: {
source: "() => <Button />",
...Basic.parameters?.storySource
}
};
`);
});

it('respects meta variables', () => {
expect(
enrich(dedent`
/** The most basic button */
const meta = {
title: 'Button'
}
/** This should be ignored */
export default meta;
export const Basic = () => <Button />
`)
).toMatchInlineSnapshot(`
/** The most basic button */
const meta = {
title: 'Button',
parameters: {
docs: {
description: {
component: "The most basic button"
}
}
}
};
/** This should be ignored */
export default meta;
export const Basic = () => <Button />;
Basic.parameters = {
...Basic.parameters,
storySource: {
source: "() => <Button />",
...Basic.parameters?.storySource
}
};
`);
});
});

describe('options', () => {
it('disableSource', () => {
expect(
Expand Down
Loading