mp-lens
Version:
微信小程序分析工具 (Unused Code, Dependencies, Visualization)
134 lines • 3.38 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DependencyGraph = void 0;
/**
* 依赖图数据结构
* 用于存储文件之间的依赖关系,支持节点和边的操作
*/
class DependencyGraph {
constructor() {
this._nodes = new Set();
this._outEdges = new Map();
this._inEdges = new Map();
}
/**
* 向图中添加节点
* @param node 节点名称(文件路径)
*/
addNode(node) {
this._nodes.add(node);
// 确保每个节点都有对应的边集合
if (!this._outEdges.has(node)) {
this._outEdges.set(node, new Set());
}
if (!this._inEdges.has(node)) {
this._inEdges.set(node, new Set());
}
}
/**
* 向图中添加边(依赖关系)
* @param from 源文件
* @param to 目标文件(被依赖)
*/
addEdge(from, to) {
// 确保两个节点都存在
this.addNode(from);
this.addNode(to);
// 添加边
this._outEdges.get(from).add(to);
this._inEdges.get(to).add(from);
}
/**
* 获取所有节点
*/
nodes() {
return [...this._nodes];
}
/**
* 获取节点的所有出边(依赖)
* @param node 节点名称
*/
outEdges(node) {
if (!this._outEdges.has(node)) {
return [];
}
return [...this._outEdges.get(node)];
}
/**
* 获取节点的所有入边(被谁依赖)
* @param node 节点名称
*/
inEdges(node) {
if (!this._inEdges.has(node)) {
return [];
}
return [...this._inEdges.get(node)];
}
/**
* 获取节点的出度(依赖了多少文件)
* @param node 节点名称
*/
outDegree(node) {
if (!this._outEdges.has(node)) {
return 0;
}
return this._outEdges.get(node).size;
}
/**
* 获取节点的入度(被多少文件依赖)
* @param node 节点名称
*/
inDegree(node) {
if (!this._inEdges.has(node)) {
return 0;
}
return this._inEdges.get(node).size;
}
/**
* 检查图中是否存在节点
* @param node 节点名称
*/
hasNode(node) {
return this._nodes.has(node);
}
/**
* 检查图中是否存在从from到to的边
* @param from 源节点
* @param to 目标节点
*/
hasEdge(from, to) {
if (!this._outEdges.has(from)) {
return false;
}
return this._outEdges.get(from).has(to);
}
/**
* 获取图中的节点数量
*/
get nodeCount() {
return this._nodes.size;
}
/**
* 获取图中的边数量
*/
get edgeCount() {
let count = 0;
for (const edges of this._outEdges.values()) {
count += edges.size;
}
return count;
}
/**
* 将图转换为JSON格式
*/
toJSON() {
const nodes = this.nodes();
const links = nodes.flatMap((from) => this.outEdges(from).map((to) => ({ source: from, target: to })));
return {
nodes: nodes.map((id) => ({ id })),
links,
};
}
}
exports.DependencyGraph = DependencyGraph;
//# sourceMappingURL=dependency-graph.js.map