weavatrix 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +146 -0
- package/bin/weavatrix-mcp.mjs +6 -0
- package/package.json +50 -0
- package/skill/SKILL.md +56 -0
- package/src/analysis/coverage-reports.js +232 -0
- package/src/analysis/dead-check.js +136 -0
- package/src/analysis/dep-check.js +310 -0
- package/src/analysis/dep-rules.js +221 -0
- package/src/analysis/duplicates-worker.js +11 -0
- package/src/analysis/duplicates.compute.js +215 -0
- package/src/analysis/duplicates.js +15 -0
- package/src/analysis/duplicates.run.js +51 -0
- package/src/analysis/duplicates.tokenize.js +182 -0
- package/src/analysis/endpoints.js +156 -0
- package/src/analysis/findings.js +52 -0
- package/src/analysis/graph-analysis.aggregate.js +280 -0
- package/src/analysis/graph-analysis.js +7 -0
- package/src/analysis/graph-analysis.refs.js +146 -0
- package/src/analysis/graph-analysis.summaries.js +62 -0
- package/src/analysis/internal-audit.collect.js +117 -0
- package/src/analysis/internal-audit.js +9 -0
- package/src/analysis/internal-audit.reach.js +47 -0
- package/src/analysis/internal-audit.run.js +189 -0
- package/src/analysis/manifests.js +169 -0
- package/src/analysis/source-complexity.ast.js +97 -0
- package/src/analysis/source-complexity.constants.js +55 -0
- package/src/analysis/source-complexity.js +14 -0
- package/src/analysis/source-complexity.report.js +76 -0
- package/src/analysis/source-complexity.walk.js +174 -0
- package/src/build-graph.js +102 -0
- package/src/config.js +5 -0
- package/src/graph/build-worker.js +30 -0
- package/src/graph/builder/lang-csharp.js +40 -0
- package/src/graph/builder/lang-css.js +29 -0
- package/src/graph/builder/lang-go.js +53 -0
- package/src/graph/builder/lang-html.js +29 -0
- package/src/graph/builder/lang-java.js +29 -0
- package/src/graph/builder/lang-js.js +168 -0
- package/src/graph/builder/lang-python.js +78 -0
- package/src/graph/builder/lang-rust.js +45 -0
- package/src/graph/builder/spec-pkg.js +87 -0
- package/src/graph/graph-filter.js +44 -0
- package/src/graph/internal-builder.build.js +217 -0
- package/src/graph/internal-builder.js +12 -0
- package/src/graph/internal-builder.langs.js +86 -0
- package/src/graph/internal-builder.resolvers.js +116 -0
- package/src/graph/layout.js +35 -0
- package/src/infra/infra-items.js +255 -0
- package/src/infra/infra-registry.js +184 -0
- package/src/infra/infra.detect.js +119 -0
- package/src/infra/infra.js +38 -0
- package/src/infra/infra.match.js +102 -0
- package/src/infra/infra.scan.js +93 -0
- package/src/mcp/catalog.mjs +68 -0
- package/src/mcp/graph-context.mjs +276 -0
- package/src/mcp/tools-actions.mjs +132 -0
- package/src/mcp/tools-graph.mjs +274 -0
- package/src/mcp/tools-health.mjs +209 -0
- package/src/mcp/tools-impact.mjs +189 -0
- package/src/mcp-rg.mjs +51 -0
- package/src/mcp-server.mjs +171 -0
- package/src/mcp-source-tools.mjs +139 -0
- package/src/process.js +77 -0
- package/src/scan/discover.inventory.js +78 -0
- package/src/scan/discover.js +5 -0
- package/src/scan/discover.list.js +79 -0
- package/src/scan/discover.stack.js +227 -0
- package/src/scan/search.core.js +24 -0
- package/src/scan/search.git.js +102 -0
- package/src/scan/search.js +9 -0
- package/src/scan/search.node.js +83 -0
- package/src/scan/search.preview.js +49 -0
- package/src/scan/search.rg.js +145 -0
- package/src/security/advisory-store.js +177 -0
- package/src/security/installed.js +247 -0
- package/src/security/malware-file-heuristics.js +121 -0
- package/src/security/malware-heuristics.exclusions.js +134 -0
- package/src/security/malware-heuristics.js +15 -0
- package/src/security/malware-heuristics.roots.js +142 -0
- package/src/security/malware-heuristics.scan.js +87 -0
- package/src/security/malware-heuristics.sweep.js +122 -0
- package/src/security/malware-scoring.js +84 -0
- package/src/security/match.js +85 -0
- package/src/security/registry-sig.classify.js +136 -0
- package/src/security/registry-sig.js +18 -0
- package/src/security/registry-sig.rules.js +188 -0
- package/src/security/typosquat.js +72 -0
- package/src/util.js +27 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// internal-audit.js — façade over the internal analyzers: loads a repo's graph.json + package.json,
|
|
2
|
+
// runs dead-check (files) + computeUnusedExports + dep-check, and emits the unified findings envelope
|
|
3
|
+
// (DEPS_SECURITY_PLAN.md §2.2-2.3). ALL filesystem access lives here; the analyzers stay pure.
|
|
4
|
+
// P2 will add dep-rules (cycles/orphans/boundary); the security/ analyzers join in P4-P5.
|
|
5
|
+
// Split: fs collection helpers live in internal-audit.collect.js, reachability in
|
|
6
|
+
// internal-audit.reach.js, and the audit runner in internal-audit.run.js — this file
|
|
7
|
+
// re-exports the public surface so external import paths keep working unchanged.
|
|
8
|
+
export { collectSourceTexts } from "./internal-audit.collect.js";
|
|
9
|
+
export { runInternalAudit } from "./internal-audit.run.js";
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// internal-audit.reach.js — entry-point discovery + file-level reachability BFS for the internal
|
|
2
|
+
// audit. Split from internal-audit.js.
|
|
3
|
+
import { ENTRY_FILE } from "./dead-check.js";
|
|
4
|
+
import { TEST_FILE_RE } from "./internal-audit.collect.js";
|
|
5
|
+
|
|
6
|
+
const isFileNode = (n) => !String(n.id).includes("#");
|
|
7
|
+
|
|
8
|
+
// Entry set for reachability: conventional entry names + package.json main/module/browser/bin/exports +
|
|
9
|
+
// html pages (they root classic-script apps) + test files (the runner enters them) + root config files +
|
|
10
|
+
// dynamic-import targets. Anything reachable from here is "used"; the rest corroborates unused-file.
|
|
11
|
+
export function entryFiles(graph, pkg, dynamicTargets) {
|
|
12
|
+
const entries = new Set();
|
|
13
|
+
const pkgEntries = [];
|
|
14
|
+
for (const k of ["main", "module", "browser"]) if (typeof pkg[k] === "string") pkgEntries.push(pkg[k]);
|
|
15
|
+
if (pkg.bin) pkgEntries.push(...(typeof pkg.bin === "string" ? [pkg.bin] : Object.values(pkg.bin)));
|
|
16
|
+
(function walkExports(e) {
|
|
17
|
+
if (typeof e === "string") pkgEntries.push(e);
|
|
18
|
+
else if (e && typeof e === "object") Object.values(e).forEach(walkExports);
|
|
19
|
+
})(pkg.exports);
|
|
20
|
+
const pe = new Set(pkgEntries.map((p) => String(p).replace(/^\.\//, "").replace(/\\/g, "/")));
|
|
21
|
+
for (const n of graph.nodes || []) {
|
|
22
|
+
if (!isFileNode(n)) continue;
|
|
23
|
+
const f = n.source_file;
|
|
24
|
+
if (ENTRY_FILE.test(f) || TEST_FILE_RE.test(f) || /\.html?$/i.test(f) || pe.has(f) || /(^|\/)[^/]*\.config\.[a-z]+$/i.test(f)) entries.add(f);
|
|
25
|
+
}
|
|
26
|
+
for (const t of dynamicTargets) entries.add(t);
|
|
27
|
+
return entries;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// File-level BFS over every non-contains link (symbol endpoints collapse to their file via the id prefix).
|
|
31
|
+
export function computeReachability(graph, entries) {
|
|
32
|
+
const fileOf = (v) => { const s = String(v && typeof v === "object" ? v.id : v); const h = s.indexOf("#"); return h < 0 ? s : s.slice(0, h); };
|
|
33
|
+
const adj = new Map();
|
|
34
|
+
for (const l of graph.links || []) {
|
|
35
|
+
if (l.relation === "contains") continue;
|
|
36
|
+
const a = fileOf(l.source), b = fileOf(l.target);
|
|
37
|
+
if (!a || !b || a === b) continue;
|
|
38
|
+
(adj.get(a) || adj.set(a, new Set()).get(a)).add(b);
|
|
39
|
+
}
|
|
40
|
+
const reached = new Set(entries);
|
|
41
|
+
const queue = [...entries];
|
|
42
|
+
while (queue.length) {
|
|
43
|
+
const cur = queue.pop();
|
|
44
|
+
for (const nxt of adj.get(cur) || []) if (!reached.has(nxt)) { reached.add(nxt); queue.push(nxt); }
|
|
45
|
+
}
|
|
46
|
+
return reached;
|
|
47
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
// internal-audit.run.js — the audit runner: loads a repo's graph.json + package.json, runs
|
|
2
|
+
// dead-check (files) + computeUnusedExports + dep-check, and emits the unified findings envelope
|
|
3
|
+
// (DEPS_SECURITY_PLAN.md §2.2-2.3). Split from internal-audit.js.
|
|
4
|
+
import { existsSync } from "node:fs";
|
|
5
|
+
import { join, basename } from "node:path";
|
|
6
|
+
import { computeDead, computeUnusedExports } from "./dead-check.js";
|
|
7
|
+
import { computeDepFindings, computeGoDepFindings, computePyDepFindings } from "./dep-check.js";
|
|
8
|
+
import { parseGoMod } from "./manifests.js";
|
|
9
|
+
import { computeStructureFindings } from "./dep-rules.js";
|
|
10
|
+
import { makeFinding, summarizeFindings, sortFindings } from "./findings.js";
|
|
11
|
+
import { graphOutDirForRepo } from "../graph/layout.js";
|
|
12
|
+
import { collectInstalled } from "../security/installed.js";
|
|
13
|
+
import { loadStore, queryStore } from "../security/advisory-store.js";
|
|
14
|
+
import { matchAdvisories } from "../security/match.js";
|
|
15
|
+
import { scanMalware } from "../security/malware-heuristics.js";
|
|
16
|
+
import { classifyTyposquat } from "../security/typosquat.js";
|
|
17
|
+
import {
|
|
18
|
+
readText, readJson, collectSourceTexts, collectConfigTexts, workspacePkgNames,
|
|
19
|
+
collectPyManifest, TEST_FILE_RE,
|
|
20
|
+
} from "./internal-audit.collect.js";
|
|
21
|
+
import { entryFiles, computeReachability } from "./internal-audit.reach.js";
|
|
22
|
+
|
|
23
|
+
// Run the internal audit. graph is optional (loaded from the repo's central graph.json when absent);
|
|
24
|
+
// advisoryStorePath overrides the default ~/.weavatrix/advisories.json (tests use a scratch path).
|
|
25
|
+
// async because the malware sweep shells out to ripgrep.
|
|
26
|
+
export async function runInternalAudit(repoPath, { graph, advisoryStorePath, skipMalwareScan = false, malwareExclusions = {}, rgPath = "" } = {}) {
|
|
27
|
+
if (!existsSync(repoPath)) return { ok: false, error: "Repo path not found" };
|
|
28
|
+
if (!graph) {
|
|
29
|
+
graph = readJson(join(graphOutDirForRepo(repoPath), "graph.json"));
|
|
30
|
+
if (!graph) return { ok: false, error: "Build the graph first (no graph.json)" };
|
|
31
|
+
}
|
|
32
|
+
const pkg = readJson(join(repoPath, "package.json")) || {};
|
|
33
|
+
const externalImports = graph.externalImports || [];
|
|
34
|
+
const dynamicTargets = new Set(externalImports.filter((e) => e.dynamic && e.target).map((e) => e.target));
|
|
35
|
+
|
|
36
|
+
// Graphs can be stale or miss a helper file; text fallbacks must scan the real repo tree too.
|
|
37
|
+
const sources = collectSourceTexts(repoPath, graph);
|
|
38
|
+
|
|
39
|
+
const dead = computeDead(graph, sources);
|
|
40
|
+
const unusedExports = computeUnusedExports(graph, sources, { dynamicTargets });
|
|
41
|
+
const entries = entryFiles(graph, pkg, dynamicTargets);
|
|
42
|
+
const reachable = computeReachability(graph, entries);
|
|
43
|
+
const configTexts = collectConfigTexts(repoPath);
|
|
44
|
+
const dep = computeDepFindings({ externalImports, pkg, workspacePkgNames: workspacePkgNames(repoPath, pkg), configTexts });
|
|
45
|
+
// non-npm ecosystems: Go (go.mod) + Python (requirements/pyproject/Pipfile) — same findings shape
|
|
46
|
+
const goModText = readText(join(repoPath, "go.mod"));
|
|
47
|
+
const goDep = computeGoDepFindings({ externalImports, goMod: goModText != null ? parseGoMod(goModText) : null });
|
|
48
|
+
const pyDep = computePyDepFindings({ externalImports, pyManifest: collectPyManifest(repoPath), configTexts });
|
|
49
|
+
|
|
50
|
+
// structure: cycles / orphans / boundary rules. Rules come from the repo's optional .weavatrix-deps.json
|
|
51
|
+
// (the depcruise-config analogue); no bundled default rules — cycles+orphans are always on.
|
|
52
|
+
const rules = readJson(join(repoPath, ".weavatrix-deps.json")) || {};
|
|
53
|
+
const externalImportFiles = new Set(externalImports.filter((e) => e.pkg && !e.builtin).map((e) => e.file));
|
|
54
|
+
const structure = computeStructureFindings(graph, { rules, entrySet: entries, externalImportFiles });
|
|
55
|
+
|
|
56
|
+
const findings = [...dep.findings, ...goDep.findings, ...pyDep.findings];
|
|
57
|
+
// orphan ∩ dead-file → one finding: keep the stronger unused-file, drop the duplicate orphan
|
|
58
|
+
const deadFileSet = new Set(dead.deadFiles.map((f) => f.file));
|
|
59
|
+
for (const f of structure.findings) if (!(f.rule === "orphan-file" && deadFileSet.has(f.file))) findings.push(f);
|
|
60
|
+
for (const f of dead.deadFiles) {
|
|
61
|
+
if (dynamicTargets.has(f.file) || TEST_FILE_RE.test(f.file)) continue;
|
|
62
|
+
findings.push(makeFinding({
|
|
63
|
+
category: "unused",
|
|
64
|
+
rule: "unused-file",
|
|
65
|
+
severity: "low",
|
|
66
|
+
confidence: reachable.has(f.file) ? "medium" : "high", // unreachable from every entry = strong corroboration
|
|
67
|
+
title: `Unused file: ${f.file}`,
|
|
68
|
+
detail: `${f.reason}${reachable.has(f.file) ? "" : "; also unreachable from every entry point"}. Dynamic loading and framework conventions can't be fully ruled out — review before deleting.`,
|
|
69
|
+
file: f.file,
|
|
70
|
+
graphNodeId: f.file,
|
|
71
|
+
source: "internal",
|
|
72
|
+
fixHint: "review, then delete the file",
|
|
73
|
+
}));
|
|
74
|
+
}
|
|
75
|
+
for (const s of unusedExports) {
|
|
76
|
+
if (s.test) continue; // exports from test files are runner-visible noise
|
|
77
|
+
if (/(^|\/)[^/]*\.config\.[a-z0-9]+$|(^|\/)\.[^/]+rc(\.[a-z]+)?$/i.test(s.file)) continue; // config exports are consumed by their tool
|
|
78
|
+
findings.push(makeFinding({
|
|
79
|
+
category: "unused",
|
|
80
|
+
rule: "unused-export",
|
|
81
|
+
severity: "info",
|
|
82
|
+
confidence: "medium",
|
|
83
|
+
title: `Unused export: ${s.label.replace(/\(\)$/, "")} — ${s.file}`,
|
|
84
|
+
detail: `${s.reason}. Either remove the export keyword (if used only internally) or delete the symbol.`,
|
|
85
|
+
file: s.file,
|
|
86
|
+
symbol: s.label,
|
|
87
|
+
graphNodeId: s.id,
|
|
88
|
+
source: "internal",
|
|
89
|
+
}));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// ---- supply-chain: installed packages × cached OSV advisories. 100% OFFLINE here — the cache is
|
|
93
|
+
// refreshed only by the explicit repos:advisory-refresh action. Never blocks the rest of the audit.
|
|
94
|
+
let advisoryDbDate = null;
|
|
95
|
+
let installedCount = 0;
|
|
96
|
+
let inst = { installed: [], drift: [] };
|
|
97
|
+
try {
|
|
98
|
+
inst = collectInstalled(repoPath);
|
|
99
|
+
installedCount = inst.installed.length;
|
|
100
|
+
const store = advisoryStorePath ? loadStore(advisoryStorePath) : loadStore();
|
|
101
|
+
// per-repo date when the store tracks it (cache only covers QUERIED packages); legacy stores
|
|
102
|
+
// without the repos map keep the old global-date behavior
|
|
103
|
+
advisoryDbDate = store.meta?.repos ? store.meta.repos[repoPath] || null : store.meta?.fetched_at || null;
|
|
104
|
+
if (advisoryDbDate) {
|
|
105
|
+
for (const h of matchAdvisories(inst.installed, (eco, name) => queryStore(store, eco, name))) {
|
|
106
|
+
const mal = h.adv.kind === "malicious";
|
|
107
|
+
findings.push(makeFinding({
|
|
108
|
+
category: mal ? "malware" : "vulnerability",
|
|
109
|
+
rule: mal ? "malicious-package" : "known-vuln",
|
|
110
|
+
severity: mal ? "critical" : h.adv.severity,
|
|
111
|
+
confidence: h.confidence,
|
|
112
|
+
title: `${mal ? "Known-malicious package" : `Known vulnerability (${h.adv.id})`}: ${h.pkg.name}@${h.pkg.version}`,
|
|
113
|
+
detail: `${h.adv.summary || h.adv.id}${h.adv.fixedIn.length ? ` Fixed in: ${h.adv.fixedIn.join(", ")}.` : mal ? " Remove this package immediately and rotate any secrets it could reach." : ""} (matched by ${h.matchedBy}${h.adv.aliases.length ? `; aliases ${h.adv.aliases.join(", ")}` : ""})`,
|
|
114
|
+
package: h.pkg.name,
|
|
115
|
+
version: h.pkg.version,
|
|
116
|
+
evidence: [{ file: h.adv.url, line: 0, snippet: `installed via ${h.pkg.source}${h.pkg.dev ? " (dev)" : ""}` }],
|
|
117
|
+
source: "osv",
|
|
118
|
+
fixHint: mal ? `npm uninstall ${h.pkg.name} + audit what it touched` : h.adv.fixedIn.length ? `upgrade ${h.pkg.name} to ${h.adv.fixedIn[h.adv.fixedIn.length - 1]}+` : "no fixed version published — consider replacing the package",
|
|
119
|
+
}));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
// direct-dependency typosquat (dev-chosen names, small set → low FP): surface quietly even alone.
|
|
123
|
+
for (const name of Object.keys({ ...(pkg.dependencies || {}), ...(pkg.devDependencies || {}) })) {
|
|
124
|
+
const sq = classifyTyposquat(name);
|
|
125
|
+
if (!sq) continue;
|
|
126
|
+
findings.push(makeFinding({
|
|
127
|
+
category: "malware",
|
|
128
|
+
rule: "typosquat",
|
|
129
|
+
severity: "medium",
|
|
130
|
+
confidence: "low",
|
|
131
|
+
title: `Possible typosquat: ${name} (looks like "${sq.nearest}")`,
|
|
132
|
+
detail: `Direct dependency "${name}" is edit-distance ${sq.distance} from the popular package "${sq.nearest}". Confirm you meant "${name}" and not "${sq.nearest}" — name-confusion is a common supply-chain lure.`,
|
|
133
|
+
package: name,
|
|
134
|
+
source: "internal",
|
|
135
|
+
fixHint: `verify "${name}" is the intended package (not a typo of "${sq.nearest}")`,
|
|
136
|
+
}));
|
|
137
|
+
}
|
|
138
|
+
for (const d of inst.drift.slice(0, 20)) {
|
|
139
|
+
findings.push(makeFinding({
|
|
140
|
+
category: "malware",
|
|
141
|
+
rule: "lockfile-drift",
|
|
142
|
+
severity: "low",
|
|
143
|
+
confidence: "medium",
|
|
144
|
+
title: `Lockfile drift: ${d.name} (locked ${d.locked}, installed ${d.installed})`,
|
|
145
|
+
detail: "The version on disk differs from the lockfile — a stale install, a manual edit, or (worst case) tampering. Reinstall from the lockfile to realign.",
|
|
146
|
+
package: d.name,
|
|
147
|
+
version: d.installed,
|
|
148
|
+
source: "internal",
|
|
149
|
+
fixHint: "npm ci (clean install from the lockfile)",
|
|
150
|
+
}));
|
|
151
|
+
}
|
|
152
|
+
} catch { /* supply-chain layer is best-effort */ }
|
|
153
|
+
|
|
154
|
+
// ---- malware heuristics: install-script beacons / miners / exfil / obfuscation across installed libs.
|
|
155
|
+
// Local + offline (ripgrep or a bounded Node fallback). Scans node_modules, Python venvs, Go vendor/cache.
|
|
156
|
+
let malwareScan = null;
|
|
157
|
+
if (!skipMalwareScan) {
|
|
158
|
+
try {
|
|
159
|
+
const importedPkgs = new Set(externalImports.filter((e) => e.pkg && !e.builtin).map((e) => e.pkg));
|
|
160
|
+
const scan = await scanMalware(repoPath, { installed: inst.installed, importedPkgs, malwareExclusions, rgPath });
|
|
161
|
+
findings.push(...scan.findings);
|
|
162
|
+
malwareScan = { scanMode: scan.scanMode, packagesScanned: scan.packagesScanned, findings: scan.findings.length, excludedSignals: scan.excludedSignals || 0 };
|
|
163
|
+
} catch { /* heuristic scan is best-effort */ }
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const sorted = sortFindings(findings);
|
|
167
|
+
return {
|
|
168
|
+
ok: true,
|
|
169
|
+
engine: "internal",
|
|
170
|
+
repo: basename(repoPath),
|
|
171
|
+
path: repoPath,
|
|
172
|
+
savedAt: new Date().toISOString(),
|
|
173
|
+
scanned: {
|
|
174
|
+
files: dead.stats.files,
|
|
175
|
+
symbols: dead.stats.symbols,
|
|
176
|
+
manifestDeps: dep.declared.size + goDep.declared.size + pyDep.declared.size,
|
|
177
|
+
externalImports: externalImports.length,
|
|
178
|
+
nodeModulesPresent: existsSync(join(repoPath, "node_modules")),
|
|
179
|
+
installedPackages: installedCount,
|
|
180
|
+
advisoryDbDate,
|
|
181
|
+
malwareScanMode: malwareScan?.scanMode || "skipped",
|
|
182
|
+
},
|
|
183
|
+
summary: summarizeFindings(sorted),
|
|
184
|
+
findings: sorted,
|
|
185
|
+
deadReport: { deadSymbols: dead.deadSymbols.length, deadFiles: dead.deadFiles.length, unusedExports: unusedExports.length },
|
|
186
|
+
structureReport: structure.stats,
|
|
187
|
+
malwareScan,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
// Pure manifest parsers for the non-npm ecosystems (Go + Python) — text in, declared deps out.
|
|
2
|
+
// NO filesystem here (internal-audit.js is the fs wrapper; internal-builder reads go.mod for resolvers).
|
|
3
|
+
// Philosophy matches dep-check.js: tolerate real-world files, bias to FALSE-NEGATIVES on weird syntax.
|
|
4
|
+
|
|
5
|
+
// pep503(name) — PyPI canonical form: lowercase, runs of -_. collapse to "-" (same as security/installed.js).
|
|
6
|
+
export const pep503 = (name) => String(name || "").toLowerCase().replace(/[-_.]+/g, "-");
|
|
7
|
+
|
|
8
|
+
// ---- go.mod → { module, requires: [{path, version, indirect}], replaces: [{from, to}] } ----
|
|
9
|
+
// Handles single-line `require path v1` and `require ( … )` blocks; `// indirect` marks transitive
|
|
10
|
+
// requires (never flagged unused — Go owns them via `go mod tidy`).
|
|
11
|
+
export function parseGoMod(text) {
|
|
12
|
+
const src = String(text || "");
|
|
13
|
+
const out = { module: "", requires: [], replaces: [] };
|
|
14
|
+
const m = src.match(/^\s*module\s+(\S+)/m);
|
|
15
|
+
if (m) out.module = m[1].replace(/^"|"$/g, "");
|
|
16
|
+
const addReq = (line) => {
|
|
17
|
+
const r = line.match(/^\s*([^\s()=>]+)\s+(v[\w.+-]+)\s*(\/\/.*)?$/);
|
|
18
|
+
if (r) out.requires.push({ path: r[1].replace(/^"|"$/g, ""), version: r[2], indirect: /\/\/\s*indirect/.test(line) });
|
|
19
|
+
};
|
|
20
|
+
const addRepl = (line) => {
|
|
21
|
+
const r = line.match(/^\s*([^\s()=>]+)(?:\s+v[\w.+-]+)?\s*=>\s*(\S+)(?:\s+v[\w.+-]+)?\s*(\/\/.*)?$/);
|
|
22
|
+
if (r) out.replaces.push({ from: r[1].replace(/^"|"$/g, ""), to: r[2].replace(/^"|"$/g, "") });
|
|
23
|
+
};
|
|
24
|
+
for (const dir of ["require", "replace"]) {
|
|
25
|
+
const add = dir === "require" ? addReq : addRepl;
|
|
26
|
+
for (const blk of src.matchAll(new RegExp(`^\\s*${dir}\\s*\\(([\\s\\S]*?)^\\s*\\)`, "gm"))) for (const line of blk[1].split(/\r?\n/)) add(line);
|
|
27
|
+
for (const one of src.matchAll(new RegExp(`^\\s*${dir}\\s+([^(\\r\\n]+)$`, "gm"))) add(one[1]);
|
|
28
|
+
}
|
|
29
|
+
return out;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// ---- PEP 508 requirement line → dist name (extras/specifiers/markers stripped), or null ----
|
|
33
|
+
// "-r other.txt", bare options, paths and URLs are skipped; VCS urls keep their #egg= name if present.
|
|
34
|
+
export function requirementName(rawLine) {
|
|
35
|
+
const line = String(rawLine || "").replace(/(^|\s)#.*$/, "").trim();
|
|
36
|
+
if (!line) return null;
|
|
37
|
+
const egg = line.match(/#egg=([A-Za-z0-9][\w.-]*)/i);
|
|
38
|
+
if (egg) return egg[1];
|
|
39
|
+
if (line.startsWith("-") || /^(git\+|hg\+|svn\+|bzr\+|https?:|file:)/i.test(line) || /^\.{0,2}[\\/]/.test(line) || line === ".") return null;
|
|
40
|
+
const m = line.match(/^([A-Za-z0-9][A-Za-z0-9._-]*)/);
|
|
41
|
+
return m ? m[1] : null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// requirements.txt (or *.in) text → [{name}] (unique by canonical name)
|
|
45
|
+
export function parseRequirementsNames(text) {
|
|
46
|
+
const seen = new Set();
|
|
47
|
+
const out = [];
|
|
48
|
+
for (const raw of String(text || "").split(/\r?\n/)) {
|
|
49
|
+
const name = requirementName(raw);
|
|
50
|
+
if (!name || seen.has(pep503(name))) continue;
|
|
51
|
+
seen.add(pep503(name));
|
|
52
|
+
out.push({ name });
|
|
53
|
+
}
|
|
54
|
+
return out;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// ---- pyproject.toml → { present, deps: [{name, dev, buildSystem}] } ----
|
|
58
|
+
// Line-based section scanner (no TOML dependency): covers PEP 621 [project] dependencies /
|
|
59
|
+
// optional-dependencies, Poetry [tool.poetry.*dependencies] tables, PEP 735 [dependency-groups],
|
|
60
|
+
// and [build-system] requires (declared-but-implicit → suppresses "missing", never checked "unused").
|
|
61
|
+
export function parsePyprojectDeps(text) {
|
|
62
|
+
const src = String(text || "");
|
|
63
|
+
const deps = [];
|
|
64
|
+
const seen = new Set();
|
|
65
|
+
const add = (name, dev, buildSystem = false) => {
|
|
66
|
+
if (!name || name.toLowerCase() === "python" || seen.has(pep503(name))) return;
|
|
67
|
+
seen.add(pep503(name));
|
|
68
|
+
deps.push({ name, dev: !!dev, buildSystem });
|
|
69
|
+
};
|
|
70
|
+
let section = "";
|
|
71
|
+
let present = false;
|
|
72
|
+
let arr = null; // { dev, buildSystem } while inside a dependencies = [ … ] array
|
|
73
|
+
// bracket balance OUTSIDE string literals — "requests[security]" must not close the array
|
|
74
|
+
const bracketDelta = (s) => { const t = String(s).replace(/"[^"]*"|'[^']*'/g, ""); return ((t.match(/\[/g) || []).length) - ((t.match(/\]/g) || []).length); };
|
|
75
|
+
for (const raw of src.split(/\r?\n/)) {
|
|
76
|
+
const line = raw.replace(/(^|\s)#.*$/, "").trimEnd();
|
|
77
|
+
const sec = line.match(/^\s*\[+([^\]]+)\]+\s*$/);
|
|
78
|
+
if (!arr && sec) { section = sec[1].trim(); continue; }
|
|
79
|
+
if (arr) { // inside a multi-line array: pull every string literal
|
|
80
|
+
for (const s of line.matchAll(/["']([^"']+)["']/g)) add(requirementName(s[1]), arr.dev, arr.buildSystem);
|
|
81
|
+
if (bracketDelta(line) < 0) arr = null;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
const kv = line.match(/^\s*([\w."'-]+)\s*=\s*(.*)$/);
|
|
85
|
+
if (!kv) continue;
|
|
86
|
+
const key = kv[1].replace(/^["']|["']$/g, "");
|
|
87
|
+
const val = kv[2].trim();
|
|
88
|
+
const startArray = (dev, buildSystem = false) => {
|
|
89
|
+
present = true;
|
|
90
|
+
for (const s of val.matchAll(/["']([^"']+)["']/g)) add(requirementName(s[1]), dev, buildSystem);
|
|
91
|
+
if (bracketDelta(val) > 0) arr = { dev, buildSystem };
|
|
92
|
+
};
|
|
93
|
+
if (section === "project" && key === "dependencies") startArray(false);
|
|
94
|
+
else if (section === "project.optional-dependencies" || section === "dependency-groups") startArray(true);
|
|
95
|
+
else if (section === "build-system" && key === "requires") startArray(true, true);
|
|
96
|
+
else if (section === "tool.poetry.dependencies") { present = true; add(key, false); }
|
|
97
|
+
else if (section === "tool.poetry.dev-dependencies" || /^tool\.poetry\.group\.[^.]+\.dependencies$/.test(section)) { present = true; add(key, true); }
|
|
98
|
+
}
|
|
99
|
+
return { present, deps };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// ---- poetry.lock → [{name, version, deps}] — [[package]] blocks + their [package.dependencies] keys ----
|
|
103
|
+
export function parsePoetryLockDeps(text) {
|
|
104
|
+
const out = [];
|
|
105
|
+
let cur = null, inDeps = false;
|
|
106
|
+
for (const raw of String(text || "").split(/\r?\n/)) {
|
|
107
|
+
const line = raw.trim();
|
|
108
|
+
if (line === "[[package]]") { cur = { name: "", version: "", deps: [] }; out.push(cur); inDeps = false; continue; }
|
|
109
|
+
if (line.startsWith("[")) { inDeps = cur != null && line === "[package.dependencies]"; if (line.startsWith("[[")) cur = null; continue; }
|
|
110
|
+
if (!cur) continue;
|
|
111
|
+
if (inDeps) { const m = line.match(/^["']?([A-Za-z0-9][\w.-]*)["']?\s*=/); if (m) cur.deps.push(m[1]); continue; }
|
|
112
|
+
let m = line.match(/^name\s*=\s*"([^"]+)"/); if (m && !cur.name) { cur.name = m[1]; continue; }
|
|
113
|
+
m = line.match(/^version\s*=\s*"([^"]+)"/); if (m && !cur.version) cur.version = m[1];
|
|
114
|
+
}
|
|
115
|
+
return out.filter((p) => p.name);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// ---- uv.lock → [{name, version, deps}] — [[package]] blocks with dependencies = [{ name = "x" }, …] ----
|
|
119
|
+
export function parseUvLockDeps(text) {
|
|
120
|
+
const out = [];
|
|
121
|
+
let cur = null, inArr = false;
|
|
122
|
+
for (const raw of String(text || "").split(/\r?\n/)) {
|
|
123
|
+
const line = raw.trim();
|
|
124
|
+
if (line === "[[package]]") { cur = { name: "", version: "", deps: [] }; out.push(cur); inArr = false; continue; }
|
|
125
|
+
if (line.startsWith("[") && !inArr) { if (line.startsWith("[[")) cur = null; continue; }
|
|
126
|
+
if (!cur) continue;
|
|
127
|
+
if (inArr) { for (const m of line.matchAll(/name\s*=\s*"([^"]+)"/g)) cur.deps.push(m[1]); if (/\]\s*$/.test(line)) inArr = false; continue; }
|
|
128
|
+
if (/^dependencies\s*=\s*\[/.test(line)) { for (const m of line.matchAll(/name\s*=\s*"([^"]+)"/g)) cur.deps.push(m[1]); inArr = !/\]\s*$/.test(line); continue; }
|
|
129
|
+
let m = line.match(/^name\s*=\s*"([^"]+)"/); if (m && !cur.name) { cur.name = m[1]; continue; }
|
|
130
|
+
m = line.match(/^version\s*=\s*"([^"]+)"/); if (m && !cur.version) cur.version = m[1];
|
|
131
|
+
}
|
|
132
|
+
return out.filter((p) => p.name);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// ---- installed dist-info METADATA → { name, version, deps, repository } (headers until the first blank
|
|
136
|
+
// line; Requires-Dist entries gated behind `extra ==` markers are optional extras — skipped) ----
|
|
137
|
+
export function parseDistMetadata(text) {
|
|
138
|
+
const out = { name: "", version: "", deps: [], repository: "" };
|
|
139
|
+
for (const raw of String(text || "").split(/\r?\n/)) {
|
|
140
|
+
if (raw.trim() === "") break;
|
|
141
|
+
let m = raw.match(/^Name:\s*(.+)$/i); if (m) { out.name = m[1].trim(); continue; }
|
|
142
|
+
m = raw.match(/^Version:\s*(.+)$/i); if (m) { out.version = m[1].trim(); continue; }
|
|
143
|
+
m = raw.match(/^Requires-Dist:\s*([A-Za-z0-9][\w.-]*)(.*)$/i);
|
|
144
|
+
if (m) { if (!/extra\s*==/.test(m[2])) out.deps.push(m[1]); continue; }
|
|
145
|
+
m = raw.match(/^(?:Home-page:\s*|Project-URL:\s*(?:Source|Repository|Homepage|Home|Code)[^,]*,\s*)(https?:\/\/\S+)/i);
|
|
146
|
+
if (m && !out.repository) out.repository = m[1];
|
|
147
|
+
}
|
|
148
|
+
return out;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// ---- Pipfile → { present, deps: [{name, dev}] } — [packages] / [dev-packages] table keys ----
|
|
152
|
+
export function parsePipfileDeps(text) {
|
|
153
|
+
const deps = [];
|
|
154
|
+
const seen = new Set();
|
|
155
|
+
let section = "";
|
|
156
|
+
let present = false;
|
|
157
|
+
for (const raw of String(text || "").split(/\r?\n/)) {
|
|
158
|
+
const line = raw.replace(/(^|\s)#.*$/, "").trim();
|
|
159
|
+
const sec = line.match(/^\[([^\]]+)\]$/);
|
|
160
|
+
if (sec) { section = sec[1].trim().toLowerCase(); continue; }
|
|
161
|
+
if (section !== "packages" && section !== "dev-packages") continue;
|
|
162
|
+
const kv = line.match(/^["']?([A-Za-z0-9][\w.-]*)["']?\s*=/);
|
|
163
|
+
if (!kv || seen.has(pep503(kv[1]))) continue;
|
|
164
|
+
present = true;
|
|
165
|
+
seen.add(pep503(kv[1]));
|
|
166
|
+
deps.push({ name: kv[1], dev: section === "dev-packages" });
|
|
167
|
+
}
|
|
168
|
+
return { present, deps };
|
|
169
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// Tree-sitter node helpers shared by the source-complexity analysis modules.
|
|
2
|
+
|
|
3
|
+
import { IO_PREFIXES } from "./source-complexity.constants.js";
|
|
4
|
+
|
|
5
|
+
export function field(node, name) {
|
|
6
|
+
try { return node?.childForFieldName ? node.childForFieldName(name) : null; }
|
|
7
|
+
catch { return null; }
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function children(node) {
|
|
11
|
+
try { return Array.isArray(node?.namedChildren) ? node.namedChildren : []; }
|
|
12
|
+
catch { return []; }
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function allChildren(node) {
|
|
16
|
+
try { return Array.isArray(node?.children) ? node.children : children(node); }
|
|
17
|
+
catch { return children(node); }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function sameNode(left, right) {
|
|
21
|
+
if (!left || !right) return false;
|
|
22
|
+
if (left === right) return true;
|
|
23
|
+
if (left.id != null && right.id != null) return left.id === right.id;
|
|
24
|
+
return left.type === right.type && left.startIndex === right.startIndex && left.endIndex === right.endIndex;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function normalizedName(value) {
|
|
28
|
+
return String(value || "").replace(/[^A-Za-z0-9_$]+/g, "").toLowerCase();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function looksLikeIoCall(value) {
|
|
32
|
+
const raw = String(value || "");
|
|
33
|
+
const lower = raw.toLowerCase();
|
|
34
|
+
return IO_PREFIXES.some((prefix) => {
|
|
35
|
+
if (!lower.startsWith(prefix)) return false;
|
|
36
|
+
const boundary = raw[prefix.length];
|
|
37
|
+
return boundary == null || /[A-Z_$]/.test(boundary);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function callName(node) {
|
|
42
|
+
const callee = field(node, "function") || field(node, "name") || field(node, "constructor");
|
|
43
|
+
if (!callee) return "";
|
|
44
|
+
const member = field(callee, "property") || field(callee, "field") || field(callee, "attribute") || field(callee, "name");
|
|
45
|
+
return String((member || callee).text || "").replace(/\?$/, "");
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function logicalBranch(node) {
|
|
49
|
+
if (!/binary_expression|boolean_operator/.test(String(node?.type || ""))) return false;
|
|
50
|
+
return allChildren(node).some((child) => ["&&", "||", "??", "and", "or"].includes(String(child?.type || child?.text || "")));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function isDefaultCase(node) {
|
|
54
|
+
const text = String(node?.text || "").trimStart();
|
|
55
|
+
return /^(default\b|case\s+default\b)/i.test(text);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function directParameterCount(paramNode, family) {
|
|
59
|
+
if (!paramNode) return 0;
|
|
60
|
+
if (!/parameters|parameter_list|formal_parameters/.test(String(paramNode.type || ""))) {
|
|
61
|
+
return /^(self|cls)$/.test(String(paramNode.text || "").trim()) ? 0 : 1;
|
|
62
|
+
}
|
|
63
|
+
let count = 0;
|
|
64
|
+
for (const part of children(paramNode)) {
|
|
65
|
+
const type = String(part.type || "");
|
|
66
|
+
if (/comment|type_parameter|type_parameters/.test(type)) continue;
|
|
67
|
+
if (family === "go" && /parameter_declaration|variadic_parameter_declaration/.test(type)) {
|
|
68
|
+
const ids = children(part).filter((child) => /^(identifier|field_identifier)$/.test(String(child.type || "")));
|
|
69
|
+
count += Math.max(1, ids.length);
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (/^(self|cls)(\s*[:=].*)?$/.test(String(part.text || "").trim())) continue;
|
|
73
|
+
count++;
|
|
74
|
+
}
|
|
75
|
+
return count;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function countObjectPatternFields(paramNode) {
|
|
79
|
+
if (!paramNode) return 0;
|
|
80
|
+
let total = 0;
|
|
81
|
+
const visit = (node, depth) => {
|
|
82
|
+
if (!node || depth > 5) return;
|
|
83
|
+
if (node.type === "object_pattern") {
|
|
84
|
+
total += children(node).filter((child) => !/type_annotation|comment/.test(String(child.type || ""))).length;
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
for (const child of children(node)) visit(child, depth + 1);
|
|
88
|
+
};
|
|
89
|
+
visit(paramNode, 0);
|
|
90
|
+
return total;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function sourceRange(node) {
|
|
94
|
+
const startLine = node?.startPosition ? node.startPosition.row + 1 : 0;
|
|
95
|
+
const endLine = node?.endPosition ? node.endPosition.row + 1 : startLine;
|
|
96
|
+
return { startLine, endLine, loc: startLine ? Math.max(1, endLine - startLine + 1) : 0 };
|
|
97
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// Syntax node-type and call-name tables for the source-complexity analysis.
|
|
2
|
+
|
|
3
|
+
export const LOOP_NODES = new Set([
|
|
4
|
+
"for_statement", "for_in_statement", "for_each_statement", "enhanced_for_statement",
|
|
5
|
+
"while_statement", "do_statement", "for_in_clause"
|
|
6
|
+
]);
|
|
7
|
+
export const CALL_NODES = new Set(["call", "call_expression", "method_invocation", "object_creation_expression"]);
|
|
8
|
+
export const RETURN_NODES = new Set(["return_statement"]);
|
|
9
|
+
export const AWAIT_NODES = new Set(["await_expression"]);
|
|
10
|
+
export const OBJECT_NODES = new Set(["object", "dictionary", "map", "record_literal"]);
|
|
11
|
+
export const FIXED_ALLOCATION_NODES = new Set([
|
|
12
|
+
...OBJECT_NODES,
|
|
13
|
+
"array", "list", "set", "tuple", "composite_literal", "new_expression",
|
|
14
|
+
"object_creation_expression", "array_creation_expression", "make_expression",
|
|
15
|
+
"list_comprehension", "set_comprehension", "dictionary_comprehension"
|
|
16
|
+
]);
|
|
17
|
+
export const VARIABLE_ALLOCATION_NODES = new Set(["list_comprehension", "set_comprehension", "dictionary_comprehension"]);
|
|
18
|
+
export const SPREAD_NODES = new Set([
|
|
19
|
+
"spread_element", "list_splat", "dictionary_splat", "set_splat", "spread_expression"
|
|
20
|
+
]);
|
|
21
|
+
export const DECLARATION_BOUNDARIES = new Set([
|
|
22
|
+
"function_declaration", "generator_function_declaration", "function_definition",
|
|
23
|
+
"method_definition", "method_declaration", "constructor_declaration",
|
|
24
|
+
"class_declaration", "class_definition", "interface_declaration", "enum_declaration"
|
|
25
|
+
]);
|
|
26
|
+
export const EXPRESSION_CALLABLES = new Set([
|
|
27
|
+
"arrow_function", "function_expression", "generator_function", "lambda", "lambda_expression"
|
|
28
|
+
]);
|
|
29
|
+
export const ARGUMENT_NODES = new Set(["arguments", "argument_list"]);
|
|
30
|
+
export const ITERATOR_CALLS = new Set([
|
|
31
|
+
"foreach", "map", "flatmap", "filter", "reduce", "reduceright", "some", "every",
|
|
32
|
+
"find", "findindex", "findlast", "findlastindex", "collect", "select"
|
|
33
|
+
]);
|
|
34
|
+
export const PRODUCER_CALLS = new Set([
|
|
35
|
+
"map", "flatmap", "filter", "slice", "concat", "split", "toarray", "tolist",
|
|
36
|
+
"keys", "values", "entries", "fromentries", "from", "copy", "clone", "collect", "make",
|
|
37
|
+
"all", "allsettled", "stringify", "parse"
|
|
38
|
+
]);
|
|
39
|
+
export const LINEAR_CALLS = new Set([
|
|
40
|
+
...ITERATOR_CALLS,
|
|
41
|
+
...PRODUCER_CALLS,
|
|
42
|
+
"includes", "indexof", "lastindexof", "join", "reverse", "stringify", "parse",
|
|
43
|
+
"all", "allsettled", "any", "race"
|
|
44
|
+
]);
|
|
45
|
+
export const SORT_CALLS = new Set(["sort", "sorted", "sortby", "orderby", "order_by"]);
|
|
46
|
+
export const IO_PREFIXES = [
|
|
47
|
+
"fetch", "request", "query", "execute", "insert", "update", "delete", "save", "read",
|
|
48
|
+
"write", "send", "publish", "consume", "find", "load", "download", "upload", "connect", "transaction"
|
|
49
|
+
];
|
|
50
|
+
|
|
51
|
+
export const BRANCH_NODES = new Set([
|
|
52
|
+
"if_statement", "elif_clause", "catch_clause", "except_clause", "conditional_expression",
|
|
53
|
+
"ternary_expression", "switch_case", "case_statement", "expression_case", "type_case",
|
|
54
|
+
"communication_case", "match_case"
|
|
55
|
+
]);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// AST-backed, language-aware source complexity summary.
|
|
2
|
+
//
|
|
3
|
+
// This intentionally reports LOCAL algorithmic work separately from calls/I/O. A method with no loop
|
|
4
|
+
// can be O(1) locally while its end-to-end latency remains callee-bound; object/array spreads are linear
|
|
5
|
+
// shallow copies even without an explicit loop. Reports are plain JSON and are persisted on graph nodes,
|
|
6
|
+
// so the renderer never needs a second implementation of the algorithm.
|
|
7
|
+
//
|
|
8
|
+
// Implementation lives in the sibling modules:
|
|
9
|
+
// source-complexity.constants.js — node-type and call-name tables
|
|
10
|
+
// source-complexity.ast.js — tree-sitter node helpers
|
|
11
|
+
// source-complexity.report.js — rank/label/evidence builders
|
|
12
|
+
// source-complexity.walk.js — the syntax walk and summary assembly
|
|
13
|
+
|
|
14
|
+
export { analyzeSyntaxComplexity } from "./source-complexity.walk.js";
|