Repository navigation
Angular: resolve story-docs snippets through the core/docgen service instead of a second analyzer #35843
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
Angular: resolve story-docs snippets through the core/docgen service instead of a second analyzer #35843
Changes from all commits
772acce
f6e54c6
abb8de5
a069ec2
ec12d5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,39 +1,41 @@ | ||
| import { STORY_FILE_TEST_REGEXP, getStoryImportPathFromEntry } from 'storybook/internal/common'; | ||
| import { getService } from 'storybook/internal/core-server'; | ||
| import { logger } from 'storybook/internal/node-logger'; | ||
| import type { StoryDocsProviderPreset } from 'storybook/internal/types'; | ||
|
|
||
| import { AngularComponentMetaManager } from '@storybook/angular-cm'; | ||
| import type { AngularDocgenPayload } from './build-docgen.ts'; | ||
| import { buildStoryDocsPayload } from './story-docs-build.ts'; | ||
|
|
||
| const createManager = async (): Promise<AngularComponentMetaManager | undefined> => { | ||
| // `core/docgen` is only registered when `experimentalDocgenServer` set up its worker (see | ||
| // `common-preset.ts`); both services are gated by the same feature, but registration order isn't | ||
| // a type-level guarantee, so this stays defensive rather than asserting the service exists. | ||
|
Comment on lines
+9
to
+11
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Remove the cross-file source reference. Keep the registration-order rationale. Remove the As per coding guidelines, comments should explain maintenance-relevant rationale, not cross-file line references. 🤖 Prompt for AI AgentsSource: Coding guidelines |
||
| let warnedMissingDocgenService = false; | ||
|
|
||
| const getDocgenPayload = async (componentId: string): Promise<AngularDocgenPayload | undefined> => { | ||
| try { | ||
| const typescript = await import('typescript'); | ||
| return new AngularComponentMetaManager(typescript.default ?? typescript); | ||
| const docgenService = getService('core/docgen', { internal: true }); | ||
| return await docgenService.queries.docgen.loaded({ id: componentId }); | ||
| } catch (error) { | ||
| logger.warn( | ||
| `Angular story snippets are unavailable: the component meta analyzer could not be created. ${ | ||
| error instanceof Error ? error.message : String(error) | ||
| }` | ||
| ); | ||
| if (!warnedMissingDocgenService) { | ||
| warnedMissingDocgenService = true; | ||
| logger.warn( | ||
| `Angular story snippets are unavailable: querying core/docgen failed. ${ | ||
| error instanceof Error ? error.message : String(error) | ||
| }` | ||
| ); | ||
| } | ||
| return undefined; | ||
| } | ||
| }; | ||
|
|
||
| export const experimental_storyDocsProvider: StoryDocsProviderPreset = async (nextStoryDocs) => { | ||
| // Scoped to the composed chain rather than the module, so the manager has one owner and one | ||
| // lifetime. | ||
| let managerPromise: Promise<AngularComponentMetaManager | undefined> | undefined; | ||
|
|
||
| return async (input) => { | ||
| const storyImportPath = getStoryImportPathFromEntry(input.entry); | ||
| if (!storyImportPath || !STORY_FILE_TEST_REGEXP.test(storyImportPath)) { | ||
| return nextStoryDocs(input); | ||
| } | ||
|
|
||
| const manager = await (managerPromise ??= createManager()); | ||
| const ours = buildStoryDocsPayload(input, { manager }); | ||
| // The language service holds large type caches; check heap pressure after each extraction. | ||
| manager?.recycleIfHeapPressured(); | ||
| const ours = await buildStoryDocsPayload(input, { getDocgenPayload }); | ||
|
|
||
| if (!ours) { | ||
| return nextStoryDocs(input); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Use the required Vitest spy setup for
getDocgenPayload.Declare the resolver spy before the test cases. Configure its behavior in
beforeEach. Usevi.mocked()to access the typed mock.code/frameworks/angular-vite/src/docgen/story-docs-build.test.ts#L86-L99: Configure the successful payload response inbeforeEach.code/frameworks/angular-vite/src/docgen/story-docs-build.test.ts#L114-L116: Configure the rejected query response inbeforeEach.code/lib/docgen-harness/src/angular/angular-story-docs-snippets.test.ts#L37-L63: Replace the direct resolver callback with the shared typed spy.code/lib/docgen-harness/src/angular/story-docs/build-story-docs.test.ts#L26-L52: Replace the direct resolver callback with the shared typed spy.As per coding guidelines, implement mock behaviors in
beforeEachblocks, usevi.mocked()to type and access mocked functions, and avoid direct function mocking withoutvi.mocked().📍 Affects 3 files
code/frameworks/angular-vite/src/docgen/story-docs-build.test.ts#L86-L99(this comment)code/frameworks/angular-vite/src/docgen/story-docs-build.test.ts#L114-L116code/lib/docgen-harness/src/angular/angular-story-docs-snippets.test.ts#L37-L63code/lib/docgen-harness/src/angular/story-docs/build-story-docs.test.ts#L26-L52🤖 Prompt for AI Agents
Source: Coding guidelines