Skip to content
Closed
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
1 change: 1 addition & 0 deletions code/core/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export * from './utils/sync-main-preview-addons';
export * from './utils/setup-addon-in-config';
export * from './utils/wrap-getAbsolutePath-utils';
export * from './js-package-manager';
export * from './utils/preview-annotations';
export * from './utils/scan-and-transform-files';
export * from './utils/transform-imports';
export * from '../shared/utils/module';
Expand Down
27 changes: 27 additions & 0 deletions code/core/src/common/utils/preview-annotations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { fileURLToPath } from 'node:url';

import type { PresetProperty } from 'storybook/internal/types';

/**
* Creates a `previewAnnotations` preset function for a renderer package.
*
* @param packageName - The renderer package name (e.g. `'@storybook/vue3'`)
* @param extraEntries - Additional entry points to include before the docs entry (e.g. `['entry-preview-argtypes']`)
*/
export function createPreviewAnnotations(
packageName: string,
extraEntries: string[] = []
): PresetProperty<'previewAnnotations'> {
return async (input = [], options) => {
const docsEnabled = Object.keys(await options.presets.apply('docs', {}, options)).length > 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Add defensive nullish coalescing for presets.apply() result.

Per the codebase pattern shown in common-preset.ts and server-statics.ts, presets.apply() can return null/undefined if a preset's extension function returns a nullish value. Calling Object.keys() on null/undefined throws a TypeError.

Proposed fix
-    const docsEnabled = Object.keys(await options.presets.apply('docs', {}, options)).length > 0;
+    const docsEnabled =
+      Object.keys((await options.presets.apply('docs', {}, options)) ?? {}).length > 0;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const docsEnabled = Object.keys(await options.presets.apply('docs', {}, options)).length > 0;
const docsEnabled =
Object.keys((await options.presets.apply('docs', {}, options)) ?? {}).length > 0;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@code/core/src/common/utils/preview-annotations.ts` at line 16, The expression
computing docsEnabled calls Object.keys on the result of presets.apply('docs',
{}, options) which can be null/undefined; change the expression that assigns
docsEnabled so it treats the apply result as (presets.apply('docs', {}, options)
?? {}) before calling Object.keys (i.e., use nullish coalescing) to avoid
TypeError when a preset returns a nullish value; update the assignment where
docsEnabled is defined to apply this coalescing.


return (input as string[])
.concat([fileURLToPath(import.meta.resolve(`${packageName}/entry-preview`))])
.concat(
extraEntries.map((entry) => fileURLToPath(import.meta.resolve(`${packageName}/${entry}`)))
)
.concat(
docsEnabled ? [fileURLToPath(import.meta.resolve(`${packageName}/entry-preview-docs`))] : []
);
};
}
19 changes: 2 additions & 17 deletions code/renderers/html/src/preset.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
import { fileURLToPath } from 'node:url';
import { createPreviewAnnotations } from 'storybook/internal/common';

import type { PresetProperty } from 'storybook/internal/types';

export const previewAnnotations: PresetProperty<'previewAnnotations'> = async (
input = [],
options
) => {
const docsEnabled = Object.keys(await options.presets.apply('docs', {}, options)).length > 0;
const result: string[] = [];

return result
.concat(input)
.concat([fileURLToPath(import.meta.resolve('@storybook/html/entry-preview'))])
.concat(
docsEnabled ? [fileURLToPath(import.meta.resolve('@storybook/html/entry-preview-docs'))] : []
);
};
export const previewAnnotations = createPreviewAnnotations('@storybook/html');
21 changes: 2 additions & 19 deletions code/renderers/preact/src/preset.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
import { fileURLToPath } from 'node:url';
import { createPreviewAnnotations } from 'storybook/internal/common';

import type { PresetProperty } from 'storybook/internal/types';

export const previewAnnotations: PresetProperty<'previewAnnotations'> = async (
input = [],
options
) => {
const docsEnabled = Object.keys(await options.presets.apply('docs', {}, options)).length > 0;
const result: string[] = [];

return result
.concat(input)
.concat([fileURLToPath(import.meta.resolve('@storybook/preact/entry-preview'))])
.concat(
docsEnabled
? [fileURLToPath(import.meta.resolve('@storybook/preact/entry-preview-docs'))]
: []
);
};
export const previewAnnotations = createPreviewAnnotations('@storybook/preact');

/**
* Alias react and react-dom to preact/compat similar to the preact vite preset
Expand Down
21 changes: 2 additions & 19 deletions code/renderers/svelte/src/preset.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
import { fileURLToPath } from 'node:url';
import { createPreviewAnnotations } from 'storybook/internal/common';

import type { PresetProperty } from 'storybook/internal/types';

export const previewAnnotations: PresetProperty<'previewAnnotations'> = async (
input = [],
options
) => {
const docsEnabled = Object.keys(await options.presets.apply('docs', {}, options)).length > 0;
const result: string[] = [];

return result
.concat(input)
.concat([fileURLToPath(import.meta.resolve('@storybook/svelte/entry-preview'))])
.concat(
docsEnabled
? [fileURLToPath(import.meta.resolve('@storybook/svelte/entry-preview-docs'))]
: []
);
};
export const previewAnnotations = createPreviewAnnotations('@storybook/svelte');
19 changes: 2 additions & 17 deletions code/renderers/vue3/src/preset.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
import { fileURLToPath } from 'node:url';
import { createPreviewAnnotations } from 'storybook/internal/common';

import type { PresetProperty } from 'storybook/internal/types';

export const previewAnnotations: PresetProperty<'previewAnnotations'> = async (
input = [],
options
) => {
const docsEnabled = Object.keys(await options.presets.apply('docs', {}, options)).length > 0;
const result: string[] = [];

return result
.concat(input)
.concat([fileURLToPath(import.meta.resolve('@storybook/vue3/entry-preview'))])
.concat(
docsEnabled ? [fileURLToPath(import.meta.resolve('@storybook/vue3/entry-preview-docs'))] : []
);
};
export const previewAnnotations = createPreviewAnnotations('@storybook/vue3');
26 changes: 4 additions & 22 deletions code/renderers/web-components/src/preset.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
import { fileURLToPath } from 'node:url';
import { createPreviewAnnotations } from 'storybook/internal/common';

import type { PresetProperty } from 'storybook/internal/types';

export const previewAnnotations: PresetProperty<'previewAnnotations'> = async (
input = [],
options
) => {
const docsEnabled = Object.keys(await options.presets.apply('docs', {}, options)).length > 0;
const result: string[] = [];

return result
.concat(input)
.concat([
fileURLToPath(import.meta.resolve('@storybook/web-components/entry-preview')),
fileURLToPath(import.meta.resolve('@storybook/web-components/entry-preview-argtypes')),
])
.concat(
docsEnabled
? [fileURLToPath(import.meta.resolve('@storybook/web-components/entry-preview-docs'))]
: []
);
};
export const previewAnnotations = createPreviewAnnotations('@storybook/web-components', [
'entry-preview-argtypes',
]);
Loading