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

docs(storybook): fix code snippets for slot bindings #1458

Merged
merged 3 commits into from
Jul 1, 2024
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
5 changes: 5 additions & 0 deletions .changeset/curvy-mails-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sit-onyx/storybook-utils": patch
---

fix: generate correct code for slot bindings
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,10 @@ const meta: Meta<typeof OnyxAppLayout> = {
control: { disable: true },
},
},
// storybook adds 1rem padding. The app layout fills the full available space
// so we need to counteract the padding with a negative margin.
decorators: [
(story) => ({
components: { story },
template: `
<div style="margin: -1rem;" >
<story />
</div>`,
}),
],
}),
parameters: {
layout: "fullscreen",
},
};

export default meta;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ const meta: Meta<typeof OnyxPageLayout> = {
(story) => ({
components: { story },
template: `
<div style="margin: -1rem; height: 15rem;
<div style="height: 15rem;
font-family: var(--onyx-font-family);
color: var(--onyx-color-text-icons-neutral-intense);
border: 1px solid var(--onyx-color-text-icons-neutral-soft);" >
color: var(--onyx-color-text-icons-neutral-intense);" >
<story />
</div>`,
}),
],
parameters: {
layout: "fullscreen",
},
};

export default meta;
Expand Down
22 changes: 22 additions & 0 deletions packages/storybook-utils/src/source-code-generator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
//
// This file is only a temporary copy of the improved source code generation for Storybook.
// It is intended to be deleted once its officially released in Storybook itself, see:
Expand All @@ -9,6 +10,7 @@
generatePropsSourceCode,
generateSlotSourceCode,
generateSourceCode,
getFunctionParamNames,
parseDocgenInfo,
type SourceCodeGeneratorContext,
} from "./source-code-generator";
Expand Down Expand Up @@ -234,3 +236,23 @@
const docgenInfo = parseDocgenInfo({ __docgenInfo });
expect(docgenInfo.eventNames).toStrictEqual(eventNames);
});

// eslint-disable-next-line @typescript-eslint/no-explicit-any
test.each<{ fn: (...args: any[]) => unknown; expectedNames: string[] }>([
Dismissed Show dismissed Hide dismissed
{ fn: () => ({}), expectedNames: [] },
{ fn: (a) => ({}), expectedNames: ["a"] },
Dismissed Show dismissed Hide dismissed
larsrickert marked this conversation as resolved.
Show resolved Hide resolved
{ fn: (a, b) => ({}), expectedNames: ["a", "b"] },
Dismissed Show dismissed Hide dismissed
Dismissed Show dismissed Hide dismissed
{ fn: (a, b, { c }) => ({}), expectedNames: ["a", "b", "{", "c", "}"] },
Dismissed Show dismissed Hide dismissed
Dismissed Show dismissed Hide dismissed
Dismissed Show dismissed Hide dismissed
{ fn: ({ a, b }) => ({}), expectedNames: ["{", "a", "b", "}"] },
Dismissed Show dismissed Hide dismissed
Dismissed Show dismissed Hide dismissed
{
fn: {
// simulate minified function after running "storybook build"
toString: () => "({a:foo,b:bar})=>({})",
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as (...args: any[]) => unknown,
Dismissed Show dismissed Hide dismissed
expectedNames: ["{", "a", "b", "}"],
},
])("should extract function parameter names", ({ fn, expectedNames }) => {
const paramNames = getFunctionParamNames(fn);
expect(paramNames).toStrictEqual(expectedNames);
});
20 changes: 18 additions & 2 deletions packages/storybook-utils/src/source-code-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,13 +478,29 @@
* @see Based on https://stackoverflow.com/a/9924463
*/
// eslint-disable-next-line @typescript-eslint/ban-types
const getFunctionParamNames = (func: Function): string[] => {
export const getFunctionParamNames = (func: Function): string[] => {
Dismissed Show dismissed Hide dismissed
const STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/gm;
const ARGUMENT_NAMES = /([^\s,]+)/g;

const fnStr = func.toString().replace(STRIP_COMMENTS, "");
const result = fnStr.slice(fnStr.indexOf("(") + 1, fnStr.indexOf(")")).match(ARGUMENT_NAMES);
return result ?? [];
if (!result) return [];

// when running "storybook build", the function will be minified, so result for e.g.
// `({ foo, bar }) => { // function body }` will be `["{foo:e", "bar:a}"]`
// therefore we need to remove the :e and :a mappings and extract the "{" and "}"" from the destructured object
// so the final result becomes `["{", "foo", "bar", "}"]`
return result.flatMap((param) => {
if (["{", "}"].includes(param)) return param;
const nonMinifiedName = param.split(":")[0].trim();
if (nonMinifiedName.startsWith("{")) {
return ["{", nonMinifiedName.substring(1)];
}
if (param.endsWith("}") && !nonMinifiedName.endsWith("}")) {
return [nonMinifiedName, "}"];
}
return nonMinifiedName;
});
};

/**
Expand Down
Loading