@blundergoat/gruff-ts 0.5.0 → 0.6.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/CHANGELOG.md +82 -0
- package/CONTRIBUTING.md +32 -25
- package/README.md +49 -50
- package/SECURITY.md +18 -31
- package/UPGRADING.md +101 -0
- package/bin/gruff-ts +3 -0
- package/docs/README.md +11 -10
- package/docs/agent-hook.md +32 -35
- package/docs/ci-integration.md +38 -24
- package/docs/configuration.md +160 -89
- package/docs/dashboard.md +12 -13
- package/docs/output-formats.md +100 -25
- package/docs/philosophy.md +2 -2
- package/docs/reports-and-ci.md +14 -172
- package/docs/rules.md +126 -98
- package/package.json +10 -8
- package/src/analyser.ts +817 -323
- package/src/baseline-file.ts +867 -0
- package/src/baseline-identity.ts +463 -0
- package/src/baseline-options.ts +139 -34
- package/src/baseline.ts +62 -68
- package/src/blocks.ts +512 -336
- package/src/changed-regions.ts +380 -79
- package/src/check-ignore.ts +28 -13
- package/src/class-rules.ts +445 -436
- package/src/cli-program.ts +915 -172
- package/src/cli.ts +9 -3
- package/src/comment-rules.ts +236 -418
- package/src/comment-scanner.ts +339 -47
- package/src/complexity-metrics.ts +111 -35
- package/src/config-load-error.ts +17 -8
- package/src/config-parse.ts +291 -98
- package/src/config-preservation.ts +81 -26
- package/src/config.ts +751 -223
- package/src/constants.ts +7 -6
- package/src/context-doc-rules.ts +182 -54
- package/src/dashboard.ts +88 -23
- package/src/dead-code-rules.ts +153 -74
- package/src/discovery.ts +493 -140
- package/src/doc-rules.ts +602 -126
- package/src/duplicate-test-group-rules.ts +359 -0
- package/src/findings-helpers.ts +134 -109
- package/src/findings.ts +47 -27
- package/src/fixture-purpose-rules.ts +194 -50
- package/src/github-actions-event-guards.ts +331 -0
- package/src/github-actions-rules.ts +328 -121
- package/src/hook-contract.ts +681 -255
- package/src/init-config.ts +163 -118
- package/src/inline-suppressions.ts +340 -0
- package/src/limit-band.ts +56 -0
- package/src/line-rules.ts +331 -468
- package/src/migrate-config.ts +670 -0
- package/src/naming-pushers.ts +32 -127
- package/src/parsed-script.ts +868 -314
- package/src/pillar-summary.ts +56 -40
- package/src/profiles.ts +86 -74
- package/src/project-config-rules.ts +324 -140
- package/src/project-rules.ts +237 -115
- package/src/public-exports.ts +102 -38
- package/src/report-html.ts +217 -85
- package/src/report-renderers.ts +821 -203
- package/src/rule-list.ts +166 -46
- package/src/rules.ts +100 -104
- package/src/safety-rules.ts +191 -132
- package/src/scoring.ts +249 -55
- package/src/security-flow-rules.ts +791 -115
- package/src/selectors.ts +169 -0
- package/src/sensitive-data-rules.ts +460 -387
- package/src/sensitive-exclusions.ts +435 -0
- package/src/source-text.ts +378 -104
- package/src/static-analysis-redundant-rules.ts +298 -41
- package/src/test-block-rules.ts +343 -253
- package/src/text-scans.ts +62 -11
- package/src/types.ts +220 -71
- package/tsconfig.json +2 -1
- package/src/process-exec-metadata.ts +0 -163
- package/src/test-fixtures.ts +0 -671
package/src/scoring.ts
CHANGED
|
@@ -1,45 +1,152 @@
|
|
|
1
|
-
//
|
|
1
|
+
// Computes the quality scores and grades a user sees in reports and score history.
|
|
2
|
+
//
|
|
3
|
+
// A scan scores the rules and files it evaluated. Correlated findings share a penalty when they describe the same declaration.
|
|
4
|
+
// CI uses the configured finding threshold to decide whether to pass or fail independently of the score.
|
|
2
5
|
import { grade } from "./pillar-summary.ts";
|
|
3
6
|
import { ruleDescriptors } from "./rules.ts";
|
|
4
|
-
import type { AnalysisReport, FailThreshold, Finding, Pillar, Severity } from "./types.ts";
|
|
7
|
+
import type { AnalysisReport, Confidence, FailThreshold, Finding, Pillar, Severity } from "./types.ts";
|
|
5
8
|
|
|
6
9
|
// Every pillar that has at least one rule behind it. The composite averages over this whole set,
|
|
7
10
|
// because averaging only finding-bearing pillars let a pillar vanish from the mean the moment its
|
|
8
11
|
// last finding was fixed - lowering the headline score for finishing a pillar.
|
|
9
12
|
const SCOREABLE_PILLARS: readonly Pillar[] = [...new Set(ruleDescriptors().map((descriptor) => descriptor.pillar))];
|
|
10
13
|
|
|
11
|
-
//
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
// Ratified cross-port scoring constants (`bounded-normalized-density-floored`, shared by all five gruff ports).
|
|
15
|
+
//
|
|
16
|
+
// `SCORE_FLOOR` caps how far one saturated pillar drags the composite; `DENSITY_SCALE` is the per-file density halfway between the floor and 100.
|
|
17
|
+
const SCORE_FLOOR = 50;
|
|
18
|
+
const DENSITY_SCALE = 0.1;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Computes a pillar or file score using the shared density curve, where scaling penalty and file count together preserves the score.
|
|
22
|
+
*
|
|
23
|
+
* @param weight - Summed penalty weight for the pillar or file.
|
|
24
|
+
* @param evaluatedFiles - Number of evaluated files used to normalize the penalty density.
|
|
25
|
+
* @returns Rounded score on the bounded density curve, or null when evaluatedFiles is nonpositive because there is no score to include.
|
|
26
|
+
*/
|
|
27
|
+
function curveScore(weight: number, evaluatedFiles: number): number | null {
|
|
28
|
+
if (evaluatedFiles <= 0) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
const density = weight / evaluatedFiles;
|
|
32
|
+
return roundScore(SCORE_FLOOR + (100 - SCORE_FLOOR) / (1 + density / DENSITY_SCALE));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Rounds one score to the ratified two decimals, normalizing negative zero away because JSON
|
|
37
|
+
* projection keeps it in some ports and not others.
|
|
38
|
+
*
|
|
39
|
+
* @param rawScore - Unrounded numeric score to normalize.
|
|
40
|
+
* @returns Score rounded to two decimal places with negative zero normalized to 0.
|
|
41
|
+
*/
|
|
42
|
+
function roundScore(rawScore: number): number {
|
|
43
|
+
const rounded = Math.round(rawScore * 100) / 100;
|
|
44
|
+
return rounded === 0 ? 0 : rounded;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Builds the overall grade, pillar scores, and highest-priority files for the user's report.
|
|
49
|
+
* Invariant: each evaluated or finding-bearing pillar appears once, and a clean pillar scores 100 with evaluated files.
|
|
50
|
+
*
|
|
51
|
+
* @param findings - Findings emitted by the scan to aggregate into pillar, cluster, rule, and file scores.
|
|
52
|
+
* @param evaluatedFiles - Count of evaluated files; zero or less yields null scores and grades because there is no measurement.
|
|
53
|
+
* @param evaluatedPillars - Pillars evaluated by the run so clean pillars still appear alongside finding-bearing ones.
|
|
54
|
+
* @returns Report score summary with composite score, grade, evaluated file count, pillars, clusters, rule attribution, and top offenders.
|
|
55
|
+
*/
|
|
56
|
+
function scoreReport(findings: Finding[], evaluatedFiles: number, evaluatedPillars: Iterable<Pillar> = SCOREABLE_PILLARS): AnalysisReport["score"] {
|
|
17
57
|
const byPillar = new Map<Pillar, Finding[]>();
|
|
18
58
|
const byFile = new Map<string, Finding[]>();
|
|
19
59
|
const penalties = scoringPenaltyMap(findings);
|
|
60
|
+
// Use each finding in both the pillar breakdown and the list of files a user can address.
|
|
20
61
|
for (const finding of findings) {
|
|
21
62
|
byPillar.set(finding.pillar, [...(byPillar.get(finding.pillar) ?? []), finding]);
|
|
22
63
|
byFile.set(finding.filePath, [...(byFile.get(finding.filePath) ?? []), finding]);
|
|
23
64
|
}
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
65
|
+
const scoredPillars = [...new Set([...evaluatedPillars, ...byPillar.keys()])];
|
|
66
|
+
const pillars = scoredPillars.map((pillar) => {
|
|
67
|
+
// Evaluated pillars with no findings still appear as clean in the report.
|
|
68
|
+
const pillarFindings = byPillar.get(pillar) ?? [];
|
|
69
|
+
const penalty = roundScore(pillarFindings.reduce((sum, finding) => sum + findingPenalty(penalties, finding), 0));
|
|
70
|
+
const score = curveScore(penalty, evaluatedFiles);
|
|
71
|
+
return { pillar, applicable: true, score, grade: score === null ? null : grade(score), penalty, findings: pillarFindings.length };
|
|
27
72
|
});
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
const composite =
|
|
73
|
+
const applicableScores = pillars.flatMap((pillar) => (pillar.score === null ? [] : [pillar.score]));
|
|
74
|
+
// Without evaluated files, pillars and the composite have null scores and grades because the scan has no measurement.
|
|
75
|
+
const composite =
|
|
76
|
+
applicableScores.length === 0 ? null : roundScore(applicableScores.reduce((sum, score) => sum + score, 0) / applicableScores.length);
|
|
31
77
|
const topOffenders = [...byFile.entries()]
|
|
32
|
-
.map(([filePath, fileFindings]) =>
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
findings: fileFindings.length
|
|
78
|
+
.map(([filePath, fileFindings]) => {
|
|
79
|
+
// A file's density is its own weighted findings, so file and project scores share one curve.
|
|
80
|
+
const penalty = roundScore(fileFindings.reduce((sum, finding) => sum + findingPenalty(penalties, finding), 0));
|
|
81
|
+
return { filePath, score: evaluatedFiles <= 0 ? null : curveScore(penalty, 1), penalty, findings: fileFindings.length };
|
|
82
|
+
})
|
|
83
|
+
.sort((left, right) => (left.score ?? 100) - (right.score ?? 100));
|
|
84
|
+
return {
|
|
85
|
+
composite,
|
|
86
|
+
grade: composite === null ? null : grade(composite),
|
|
87
|
+
evaluatedFiles,
|
|
88
|
+
scoredPillars,
|
|
89
|
+
clusters: correlatedClusters(findings, penalties),
|
|
90
|
+
ruleAttribution: ruleAttribution(findings, penalties),
|
|
91
|
+
pillars,
|
|
92
|
+
topOffenders,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Builds cluster rows for groups of at least two supplied correlated findings sharing a file and symbol in stable file and symbol order.
|
|
98
|
+
*
|
|
99
|
+
* @param findings - Findings to group by correlated complexity cluster key.
|
|
100
|
+
* @param penalties - Precomputed penalty map summed per group, falling back to normal finding weight for missing entries.
|
|
101
|
+
* @returns Cluster rows sorted by file and symbol with sorted rule IDs, counts, and rounded weights, or empty when no group qualifies.
|
|
102
|
+
*/
|
|
103
|
+
function correlatedClusters(findings: Finding[], penalties: ReadonlyMap<Finding, number>): AnalysisReport["score"]["clusters"] {
|
|
104
|
+
const groups = new Map<string, Finding[]>();
|
|
105
|
+
for (const finding of findings) {
|
|
106
|
+
const key = complexityClusterKey(finding);
|
|
107
|
+
if (key) {
|
|
108
|
+
groups.set(key, [...(groups.get(key) ?? []), finding]);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return [...groups.values()]
|
|
112
|
+
// A lone correlated finding billed its own full weight, so it is not a cluster to report.
|
|
113
|
+
.filter((group) => group.length >= 2)
|
|
114
|
+
.map((group) => ({
|
|
115
|
+
file: group[0]!.filePath,
|
|
116
|
+
symbol: String(group[0]!.symbol),
|
|
117
|
+
ruleIds: group.map((finding) => finding.ruleId).sort(),
|
|
118
|
+
findings: group.length,
|
|
119
|
+
weight: roundScore(group.reduce((sum, finding) => sum + findingPenalty(penalties, finding), 0)),
|
|
36
120
|
}))
|
|
37
|
-
.sort((left, right) => left.
|
|
38
|
-
return { composite, grade: grade(composite), pillars, topOffenders };
|
|
121
|
+
.sort((left, right) => left.file.localeCompare(right.file) || left.symbol.localeCompare(right.symbol));
|
|
39
122
|
}
|
|
40
123
|
|
|
41
|
-
|
|
42
|
-
|
|
124
|
+
/**
|
|
125
|
+
* Aggregates finding counts and rounded scoring penalties by native rule ID in stable rule ID order.
|
|
126
|
+
* The attribution contract keeps the native rule ID even when reporting groups concepts.
|
|
127
|
+
*
|
|
128
|
+
* @param findings - Findings to tally by rule ID; rules with no findings produce no row.
|
|
129
|
+
* @param penalties - Precomputed penalty map used per finding, falling back to normal finding weight when an entry is missing.
|
|
130
|
+
* @returns Attribution entries sorted by native rule ID with finding counts and rounded total weights.
|
|
131
|
+
*/
|
|
132
|
+
function ruleAttribution(findings: Finding[], penalties: ReadonlyMap<Finding, number>): AnalysisReport["score"]["ruleAttribution"] {
|
|
133
|
+
const totals = new Map<string, { findings: number; weight: number }>();
|
|
134
|
+
for (const finding of findings) {
|
|
135
|
+
const entry = totals.get(finding.ruleId) ?? { findings: 0, weight: 0 };
|
|
136
|
+
totals.set(finding.ruleId, { findings: entry.findings + 1, weight: entry.weight + findingPenalty(penalties, finding) });
|
|
137
|
+
}
|
|
138
|
+
return [...totals.entries()]
|
|
139
|
+
.map(([ruleId, entry]) => ({ ruleId, findings: entry.findings, weight: roundScore(entry.weight) }))
|
|
140
|
+
.sort((left, right) => left.ruleId.localeCompare(right.ruleId));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Computes severity tallies emitted in native analysis state, where the four-key shape
|
|
145
|
+
* (advisory/warning/error/total) is nested under summary.findings by the v3 machine adapter.
|
|
146
|
+
*
|
|
147
|
+
* @param findings - Findings to count across advisory, warning, error, and total; total must match the findings array length.
|
|
148
|
+
* @returns Object containing advisory, warning, error, and total counts.
|
|
149
|
+
*/
|
|
43
150
|
function summarize(findings: Finding[]) {
|
|
44
151
|
return {
|
|
45
152
|
advisory: findings.filter((finding) => finding.severity === "advisory").length,
|
|
@@ -49,18 +156,45 @@ function summarize(findings: Finding[]) {
|
|
|
49
156
|
};
|
|
50
157
|
}
|
|
51
158
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
159
|
+
/**
|
|
160
|
+
* Apply the three-value exit contract used by CI and the dashboard, with invalidating diagnostics taking precedence over findings.
|
|
161
|
+
* Diagnostics invalidate the run by default. An explicit false leaves the finding gate in effect.
|
|
162
|
+
*
|
|
163
|
+
* @param report - Analysis results whose diagnostics and findings determine the exit status.
|
|
164
|
+
* @param failOn - Minimum severity for a finding to fail the gate. The none setting disables the finding gate.
|
|
165
|
+
* @param minConfidence - Confidence floor a finding must also meet. The default low admits all finding confidence levels.
|
|
166
|
+
* @returns 2 for any run-invalidating diagnostic, otherwise 1 if any finding meets both floors, otherwise 0.
|
|
167
|
+
*/
|
|
168
|
+
function exitFor(report: AnalysisReport, failOn: FailThreshold, minConfidence: Confidence = "low"): number {
|
|
169
|
+
if (report.diagnostics.some((diagnostic) => diagnostic.invalidatesRun !== false)) {
|
|
57
170
|
return 2;
|
|
58
171
|
}
|
|
59
|
-
return report.findings.some((finding) =>
|
|
172
|
+
return report.findings.some((finding) => reachesGate(finding, failOn, minConfidence)) ? 1 : 0;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Checks whether a finding meets both the severity floor and the confidence floor; both must pass to reach the gate.
|
|
177
|
+
*
|
|
178
|
+
* @param finding - Finding whose severity and confidence are evaluated against the gate floors.
|
|
179
|
+
* @param failOn - Minimum severity threshold; setting it to none always returns false so findings cannot fail the run.
|
|
180
|
+
* @param minConfidence - Minimum confidence rank required alongside the severity floor.
|
|
181
|
+
* @returns True if finding satisfies both failOn and minConfidence, otherwise false.
|
|
182
|
+
*/
|
|
183
|
+
function reachesGate(finding: Finding, failOn: FailThreshold, minConfidence: Confidence): boolean {
|
|
184
|
+
return thresholdTriggered(failOn, finding.severity) && CONFIDENCE_GATE_RANK[finding.confidence] >= CONFIDENCE_GATE_RANK[minConfidence];
|
|
60
185
|
}
|
|
61
186
|
|
|
62
|
-
//
|
|
63
|
-
|
|
187
|
+
// Confidence ladder for the gate; a finding at or above the requested rank can reach it.
|
|
188
|
+
const CONFIDENCE_GATE_RANK: Record<Confidence, number> = { low: 0, medium: 1, high: 2 };
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Checks whether a finding's severity reaches the configured failure threshold, where warning is a floor
|
|
192
|
+
* that includes warnings and errors because higher severities qualify, and error includes only errors.
|
|
193
|
+
*
|
|
194
|
+
* @param thresholdValue - Configured failure threshold; none never triggers, and advisory includes every severity.
|
|
195
|
+
* @param severity - Finding severity to compare against thresholdValue.
|
|
196
|
+
* @returns True when severity meets or exceeds thresholdValue, otherwise false.
|
|
197
|
+
*/
|
|
64
198
|
function thresholdTriggered(thresholdValue: FailThreshold, severity: Severity): boolean {
|
|
65
199
|
if (thresholdValue === "none") {
|
|
66
200
|
return false;
|
|
@@ -74,22 +208,51 @@ function thresholdTriggered(thresholdValue: FailThreshold, severity: Severity):
|
|
|
74
208
|
return severity === "error";
|
|
75
209
|
}
|
|
76
210
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
211
|
+
/**
|
|
212
|
+
* Returns the ratified family severity weight, identical in all five ports so changing it is a family decision.
|
|
213
|
+
*
|
|
214
|
+
* @param severity - Finding severity level to map to its base weight.
|
|
215
|
+
* @returns Numeric scoring weight: 12 for error, 4 for warning, or 1 for advisory.
|
|
216
|
+
*/
|
|
80
217
|
function severityPenalty(severity: Severity): number {
|
|
81
|
-
return severity === "error" ?
|
|
218
|
+
return severity === "error" ? 12 : severity === "warning" ? 4 : 1;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Maps finding confidence to its multiplier so scoring density charges uncertain findings less than high-confidence ones.
|
|
223
|
+
*
|
|
224
|
+
* @param confidence - Confidence level of a finding.
|
|
225
|
+
* @returns Multiplier for severity weight: 1 for high, 0.75 for medium, or 0.5 for low.
|
|
226
|
+
*/
|
|
227
|
+
function confidenceWeight(confidence: Confidence): number {
|
|
228
|
+
return confidence === "high" ? 1 : confidence === "medium" ? 0.75 : 0.5;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Computes one finding's ratified weight by multiplying its severity weight by its confidence weight.
|
|
233
|
+
* Invariant: the weight is never negative, and a low-confidence finding always weighs less than the same severity at high confidence.
|
|
234
|
+
*
|
|
235
|
+
* @param finding - Finding whose severity and confidence determine its weight.
|
|
236
|
+
* @returns Non-negative product of severityPenalty and confidenceWeight.
|
|
237
|
+
*/
|
|
238
|
+
function findingWeight(finding: Finding): number {
|
|
239
|
+
return severityPenalty(finding.severity) * confidenceWeight(finding.confidence);
|
|
82
240
|
}
|
|
83
241
|
|
|
84
|
-
const CORRELATED_COMPLEXITY_RULE_IDS = new Set(["complexity.
|
|
242
|
+
const CORRELATED_COMPLEXITY_RULE_IDS = new Set(["complexity.cyclomatic", "size.function-length"]);
|
|
85
243
|
|
|
86
|
-
|
|
87
|
-
|
|
244
|
+
/**
|
|
245
|
+
* Builds a scoring penalty map keyed by the input findings, where unclustered findings and singleton clusters keep their own weight.
|
|
246
|
+
* Invariant: findings in a complexity cluster share its highest weight equally so that one function incurs one combined penalty.
|
|
247
|
+
*
|
|
248
|
+
* @param findings - Findings to assign individual or cluster-shared scoring penalties.
|
|
249
|
+
* @returns Map from each finding to its effective scoring penalty.
|
|
250
|
+
*/
|
|
88
251
|
function scoringPenaltyMap(findings: Finding[]): Map<Finding, number> {
|
|
89
252
|
const penalties = new Map<Finding, number>();
|
|
90
253
|
const clusters = new Map<string, Finding[]>();
|
|
91
254
|
for (const finding of findings) {
|
|
92
|
-
penalties.set(finding,
|
|
255
|
+
penalties.set(finding, findingWeight(finding));
|
|
93
256
|
const key = complexityClusterKey(finding);
|
|
94
257
|
if (key) {
|
|
95
258
|
clusters.set(key, [...(clusters.get(key) ?? []), finding]);
|
|
@@ -99,32 +262,46 @@ function scoringPenaltyMap(findings: Finding[]): Map<Finding, number> {
|
|
|
99
262
|
if (cluster.length < 2) {
|
|
100
263
|
continue;
|
|
101
264
|
}
|
|
102
|
-
const sharedPenalty = Math.max(...cluster.map(
|
|
265
|
+
const sharedPenalty = Math.max(...cluster.map(findingWeight)) / cluster.length;
|
|
103
266
|
cluster.forEach((finding) => penalties.set(finding, sharedPenalty));
|
|
104
267
|
}
|
|
105
268
|
return penalties;
|
|
106
269
|
}
|
|
107
270
|
|
|
108
|
-
|
|
271
|
+
/**
|
|
272
|
+
* Derives a grouping key for related complexity findings on one declaration so the report can share their penalty.
|
|
273
|
+
* Invariant: findings without a symbol or a matching rule keep their own penalty and return no group key.
|
|
274
|
+
*
|
|
275
|
+
* @param finding - Finding to inspect for declaration identity and correlated complexity rule membership.
|
|
276
|
+
* @returns Composite file and declaration key when correlated, or undefined when the finding stays unclustered.
|
|
277
|
+
*/
|
|
109
278
|
function complexityClusterKey(finding: Finding): string | undefined {
|
|
110
|
-
|
|
279
|
+
// Without a declaration to match, keep the finding visible as its own contribution to the score.
|
|
280
|
+
if (!finding.symbol || !CORRELATED_COMPLEXITY_RULE_IDS.has(finding.ruleId)) {
|
|
111
281
|
return undefined;
|
|
112
282
|
}
|
|
113
|
-
|
|
283
|
+
// Prefer the declaration identity when present so same-named functions can receive separate penalties.
|
|
284
|
+
return `${finding.filePath}\0${finding.baselineSubject ?? finding.symbol}`;
|
|
114
285
|
}
|
|
115
286
|
|
|
116
|
-
|
|
287
|
+
/**
|
|
288
|
+
* Resolves a finding's effective penalty from precomputed penalties or falls back to its individual weight.
|
|
289
|
+
* Invariant: returns the finding's precomputed penalty, including zero, when present in the map.
|
|
290
|
+
*
|
|
291
|
+
* @param penalties - Precomputed penalty lookup keyed by finding instance.
|
|
292
|
+
* @param finding - Finding whose effective penalty is needed.
|
|
293
|
+
* @returns Precomputed penalty from penalties, or findingWeight when the map has no entry.
|
|
294
|
+
*/
|
|
117
295
|
function findingPenalty(penalties: ReadonlyMap<Finding, number>, finding: Finding): number {
|
|
118
|
-
return penalties.get(finding) ??
|
|
296
|
+
return penalties.get(finding) ?? findingWeight(finding);
|
|
119
297
|
}
|
|
120
298
|
|
|
121
|
-
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
* `gruff.analysis.v2` JSON fields, even when score values change.
|
|
299
|
+
/**
|
|
300
|
+
* Shows how each severity scores in the human summary. Each bucket applies its severity penalty with a zero floor.
|
|
301
|
+
* The breakdown belongs to the summary and keeps the gruff.analysis.v3 report schema stable.
|
|
302
|
+
*
|
|
303
|
+
* @param findings - findings to count by their reported severity
|
|
304
|
+
* @returns - count, score and letter grade for each severity. A bucket with no findings scores 100.
|
|
128
305
|
*/
|
|
129
306
|
function severityGradeBreakdown(findings: Finding[]): {
|
|
130
307
|
error: { grade: string; score: number; count: number };
|
|
@@ -142,12 +319,29 @@ function severityGradeBreakdown(findings: Finding[]): {
|
|
|
142
319
|
};
|
|
143
320
|
}
|
|
144
321
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
322
|
+
/**
|
|
323
|
+
* Computes a linear severity-only bucket score and grade clamped at zero, without using the project density curve or confidence weights.
|
|
324
|
+
*
|
|
325
|
+
* @param severity - Severity level whose base penalty scales the bucket deduction.
|
|
326
|
+
* @param count - Number of findings recorded at the supplied severity.
|
|
327
|
+
* @returns Bucket containing the letter grade, clamped score, and finding count.
|
|
328
|
+
*/
|
|
148
329
|
function severityBucket(severity: Severity, count: number): { grade: string; score: number; count: number } {
|
|
149
330
|
const score = Math.max(0, 100 - count * severityPenalty(severity));
|
|
150
331
|
return { grade: grade(score), score, count };
|
|
151
332
|
}
|
|
152
333
|
|
|
153
|
-
|
|
334
|
+
/**
|
|
335
|
+
* Formats one composite score line for text reports, using a shared message when either score or grade is null.
|
|
336
|
+
*
|
|
337
|
+
* @param composite - Numeric composite score, or null when nothing was evaluated.
|
|
338
|
+
* @param gradeLetter - Letter grade label for the composite score, or null when nothing was evaluated.
|
|
339
|
+
* @returns Formatted composite line, or the shared nothing-evaluated message if either input is null.
|
|
340
|
+
*/
|
|
341
|
+
function compositeLine(composite: number | null, gradeLetter: string | null): string {
|
|
342
|
+
return composite === null || gradeLetter === null
|
|
343
|
+
? "Composite: n/a (nothing evaluated)"
|
|
344
|
+
: `Composite: ${gradeLetter} (${composite.toFixed(2)} / 100)`;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
export { compositeLine, scoreReport, summarize, exitFor, severityGradeBreakdown };
|