mirror of
https://github.com/microsoft/vscode.git
synced 2026-02-25 20:24:04 +00:00
* Check for cyclic dependencies during compile Changes gulp-tsb to check the emitted JS code for cyclic dependencies. Historically we never cared about cycles between TS files as long as they dissappeared after compile (e.g type-dependencies, not runtime dependencies) https://github.com/microsoft/vscode-internalbacklog/issues/5271 * fix cycling dependencies fyi @aeschli @aiday-mar * remove cyclic dependency with unused `BasedTextEdit` fyi @hediet * remove cycle between chatEditService and chatEditingSession fyi @alexdima * remove cyclic dependency between chatSetup and chatViewPane fyi @roblourens * better cycle detection * don't check cycles when not needed * clear graph when reprocessing file dependencies * remove cycle between with `notebookChatEditController` fyi @DonJayamanne * modernize and cleanup tsb/utils
118 lines
3.4 KiB
TypeScript
118 lines
3.4 KiB
TypeScript
/*---------------------------------------------------------------------------------------------
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
|
*--------------------------------------------------------------------------------------------*/
|
|
|
|
export namespace strings {
|
|
|
|
export function format(value: string, ...rest: any[]): string {
|
|
return value.replace(/({\d+})/g, function (match) {
|
|
const index = Number(match.substring(1, match.length - 1));
|
|
return String(rest[index]) || match;
|
|
});
|
|
}
|
|
}
|
|
|
|
export namespace graph {
|
|
|
|
export class Node<T> {
|
|
|
|
readonly incoming = new Map<T, Node<T>>();
|
|
readonly outgoing = new Map<T, Node<T>>();
|
|
|
|
constructor(readonly data: T) {
|
|
|
|
}
|
|
}
|
|
|
|
export class Graph<T> {
|
|
|
|
private _nodes = new Map<T, Node<T>>();
|
|
|
|
inertEdge(from: T, to: T): void {
|
|
const fromNode = this.lookupOrInsertNode(from);
|
|
const toNode = this.lookupOrInsertNode(to);
|
|
|
|
fromNode.outgoing.set(toNode.data, toNode);
|
|
toNode.incoming.set(fromNode.data, fromNode);
|
|
}
|
|
|
|
resetNode(data: T): void {
|
|
const node = this._nodes.get(data);
|
|
if (!node) {
|
|
return;
|
|
}
|
|
for (const outDep of node.outgoing.values()) {
|
|
outDep.incoming.delete(node.data);
|
|
}
|
|
node.outgoing.clear();
|
|
}
|
|
|
|
lookupOrInsertNode(data: T): Node<T> {
|
|
let node = this._nodes.get(data);
|
|
|
|
if (!node) {
|
|
node = new Node(data);
|
|
this._nodes.set(data, node);
|
|
}
|
|
|
|
return node;
|
|
}
|
|
|
|
lookup(data: T): Node<T> | null {
|
|
return this._nodes.get(data) ?? null;
|
|
}
|
|
|
|
findCycle(): T[] | undefined {
|
|
|
|
let result: T[] | undefined;
|
|
let foundStartNodes = false;
|
|
const checked = new Set<Node<T>>();
|
|
|
|
for (const [_start, value] of this._nodes) {
|
|
|
|
if (Object.values(value.incoming).length > 0) {
|
|
continue;
|
|
}
|
|
|
|
foundStartNodes = true;
|
|
|
|
const dfs = (node: Node<T>, visited: Set<Node<T>>) => {
|
|
|
|
if (checked.has(node)) {
|
|
return;
|
|
}
|
|
|
|
if (visited.has(node)) {
|
|
result = [...visited, node].map(n => n.data);
|
|
const idx = result.indexOf(node.data);
|
|
result = result.slice(idx);
|
|
return;
|
|
}
|
|
visited.add(node);
|
|
for (const child of Object.values(node.outgoing)) {
|
|
dfs(child, visited);
|
|
if (result) {
|
|
break;
|
|
}
|
|
}
|
|
visited.delete(node);
|
|
checked.add(node);
|
|
};
|
|
dfs(value, new Set());
|
|
if (result) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!foundStartNodes) {
|
|
// everything is a cycle
|
|
return Array.from(this._nodes.keys());
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
|
|
}
|