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
40 changes: 40 additions & 0 deletions src/__tests__/markdownGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,46 @@ describe('MarkdownGenerator', () => {
expect(result).toContain('## src/utils.js');
expect(result).toContain('export const helper = () => {};');
});

test('循環依存がある場合、警告コメントが追加される', async () => {
mockConfig.get.mockImplementation((key: string, defaultValue?: any): any => {
if (key === 'includeDependencies') return true;
if (key === 'mermaid.maxNodes') return 300;
if (key === 'prefixText') return '';
if (key === 'enableCompression') return false;
return defaultValue;
});

mockGenerateYaml.mockImplementation((_directoriesInfo: DirectoryInfo[], _options: ScanOptions): string => {
const data = {
files: [
{ relativePath: 'test/a.ts', imports: ['./b.ts'] },
{ relativePath: 'test/b.ts', imports: ['./c.ts'] },
{ relativePath: 'test/c.ts', imports: ['./a.ts'] }
],
dependencies: {
'test/a.ts': ['test/b.ts'],
'test/b.ts': ['test/c.ts'],
'test/c.ts': ['test/a.ts']
}
};
return yaml.dump(data);
});

const dirWithCycle: DirectoryInfo = {
...mockDirectoryInfo,
files: [
{ ...mockFile, relativePath: 'test/a.ts', imports: ['./b.ts'] },
{ ...mockFile, relativePath: 'test/b.ts', imports: ['./c.ts'] },
{ ...mockFile, relativePath: 'test/c.ts', imports: ['./a.ts'] }
]
};

const result = await markdownGenerator.generate([dirWithCycle]);

expect(result).toContain('flowchart TD');
expect(result).toMatch(/%% Warning: Circular dependencies detected/);
});
});

test('ファイルサイズが適切にフォーマットされること', async () => {
Expand Down
44 changes: 44 additions & 0 deletions src/generators/MarkdownGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,36 @@ export class MarkdownGenerator implements IGenerator {
return `${formattedSize} ${units[unitIndex]}`;
}

private detectCycles(dependencies: { [key: string]: string[] }): string[][] {
const cycles: string[][] = [];
const visiting = new Set<string>();
const visited = new Set<string>();

const dfs = (node: string, path: string[]) => {
visiting.add(node);
for (const neighbor of dependencies[node] || []) {
if (visiting.has(neighbor)) {
const startIndex = path.indexOf(neighbor);
if (startIndex !== -1) {
cycles.push([...path.slice(startIndex), neighbor]);
}
} else if (!visited.has(neighbor)) {
dfs(neighbor, [...path, neighbor]);
}
}
visiting.delete(node);
visited.add(node);
};

for (const node of Object.keys(dependencies)) {
if (!visited.has(node)) {
dfs(node, [node]);
}
}

return cycles;
}

private async generateMermaidGraph(directories: DirectoryInfo[], config: vscode.WorkspaceConfiguration): Promise<string> {
// eslint-disable-next-line no-console
// console.log('generateMermaidGraph called with config for includeDependencies:', config.get('includeDependencies'));
Expand Down Expand Up @@ -192,6 +222,8 @@ export class MarkdownGenerator implements IGenerator {
edges.push({ from: escapedSource, to: escapedTarget });
}
}

const cycles = this.detectCycles(dependencies);
// eslint-disable-next-line no-console
// console.log(`Mermaid: Calculated ${nodes.size} nodes, ${edges.length} edges. Max nodes: ${maxNodes}`);

Expand All @@ -204,6 +236,18 @@ export class MarkdownGenerator implements IGenerator {
graphLines.push(` subgraph Warning [Warning: Mermaid graph truncated.]\n direction LR\n truncated_message["The number of nodes (${nodes.size}) exceeds the configured limit (${maxNodes})."]
end`);
}

if (cycles.length > 0) {
graphLines.push(' %% Warning: Circular dependencies detected');
const displayCycles = cycles.slice(0, 5);
for (const cycle of displayCycles) {
const cycleText = cycle.join(' -> ');
graphLines.push(` %% ${this.escapeMermaidLabel(cycleText)}`);
}
if (cycles.length > displayCycles.length) {
graphLines.push(` %% ...and ${cycles.length - displayCycles.length} more`);
}
}

for (const edge of edges) {
if (graphLines.length -1 >= maxNodes && truncated && !graphLines.some(line => line.includes('more dependencies linked here...'))) {
Expand Down