@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/selectors.ts
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The two families of rule selector, kept apart because they answer different questions.
|
|
3
|
+
*
|
|
4
|
+
* Applies the rule and pillar choices a user makes when running or viewing an analysis.
|
|
5
|
+
*
|
|
6
|
+
* Use `--include-rule` to run a narrower set of rules and score that set.
|
|
7
|
+
* Use `--show-rule` to focus the report while keeping the score and exit status of the evaluated scan.
|
|
8
|
+
*/
|
|
9
|
+
import { ConfigLoadError } from "./config-load-error.ts";
|
|
10
|
+
import { isKnownRuleId, isPillarName } from "./profiles.ts";
|
|
11
|
+
import { ruleDescriptors } from "./rules.ts";
|
|
12
|
+
import type { Config, ExecutionSelectors, Finding, Pillar, Severity } from "./types.ts";
|
|
13
|
+
|
|
14
|
+
// Severity ladder for the display floor; a finding at or above the requested rank is shown.
|
|
15
|
+
const SEVERITY_RANK: Record<Severity, number> = { advisory: 0, warning: 1, error: 2 };
|
|
16
|
+
|
|
17
|
+
/*
|
|
18
|
+
* The user's report view, selected by severity and included or hidden rules and pillars.
|
|
19
|
+
*/
|
|
20
|
+
export interface DisplaySelectors {
|
|
21
|
+
minSeverity?: Severity;
|
|
22
|
+
showRules: string[];
|
|
23
|
+
hideRules: string[];
|
|
24
|
+
showPillars: string[];
|
|
25
|
+
hidePillars: string[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Checks whether execution selectors request any rule or pillar filtering, preserving configured enablement when false.
|
|
30
|
+
*
|
|
31
|
+
* @param selectors - execution include and exclude lists for rules and pillars
|
|
32
|
+
* @returns True if any include or exclude list is non-empty; false preserves configured rule enablement.
|
|
33
|
+
*/
|
|
34
|
+
export function isExecutionRequested(selectors: ExecutionSelectors): boolean {
|
|
35
|
+
return selectors.includeRules.length > 0 || selectors.excludeRules.length > 0
|
|
36
|
+
|| selectors.includePillars.length > 0 || selectors.excludePillars.length > 0;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Checks whether the effective display selectors specify a severity floor or any rule or pillar filter.
|
|
41
|
+
*
|
|
42
|
+
* @param selectors - display filter settings; when every selector is unset or empty, all received findings stay visible
|
|
43
|
+
* @returns True when a minimum severity or any show/hide rule or pillar filter is active.
|
|
44
|
+
*/
|
|
45
|
+
export function isDisplayRequested(selectors: DisplaySelectors): boolean {
|
|
46
|
+
return selectors.minSeverity !== undefined || selectors.showRules.length > 0 || selectors.hideRules.length > 0
|
|
47
|
+
|| selectors.showPillars.length > 0 || selectors.hidePillars.length > 0;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Limits the scan before scoring when the user passes rule or pillar execution flags.
|
|
52
|
+
*
|
|
53
|
+
* @param config - analysis settings whose rule map is updated in place
|
|
54
|
+
* @param selectors - execution filters for rules and pillars. Missing or inactive selectors leave the config unchanged.
|
|
55
|
+
* @returns - Nothing. Disables excluded rules in the config while preserving their other settings.
|
|
56
|
+
* @throws {ConfigLoadError} when a selector is unknown or leaves no enabled rules.
|
|
57
|
+
*/
|
|
58
|
+
export function applyExecutionSelectors(config: Config, selectors: ExecutionSelectors | undefined): void {
|
|
59
|
+
// A run that named no selector keeps the catalogue the user's config already chose.
|
|
60
|
+
if (selectors === undefined || !isExecutionRequested(selectors)) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const unknownRule = [...selectors.includeRules, ...selectors.excludeRules].find((ruleId) => !isKnownRuleId(ruleId));
|
|
64
|
+
// A mistyped rule ID can lead the user to expect coverage that never ran.
|
|
65
|
+
if (unknownRule !== undefined) {
|
|
66
|
+
throw new ConfigLoadError(`Unknown execution selector rule: ${JSON.stringify(unknownRule)}.`, "Use a rule ID from gruff-ts list-rules.", false);
|
|
67
|
+
}
|
|
68
|
+
const unknownPillar = [...selectors.includePillars, ...selectors.excludePillars].find((pillar) => !isPillarName(pillar));
|
|
69
|
+
// A misspelled pillar gets feedback before the CLI can show a misleading empty report.
|
|
70
|
+
if (unknownPillar !== undefined) {
|
|
71
|
+
throw new ConfigLoadError(`Unknown execution selector pillar: ${JSON.stringify(unknownPillar)}.`, "Use a pillar name from gruff-ts list-rules.", false);
|
|
72
|
+
}
|
|
73
|
+
const descriptors = ruleDescriptors();
|
|
74
|
+
// Evaluate the user's include and exclude flags against every rule in the catalogue.
|
|
75
|
+
for (const descriptor of descriptors) {
|
|
76
|
+
// An excluded rule is switched off outright; every other setting the user configured for it survives.
|
|
77
|
+
if (!executionAllows(selectors, descriptor.ruleId, descriptor.pillar)) {
|
|
78
|
+
const existing = config.rules.get(descriptor.ruleId);
|
|
79
|
+
config.rules.set(descriptor.ruleId, { ...existing, options: existing?.options ?? new Map(), enabled: false });
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
// If these flags and project settings leave no active rule, the scan cannot produce a meaningful score.
|
|
83
|
+
if (!descriptors.some((descriptor) => config.rules.get(descriptor.ruleId)?.enabled ?? descriptor.isEnabledByDefault !== false)) {
|
|
84
|
+
throw new ConfigLoadError("Execution selectors leave no rules enabled.", "Select an enabled rule or enable a rule in your configuration.", false);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Evaluates whether a rule and its pillar pass the execution selectors; every non-empty include list must match and either exclude list vetoes.
|
|
90
|
+
*
|
|
91
|
+
* @param selectors - execution include and exclude lists for rules and pillars
|
|
92
|
+
* @param ruleId - identifier of the candidate rule being evaluated
|
|
93
|
+
* @param pillar - pillar to which the candidate rule belongs
|
|
94
|
+
* @returns True if the rule and pillar match all non-empty include lists and appear in neither exclude list.
|
|
95
|
+
*/
|
|
96
|
+
function executionAllows(selectors: ExecutionSelectors, ruleId: string, pillar: Pillar): boolean {
|
|
97
|
+
// An empty rule include list keeps all rules eligible for the other filters.
|
|
98
|
+
if (selectors.includeRules.length > 0 && !selectors.includeRules.includes(ruleId)) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
// An empty pillar include list keeps all pillars eligible for the other filters.
|
|
102
|
+
if (selectors.includePillars.length > 0 && !selectors.includePillars.includes(pillar)) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
return !selectors.excludeRules.includes(ruleId) && !selectors.excludePillars.includes(pillar);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Validates every rule ID and pillar name passed to `--show-rule`, `--hide-rule`, `--show-pillar`, or `--hide-pillar`.
|
|
110
|
+
* Rejects typos or retired IDs before rendering so a misspelled display filter cannot silently hide findings.
|
|
111
|
+
*
|
|
112
|
+
* @param selectors - display filter settings whose show/hide rule and pillar lists are checked; empty lists pass immediately
|
|
113
|
+
* @returns Nothing. Completes when every named display selector exists in the active catalogue.
|
|
114
|
+
* @throws {ConfigLoadError} when any show/hide rule ID or pillar name is unknown.
|
|
115
|
+
*/
|
|
116
|
+
export function validateDisplaySelectors(selectors: DisplaySelectors): void {
|
|
117
|
+
const unknownRule = [...selectors.showRules, ...selectors.hideRules].find((ruleId) => !isKnownRuleId(ruleId));
|
|
118
|
+
// A mistyped or retired rule ID would silently hide or keep findings the user intended to filter.
|
|
119
|
+
if (unknownRule !== undefined) {
|
|
120
|
+
throw new ConfigLoadError(`Unknown display selector rule: ${JSON.stringify(unknownRule)}.`, "Use a rule ID from gruff-ts list-rules.", false);
|
|
121
|
+
}
|
|
122
|
+
const unknownPillar = [...selectors.showPillars, ...selectors.hidePillars].find((pillar) => !isPillarName(pillar));
|
|
123
|
+
// A misspelled pillar gets immediate feedback instead of producing an empty report view.
|
|
124
|
+
if (unknownPillar !== undefined) {
|
|
125
|
+
throw new ConfigLoadError(`Unknown display selector pillar: ${JSON.stringify(unknownPillar)}.`, "Use a pillar name from gruff-ts list-rules.", false);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Select which findings appear in the report. Stable contract: the score and exit code are already decided.
|
|
131
|
+
* Validates show/hide rule and pillar names before filtering so typos fail loudly instead of silently hiding results.
|
|
132
|
+
*
|
|
133
|
+
* @param findings - scored findings available for display; an empty list still validates selector names and stays empty
|
|
134
|
+
* @param selectors - display restrictions for this run, including any explicit minimum severity
|
|
135
|
+
* @param configuredFloor - project minimum severity used when the run supplies none; undefined applies no config floor
|
|
136
|
+
* @returns A filtered list when an effective restriction is active, or the original list when none is active.
|
|
137
|
+
* @throws {ConfigLoadError} when a display selector names an unknown rule ID or pillar.
|
|
138
|
+
*/
|
|
139
|
+
export function applyDisplaySelectors(findings: Finding[], selectors: DisplaySelectors, configuredFloor: Severity | undefined): Finding[] {
|
|
140
|
+
validateDisplaySelectors(selectors);
|
|
141
|
+
const floor = selectors.minSeverity ?? configuredFloor;
|
|
142
|
+
const effective: DisplaySelectors = { ...selectors, ...(floor === undefined ? {} : { minSeverity: floor }) };
|
|
143
|
+
// Nothing was asked for, so the report shows exactly what the run found.
|
|
144
|
+
if (!isDisplayRequested(effective)) {
|
|
145
|
+
return findings;
|
|
146
|
+
}
|
|
147
|
+
return findings.filter((finding) => displayAllows(effective, finding));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Decides whether to show a finding using the severity floor and rule and pillar lists.
|
|
152
|
+
* To pass, the finding must meet any configured floor, match each non-empty show list, and match neither hide list.
|
|
153
|
+
*
|
|
154
|
+
* @param selectors - display filter settings including optional severity floor and show/hide lists
|
|
155
|
+
* @param finding - scored finding evaluated for report visibility
|
|
156
|
+
* @returns True when the finding satisfies the severity floor, show lists, and hide lists.
|
|
157
|
+
*/
|
|
158
|
+
function displayAllows(selectors: DisplaySelectors, finding: Finding): boolean {
|
|
159
|
+
if (selectors.minSeverity !== undefined && SEVERITY_RANK[finding.severity] < SEVERITY_RANK[selectors.minSeverity]) {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
if (selectors.showRules.length > 0 && !selectors.showRules.includes(finding.ruleId)) {
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
if (selectors.showPillars.length > 0 && !selectors.showPillars.includes(finding.pillar)) {
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
return !selectors.hideRules.includes(finding.ruleId) && !selectors.hidePillars.includes(finding.pillar);
|
|
169
|
+
}
|