Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | 17x 16x 1x 15x 224x 224x 15x 113x 103x 24x 7x 24x 21x 24x 24x 24x 217x 119x 98x 98x 98x 98x 110x 116x 96x 2x 2x 2x 211x 3x 5x 1x 1x | /**
* DependencyGraph
*
* Manages file dependencies and provides topological sorting for correct
* processing order. Files are sorted so that dependencies are processed
* before dependents.
*
* Uses @n1ru4l/toposort for cycle-aware topological sorting.
*/
// Handle tsx vs vitest ESM/CJS interop differences:
// - tsx wraps named exports inside `default`
// - vitest exposes named exports at namespace level
import * as toposortNS from "@n1ru4l/toposort";
type ToposortFn = (deps: Map<string, string[]>) => Set<string>[];
type ToposortModule = {
toposortReverse?: ToposortFn;
default?: ToposortModule;
};
/**
* Resolves toposortReverse from module with tsx/vitest interop support.
*/
function resolveToposortReverse(mod: ToposortModule): ToposortFn {
if (mod.toposortReverse) {
return mod.toposortReverse;
}
return mod.default!.toposortReverse!;
}
const toposortReverse = resolveToposortReverse(toposortNS as ToposortModule);
/**
* Manages file dependencies for topological sorting
*/
class DependencyGraph {
/** Maps each file to its dependencies (files it includes) */
private readonly dependencies: Map<string, string[]> = new Map();
private readonly warnings: string[] = [];
/**
* Resolves toposortReverse with tsx/vitest interop support.
* Exposed as static for testing both code paths.
*/
static readonly resolveToposortReverse = resolveToposortReverse;
/**
* Add a file to the graph without dependencies
*/
addFile(path: string): void {
if (!this.dependencies.has(path)) {
this.dependencies.set(path, []);
}
}
/**
* Add a dependency relationship
* @param dependent - The file that depends on another (the includer)
* @param dependency - The file being depended on (the included file)
*/
addDependency(dependent: string, dependency: string): void {
// Ensure both nodes exist
if (!this.dependencies.has(dependent)) {
this.dependencies.set(dependent, []);
}
if (!this.dependencies.has(dependency)) {
this.dependencies.set(dependency, []);
}
// Add the dependency relationship
const deps = this.dependencies.get(dependent)!;
Eif (!deps.includes(dependency)) {
deps.push(dependency);
}
}
/**
* Get files in topological order (dependencies first)
*
* Uses toposortReverse which expects a map of [node -> dependencies].
* The result is batches of files that can be processed in parallel,
* but we flatten it to a single array.
*
* If a cycle is detected, returns nodes in arbitrary order with a warning.
*/
getSortedFiles(): string[] {
if (this.dependencies.size === 0) {
return [];
}
try {
// toposortReverse returns batches (Set[]) - flatten to array
const batches = toposortReverse(this.dependencies);
const result: string[] = [];
for (const batch of batches) {
for (const file of batch) {
result.push(file);
}
}
return result;
} catch (error) {
// Cycle detected - return nodes in arbitrary order with warning
const message = error instanceof Error ? error.message : "unknown error";
this.warnings.push(
`Warning: Circular dependency detected in include graph (${message}). Files may be processed in incorrect order.`,
);
return [...this.dependencies.keys()];
}
}
/**
* Get any warnings generated during sorting
*/
getWarnings(): string[] {
return [...this.warnings];
}
/**
* Check if the graph has any files
*/
isEmpty(): boolean {
return this.dependencies.size === 0;
}
/**
* Get the number of files in the graph
*/
size(): number {
return this.dependencies.size;
}
/**
* Clear the graph
*/
clear(): void {
this.dependencies.clear();
this.warnings.length = 0;
}
}
export default DependencyGraph;
|