@boringparts/verule 0.0.0-stage → 0.2.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.
@@ -0,0 +1,21 @@
1
+ import { type DiagnosticLocation, type Evidence, type Gap } from "tssae";
2
+ import { type Diagnostic, type VeruleConfig } from "./types.ts";
3
+ export type CollectedDiagnostic = {
4
+ readonly ruleId: string;
5
+ readonly projectId: string;
6
+ readonly at: DiagnosticLocation;
7
+ readonly message: string;
8
+ readonly findingKey?: string;
9
+ readonly evidence: readonly Evidence[];
10
+ };
11
+ /** Resolves the configured tsconfig/jsconfig entry paths for an analysis session. */
12
+ export declare function resolveProjectEntries(config: VeruleConfig | undefined): readonly string[];
13
+ /**
14
+ * Normalizes, deduplicates, and merges multi-project diagnostics.
15
+ *
16
+ * Diagnostics are merged only when rule id, primary source location, message, `findingKey`,
17
+ * and normalized evidence all match identically; differing conclusions or evidence stay separate.
18
+ */
19
+ export declare function buildDiagnostics(collected: readonly CollectedDiagnostic[]): readonly Diagnostic[];
20
+ /** Deduplicates and deterministically orders analysis gaps across projects and rules. */
21
+ export declare function buildGaps(collected: readonly Gap[]): readonly Gap[];
@@ -0,0 +1,171 @@
1
+ import { createHash } from "node:crypto";
2
+ import { normalizeEvidence, normalizeLocation, } from "tssae";
3
+ import { VeruleExecutionError, } from "./types.js";
4
+ function compareCodepoint(a, b) {
5
+ if (a < b)
6
+ return -1;
7
+ if (a > b)
8
+ return 1;
9
+ return 0;
10
+ }
11
+ function canonicalLocationKey(loc) {
12
+ return `${loc.file}:${loc.start}:${loc.end}:${loc.line}:${loc.column}:${loc.endLine}:${loc.endColumn}`;
13
+ }
14
+ function canonicalEvidenceKey(evidence) {
15
+ return JSON.stringify(evidence.map((item) => ({
16
+ kind: item.kind,
17
+ message: item.message,
18
+ ...(item.at !== undefined ? { at: canonicalLocationKey(item.at) } : {}),
19
+ ...(item.summaryId !== undefined ? { summaryId: item.summaryId } : {}),
20
+ ...(item.summarySource !== undefined
21
+ ? { summarySource: item.summarySource }
22
+ : {}),
23
+ ...(item.declaration !== undefined
24
+ ? { declaration: item.declaration }
25
+ : {}),
26
+ })));
27
+ }
28
+ function compareLocations(a, b) {
29
+ const fileCmp = compareCodepoint(a.file, b.file);
30
+ if (fileCmp !== 0)
31
+ return fileCmp;
32
+ if (a.start !== b.start)
33
+ return a.start - b.start;
34
+ if (a.end !== b.end)
35
+ return a.end - b.end;
36
+ if (a.line !== b.line)
37
+ return a.line - b.line;
38
+ return a.column - b.column;
39
+ }
40
+ /** Resolves the configured tsconfig/jsconfig entry paths for an analysis session. */
41
+ export function resolveProjectEntries(config) {
42
+ if (!config) {
43
+ return ["tsconfig.json"];
44
+ }
45
+ if (config.projects !== undefined) {
46
+ if (config.projects.length === 0) {
47
+ throw new VeruleExecutionError("INVALID_CONFIG", '"projects" must contain at least one tsconfig path when specified.');
48
+ }
49
+ return config.projects;
50
+ }
51
+ if (config.tsconfig !== undefined) {
52
+ return [config.tsconfig];
53
+ }
54
+ return ["tsconfig.json"];
55
+ }
56
+ /**
57
+ * Normalizes, deduplicates, and merges multi-project diagnostics.
58
+ *
59
+ * Diagnostics are merged only when rule id, primary source location, message, `findingKey`,
60
+ * and normalized evidence all match identically; differing conclusions or evidence stay separate.
61
+ */
62
+ export function buildDiagnostics(collected) {
63
+ const normalized = collected.map((item) => ({
64
+ ...item,
65
+ at: normalizeLocation(item.at),
66
+ evidence: normalizeEvidence(item.evidence),
67
+ }));
68
+ const sorted = normalized.toSorted((a, b) => {
69
+ const locCmp = compareLocations(a.at, b.at);
70
+ if (locCmp !== 0)
71
+ return locCmp;
72
+ const ruleCmp = compareCodepoint(a.ruleId, b.ruleId);
73
+ if (ruleCmp !== 0)
74
+ return ruleCmp;
75
+ const keyCmp = compareCodepoint(a.findingKey ?? "", b.findingKey ?? "");
76
+ if (keyCmp !== 0)
77
+ return keyCmp;
78
+ const msgCmp = compareCodepoint(a.message, b.message);
79
+ if (msgCmp !== 0)
80
+ return msgCmp;
81
+ return compareCodepoint(a.projectId, b.projectId);
82
+ });
83
+ const mergedMap = new Map();
84
+ const mergedList = [];
85
+ for (const item of sorted) {
86
+ const mergeKey = JSON.stringify([
87
+ item.ruleId,
88
+ canonicalLocationKey(item.at),
89
+ item.message,
90
+ item.findingKey ?? null,
91
+ canonicalEvidenceKey(item.evidence),
92
+ ]);
93
+ const existing = mergedMap.get(mergeKey);
94
+ if (existing) {
95
+ if (!existing.projects.includes(item.projectId)) {
96
+ existing.projects.push(item.projectId);
97
+ }
98
+ }
99
+ else {
100
+ const entry = {
101
+ ruleId: item.ruleId,
102
+ at: item.at,
103
+ message: item.message,
104
+ ...(item.findingKey !== undefined
105
+ ? { findingKey: item.findingKey }
106
+ : {}),
107
+ evidence: item.evidence,
108
+ projects: [item.projectId],
109
+ };
110
+ mergedMap.set(mergeKey, entry);
111
+ mergedList.push(entry);
112
+ }
113
+ }
114
+ const occurrenceCounts = new Map();
115
+ return mergedList.map((entry) => {
116
+ const anchorKey = entry.findingKey ?? entry.message;
117
+ const sigKey = JSON.stringify([entry.ruleId, entry.at.file, anchorKey]);
118
+ const occurrence = occurrenceCounts.get(sigKey) ?? 0;
119
+ occurrenceCounts.set(sigKey, occurrence + 1);
120
+ const id = createHash("sha256")
121
+ .update(JSON.stringify([
122
+ entry.ruleId,
123
+ entry.at.file,
124
+ anchorKey,
125
+ `${entry.at.start}:${entry.at.end}`,
126
+ occurrence,
127
+ ]))
128
+ .digest("hex")
129
+ .slice(0, 16);
130
+ return {
131
+ id,
132
+ ruleId: entry.ruleId,
133
+ at: entry.at,
134
+ message: entry.message,
135
+ evidence: entry.evidence,
136
+ projects: entry.projects.toSorted(compareCodepoint),
137
+ };
138
+ });
139
+ }
140
+ /** Deduplicates and deterministically orders analysis gaps across projects and rules. */
141
+ export function buildGaps(collected) {
142
+ const unique = new Map();
143
+ for (const rawGap of collected) {
144
+ const normAt = rawGap.at !== undefined ? normalizeLocation(rawGap.at) : undefined;
145
+ const gap = normAt !== rawGap.at && normAt !== undefined
146
+ ? { ...rawGap, at: normAt }
147
+ : rawGap;
148
+ const key = JSON.stringify([
149
+ gap.kind,
150
+ gap.message,
151
+ gap.at !== undefined ? canonicalLocationKey(gap.at) : null,
152
+ ]);
153
+ if (!unique.has(key)) {
154
+ unique.set(key, gap);
155
+ }
156
+ }
157
+ return [...unique.values()].toSorted((a, b) => {
158
+ if (a.at && b.at) {
159
+ const locCmp = compareLocations(a.at, b.at);
160
+ if (locCmp !== 0)
161
+ return locCmp;
162
+ }
163
+ else if (a.at || b.at) {
164
+ return a.at ? -1 : 1;
165
+ }
166
+ const kindCmp = compareCodepoint(a.kind, b.kind);
167
+ if (kindCmp !== 0)
168
+ return kindCmp;
169
+ return compareCodepoint(a.message, b.message);
170
+ });
171
+ }