Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import os from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { expect } from 'chai';
import { describe, it } from 'vitest';
import { afterEach, beforeEach, describe, it } from 'vitest';
import expectedStorybook from '../scripts/storybook-inventory.json';
import { computeRendererMatrixScope, findSlotOwningModules } from '../scripts/lib/renderer-matrix-scope.mjs';

Expand Down Expand Up @@ -197,3 +197,70 @@ describe('when deriving renderer matrix scope', () => {
}
});
});

describe('when deriving renderer scope through cycles and missing imports', () => {
let root: string;
let result: ReturnType<typeof computeRendererMatrixScope>;

beforeEach(() => {
const fixtureDirectory = path.join(repositoryRoot, '.ai-work/506-b6');
mkdirSync(fixtureDirectory, { recursive: true });
root = mkdtempSync(path.join(fixtureDirectory, 'renderer-scope-'));
const fixtureSourceRoot = path.join(root, 'Source');
const fixtures = {
'Slot.tsx': "unstable_useSlot('common.button');\n",
'CycleA.tsx': "import './CycleB';\n",
'CycleB.tsx': "import './CycleA';\nimport './Slot';\n",
'ClosedA.tsx': "import './ClosedB';\n",
'ClosedB.tsx': "import './ClosedA';\n",
'Composite.tsx': "import 'external-package';\nimport './Missing';\nimport './nested';\n",
'nested/index.ts': "export { Slot } from '../Slot';\n",
'dist/Hidden.tsx': "unstable_useSlot('common.button');\n",
'node_modules/Hidden.tsx': "unstable_useSlot('common.button');\n",
'storybook-static/Hidden.tsx': "unstable_useSlot('common.button');\n",
'for_example/Hidden.tsx': "unstable_useSlot('common.button');\n",
'Example.stories.tsx': "unstable_useSlot('common.button');\n",
'Example.stories.ts': "unstable_useSlot('common.button');\n",
'notes.md': "unstable_useSlot('common.button');\n",
};
for (const [name, contents] of Object.entries(fixtures)) {
const file = path.join(fixtureSourceRoot, name);
mkdirSync(path.dirname(file), { recursive: true });
writeFileSync(file, contents);
}
const storyEntries = [
storyEntry('cycle--first', 'CycleA.tsx'),
storyEntry('cycle--second', 'CycleB.tsx'),
storyEntry('closed--first', 'ClosedA.tsx'),
storyEntry('closed--second', 'ClosedB.tsx'),
storyEntry('composite--default', 'Composite.tsx'),
storyEntry('missing--default', 'Missing.tsx'),
storyEntry('cycle--repeated', 'CycleA.tsx'),
storyEntry('closed--repeated', 'ClosedA.tsx'),
];
result = computeRendererMatrixScope({ storyEntries, repositoryRoot: root, sourceRoot: fixtureSourceRoot });
});

afterEach(() => rmSync(root, { recursive: true, force: true }));

it('should include only production TypeScript slot owners', () => {
[...result.slotOwningModules].should.deep.equal([path.join(root, 'Source/Slot.tsx')]);
});

it('should resolve index modules and reachable cycles without crediting closed cycles or missing modules', () => {
[...result.matrixStoryIds].should.deep.equal([
'cycle--first', 'cycle--second', 'composite--default', 'cycle--repeated',
]);
});

it('should retain both positive and negative component membership for repeated stories', () => {
[...result.componentMatrixMembership].should.deep.equal([
['./Source/CycleA.tsx', true],
['./Source/CycleB.tsx', true],
['./Source/ClosedA.tsx', false],
['./Source/ClosedB.tsx', false],
['./Source/Composite.tsx', true],
['./Source/Missing.tsx', false],
]);
});
});
38 changes: 14 additions & 24 deletions Storybook/scripts/lib/renderer-matrix-scope.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@ const IGNORED_DIRECTORY_NAMES = new Set(['dist', 'node_modules', 'storybook-stat
const SOURCE_FILE_PATTERN = /\.tsx?$/u;
const SLOT_HOOK_PATTERN = /\bunstable_useSlot\s*\(/u;

const collectFiles = (directory, predicate, results = []) => {
for (const entry of readdirSync(directory, { withFileTypes: true })) {
if (IGNORED_DIRECTORY_NAMES.has(entry.name)) continue;
const entryPath = path.join(directory, entry.name);
if (entry.isDirectory()) {
collectFiles(entryPath, predicate, results);
continue;
}
if (entry.isFile() && predicate(entryPath)) results.push(entryPath);
}
return results;
const collectFiles = (directory, predicate) =>
readdirSync(directory, { withFileTypes: true })
.filter(entry => !IGNORED_DIRECTORY_NAMES.has(entry.name))
.flatMap(entry => collectEntryFiles(directory, entry, predicate));

const collectEntryFiles = (directory, entry, predicate) => {
const entryPath = path.join(directory, entry.name);
if (entry.isDirectory()) return collectFiles(entryPath, predicate);
return entry.isFile() && predicate(entryPath) ? [entryPath] : [];
};

const isProductionModule = filePath =>
Expand All @@ -39,12 +37,8 @@ const isProductionModule = filePath =>
*/
export const findSlotOwningModules = sourceRoot => {
const candidates = collectFiles(sourceRoot, isProductionModule);
const owning = new Set();
for (const filePath of candidates) {
const contents = readFileSync(filePath, 'utf8');
if (SLOT_HOOK_PATTERN.test(contents)) owning.add(filePath);
}
return owning;
return new Set(candidates.filter(filePath =>
SLOT_HOOK_PATTERN.test(readFileSync(filePath, 'utf8'))));
};

const isTypeOnlyDeclaration = statement =>
Expand Down Expand Up @@ -90,15 +84,11 @@ const moduleReachesSlot = (file, slotOwningModules, memo, pending = new Set()) =
}
if (pending.has(file) || !existsSync(file)) return false;
pending.add(file);
let reaches = false;
const source = readFileSync(file, 'utf8');
for (const specifier of extractSpecifiers(source, file)) {
const reaches = extractSpecifiers(source, file).some(specifier => {
const resolved = resolveRelativeModule(file, specifier);
if (resolved && moduleReachesSlot(resolved, slotOwningModules, memo, pending)) {
reaches = true;
break;
}
}
return resolved !== undefined && moduleReachesSlot(resolved, slotOwningModules, memo, pending);
});
pending.delete(file);
memo.set(file, reaches);
return reaches;
Expand Down
Loading
Loading