@blundergoat/gruff-ts 0.4.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 +123 -1
- package/CONTRIBUTING.md +32 -25
- package/README.md +55 -46
- package/SECURITY.md +18 -30
- package/UPGRADING.md +101 -0
- package/bin/gruff-ts +3 -0
- package/docs/README.md +11 -13
- package/docs/agent-hook.md +33 -24
- package/docs/ci-integration.md +38 -24
- package/docs/configuration.md +173 -71
- 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 -157
- package/docs/rules.md +140 -106
- package/package.json +10 -8
- package/src/analyser.ts +886 -282
- 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 +85 -55
- package/src/blocks.ts +579 -364
- package/src/changed-regions.ts +414 -65
- package/src/check-ignore.ts +28 -13
- package/src/class-rules.ts +461 -302
- package/src/cli-program.ts +956 -166
- package/src/cli.ts +9 -2
- package/src/comment-rules.ts +277 -414
- package/src/comment-scanner.ts +363 -44
- package/src/complexity-metrics.ts +320 -0
- 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 +778 -210
- package/src/constants.ts +7 -6
- package/src/context-doc-rules.ts +188 -61
- package/src/dashboard.ts +88 -23
- package/src/dead-code-rules.ts +153 -74
- package/src/discovery.ts +521 -148
- package/src/doc-rules.ts +592 -147
- package/src/duplicate-test-group-rules.ts +359 -0
- package/src/findings-helpers.ts +240 -89
- package/src/findings.ts +48 -24
- package/src/fixture-purpose-rules.ts +226 -48
- package/src/github-actions-event-guards.ts +331 -0
- package/src/github-actions-rules.ts +336 -106
- package/src/hook-contract.ts +708 -211
- package/src/init-config.ts +169 -114
- package/src/inline-suppressions.ts +340 -0
- package/src/limit-band.ts +56 -0
- package/src/line-rules.ts +331 -419
- package/src/migrate-config.ts +670 -0
- package/src/naming-pushers.ts +33 -107
- package/src/parsed-script.ts +1206 -0
- package/src/pillar-summary.ts +56 -40
- package/src/profiles.ts +97 -71
- package/src/project-config-rules.ts +335 -146
- package/src/project-rules.ts +237 -115
- package/src/public-exports.ts +284 -0
- package/src/report-html.ts +217 -85
- package/src/report-renderers.ts +864 -195
- package/src/rule-list.ts +166 -46
- package/src/rules.ts +100 -104
- package/src/safety-rules.ts +191 -132
- package/src/scoring.ts +255 -52
- package/src/security-flow-rules.ts +930 -109
- package/src/selectors.ts +169 -0
- package/src/sensitive-data-rules.ts +502 -375
- package/src/sensitive-exclusions.ts +435 -0
- package/src/source-text.ts +380 -148
- package/src/static-analysis-redundant-rules.ts +298 -41
- package/src/test-block-rules.ts +422 -228
- package/src/text-scans.ts +67 -9
- package/src/types.ts +222 -71
- package/tsconfig.json +2 -1
- package/src/process-exec-metadata.ts +0 -153
- package/src/test-fixtures.ts +0 -668
package/src/naming-pushers.ts
CHANGED
|
@@ -1,91 +1,28 @@
|
|
|
1
1
|
// Shared "push a naming finding at this line" helpers. Used both by the per-line walker in
|
|
2
|
-
// line-rules.ts and by the parameter
|
|
3
|
-
//
|
|
2
|
+
// line-rules.ts and by the parameter walker in analyser.ts. Stable metadata.surface distinguishes
|
|
3
|
+
// call sites while every finding uses the same fingerprint identity.
|
|
4
4
|
import { type SourceFile } from "./discovery.ts";
|
|
5
5
|
import { makeFinding } from "./findings.ts";
|
|
6
|
-
import { escapeRegex } from "./findings-helpers.ts";
|
|
7
6
|
import type { Config, Finding } from "./types.ts";
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
9
|
* Stable label for the call-site that emitted a naming finding. Surfaces in `metadata.surface`
|
|
11
10
|
* so downstream tooling can split per-pillar reports by origin without re-running the analyser.
|
|
12
11
|
*/
|
|
13
|
-
export type NamingSurface = "declaration" | "parameter" | "destructure"
|
|
12
|
+
export type NamingSurface = "declaration" | "parameter" | "destructure";
|
|
14
13
|
|
|
15
|
-
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
findings.push(
|
|
28
|
-
makeFinding({
|
|
29
|
-
ruleId: "naming.negative-boolean",
|
|
30
|
-
message: `Boolean identifier \`${name}\` is framed as a negation.`,
|
|
31
|
-
filePath: file.displayPath,
|
|
32
|
-
line,
|
|
33
|
-
severity: "advisory",
|
|
34
|
-
pillar: "naming",
|
|
35
|
-
confidence: "medium",
|
|
36
|
-
symbol: name,
|
|
37
|
-
remediation: "Invert the framing so callers do not read a double negation.",
|
|
38
|
-
metadata: { identifierName: name, surface },
|
|
39
|
-
}),
|
|
40
|
-
);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/*
|
|
44
|
-
* Booleans should announce their boolean-ness with an `is`/`has`/`can`/… prefix. The accepted set
|
|
45
|
-
* lives in `config.booleanPrefixes` so projects can tune it. Reports the stable
|
|
46
|
-
* `naming.boolean-prefix` finding.
|
|
47
|
-
*/
|
|
48
|
-
export function pushBooleanPrefixAt(file: SourceFile, line: number, name: string, config: Config, findings: Finding[], surface: NamingSurface): void {
|
|
49
|
-
if (hasBooleanPrefix(name, config.booleanPrefixes) || isAcceptedBooleanStateName(name) || isAcceptedContractBooleanName(name, config, surface)) {
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
findings.push(
|
|
53
|
-
makeFinding({
|
|
54
|
-
ruleId: "naming.boolean-prefix",
|
|
55
|
-
message: `Boolean identifier \`${name}\` should use an intent-revealing prefix.`,
|
|
56
|
-
filePath: file.displayPath,
|
|
57
|
-
line,
|
|
58
|
-
severity: "advisory",
|
|
59
|
-
pillar: "naming",
|
|
60
|
-
confidence: "medium",
|
|
61
|
-
symbol: name,
|
|
62
|
-
remediation: "Use a prefix such as is, has, can, should, or will.",
|
|
63
|
-
metadata: { identifierName: name, surface },
|
|
64
|
-
}),
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const ACCEPTED_BOOLEAN_STATE_NAMES = new Set(["acknowledged", "exists", "validated", "detected", "resolved", "selected", "installed"]);
|
|
69
|
-
const ACCEPTED_BOOLEAN_STATE_SUFFIXES = ["Available", "Required", "Validated", "Detected", "Resolved", "Selected", "Installed"];
|
|
70
|
-
// Some booleans are state adjectives rather than predicate phrases. Keep this list deliberately
|
|
71
|
-
// narrow so vague local names such as `ready` or `enabled` still get the maintainability prompt.
|
|
72
|
-
function isAcceptedBooleanStateName(name: string): boolean {
|
|
73
|
-
return ACCEPTED_BOOLEAN_STATE_NAMES.has(name.toLowerCase()) || ACCEPTED_BOOLEAN_STATE_SUFFIXES.some((suffix) => name.endsWith(suffix));
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// CLI option bags, DTOs, and schema fields often must expose exact external key names such as
|
|
77
|
-
// `verbose` or `ok`. Limit the exact-name exemption to contract fields so ordinary locals still
|
|
78
|
-
// need predicate-style names.
|
|
79
|
-
function isAcceptedContractBooleanName(name: string, config: Config, surface: NamingSurface): boolean {
|
|
80
|
-
return surface === "interface-field" && config.acceptedBooleanNames.has(name.toLowerCase());
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/*
|
|
84
|
-
* Allows the standard `i`, `j`, `k` loop counters and anything on `acceptedAbbreviations`. Reports
|
|
85
|
-
* `naming.short-variable` for any other one or two character name as a stable advisory finding.
|
|
86
|
-
* `loopBodyLineCount` is the for-of caller's signal: when the binding came from a `for (const X of
|
|
87
|
-
* Y) { … }` head and the body spans ≤ 10 lines, the rule suppresses the finding because the name's
|
|
88
|
-
* lifetime is locally obvious. Other call sites (declaration, parameter, destructure) pass undefined.
|
|
14
|
+
/**
|
|
15
|
+
* Checks whether a short name qualifies for a naming.short-variable advisory.
|
|
16
|
+
* Names longer than two characters and i, j and k are exempt.
|
|
17
|
+
*
|
|
18
|
+
* @param file - supplies the display path for the advisory
|
|
19
|
+
* @param line - source line to report with the advisory
|
|
20
|
+
* @param name - identifier to check for sufficient length or an exemption
|
|
21
|
+
* @param config - supplies accepted abbreviations, checked against the lowercased name
|
|
22
|
+
* @param findings - receives the stable advisory when the name fails the check
|
|
23
|
+
* @param surface - records whether the name came from a declaration, parameter or destructure
|
|
24
|
+
* @param loopBodyLineCount - for-of body code-line count. At most 10 exempts the name. Undefined gives no loop exemption.
|
|
25
|
+
* @returns - returns nothing. Appends an advisory to findings when the name is too short and has no exemption.
|
|
89
26
|
*/
|
|
90
27
|
export function pushShortVariableAt(file: SourceFile, line: number, name: string, config: Config, findings: Finding[], surface: NamingSurface, loopBodyLineCount?: number): void {
|
|
91
28
|
if (loopBodyLineCount !== undefined && loopBodyLineCount <= 10) {
|
|
@@ -110,10 +47,16 @@ export function pushShortVariableAt(file: SourceFile, line: number, name: string
|
|
|
110
47
|
);
|
|
111
48
|
}
|
|
112
49
|
|
|
113
|
-
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
50
|
+
/**
|
|
51
|
+
* Checks for generic or numbered placeholder names, including built-in numbered stems.
|
|
52
|
+
*
|
|
53
|
+
* @param file - supplies the display path for the advisory
|
|
54
|
+
* @param line - source line to report with the advisory
|
|
55
|
+
* @param name - identifier to check against the placeholder rules
|
|
56
|
+
* @param config - supplies configured placeholders for full-name and numbered-stem checks
|
|
57
|
+
* @param findings - receives the advisory with stable variant metadata when a placeholder matches
|
|
58
|
+
* @param surface - records whether the name came from a declaration, parameter or destructure
|
|
59
|
+
* @returns - returns nothing. Appends one advisory for a matched placeholder and leaves findings unchanged otherwise.
|
|
117
60
|
*/
|
|
118
61
|
export function pushIdentifierQualityAt(file: SourceFile, line: number, name: string, config: Config, findings: Finding[], surface: NamingSurface): void {
|
|
119
62
|
const variant = identifierQualityVariant(name, config.placeholderNames);
|
|
@@ -136,9 +79,14 @@ export function pushIdentifierQualityAt(file: SourceFile, line: number, name: st
|
|
|
136
79
|
);
|
|
137
80
|
}
|
|
138
81
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
82
|
+
/**
|
|
83
|
+
* Classifies an identifier as a generic or numbered placeholder; returns undefined when no identifier-quality advisory applies.
|
|
84
|
+
* A numbered name ends in digits after a stem made of ASCII letters, underscores or dollar signs.
|
|
85
|
+
*
|
|
86
|
+
* @param name - identifier checked lowercased against placeholderNames or split into a stem and trailing digits
|
|
87
|
+
* @param placeholderNames - configured placeholder names checked for full-name and lowercased stem matches
|
|
88
|
+
* @returns "generic" if lowercased name is in placeholderNames, "numbered" if its lowercased stem is in either placeholder set, or undefined.
|
|
89
|
+
*/
|
|
142
90
|
function identifierQualityVariant(name: string, placeholderNames: Set<string>): string | undefined {
|
|
143
91
|
if (placeholderNames.has(name.toLowerCase())) {
|
|
144
92
|
return "generic";
|
|
@@ -171,25 +119,3 @@ const NUMBERED_PLACEHOLDER_STEMS = new Set([
|
|
|
171
119
|
"value",
|
|
172
120
|
"var",
|
|
173
121
|
]);
|
|
174
|
-
|
|
175
|
-
const BOOLEAN_PREFIX_REGEX_CACHE = new WeakMap<Set<string>, RegExp | null>();
|
|
176
|
-
|
|
177
|
-
// Tests the cached prefix regex from `booleanPrefixRegex`. A null regex (empty prefix set) is
|
|
178
|
-
// treated as "no rule configured" so the boolean-prefix check fires only when configured.
|
|
179
|
-
function hasBooleanPrefix(name: string, prefixes: Set<string>): boolean {
|
|
180
|
-
const regex = booleanPrefixRegex(prefixes);
|
|
181
|
-
return regex !== null && regex.test(name);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
// Cached per prefix Set via a WeakMap so each rule pass reuses the compiled regex instead of
|
|
185
|
-
// rebuilding it for every identifier. The trailing `[A-Z_]` requirement keeps single-letter
|
|
186
|
-
// names like `is` from falsely matching the prefix-followed-by-name pattern.
|
|
187
|
-
function booleanPrefixRegex(prefixes: Set<string>): RegExp | null {
|
|
188
|
-
if (BOOLEAN_PREFIX_REGEX_CACHE.has(prefixes)) {
|
|
189
|
-
return BOOLEAN_PREFIX_REGEX_CACHE.get(prefixes) ?? null;
|
|
190
|
-
}
|
|
191
|
-
const escaped = [...prefixes].map(escapeRegex);
|
|
192
|
-
const regex = prefixes.size === 0 ? null : new RegExp(`^(?:${escaped.join("|")})[A-Z_]`);
|
|
193
|
-
BOOLEAN_PREFIX_REGEX_CACHE.set(prefixes, regex);
|
|
194
|
-
return regex;
|
|
195
|
-
}
|