@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.
Files changed (77) hide show
  1. package/CHANGELOG.md +82 -0
  2. package/CONTRIBUTING.md +32 -25
  3. package/README.md +49 -50
  4. package/SECURITY.md +18 -31
  5. package/UPGRADING.md +101 -0
  6. package/bin/gruff-ts +3 -0
  7. package/docs/README.md +11 -10
  8. package/docs/agent-hook.md +32 -35
  9. package/docs/ci-integration.md +38 -24
  10. package/docs/configuration.md +160 -89
  11. package/docs/dashboard.md +12 -13
  12. package/docs/output-formats.md +100 -25
  13. package/docs/philosophy.md +2 -2
  14. package/docs/reports-and-ci.md +14 -172
  15. package/docs/rules.md +126 -98
  16. package/package.json +10 -8
  17. package/src/analyser.ts +817 -323
  18. package/src/baseline-file.ts +867 -0
  19. package/src/baseline-identity.ts +463 -0
  20. package/src/baseline-options.ts +139 -34
  21. package/src/baseline.ts +62 -68
  22. package/src/blocks.ts +512 -336
  23. package/src/changed-regions.ts +380 -79
  24. package/src/check-ignore.ts +28 -13
  25. package/src/class-rules.ts +445 -436
  26. package/src/cli-program.ts +915 -172
  27. package/src/cli.ts +9 -3
  28. package/src/comment-rules.ts +236 -418
  29. package/src/comment-scanner.ts +339 -47
  30. package/src/complexity-metrics.ts +111 -35
  31. package/src/config-load-error.ts +17 -8
  32. package/src/config-parse.ts +291 -98
  33. package/src/config-preservation.ts +81 -26
  34. package/src/config.ts +751 -223
  35. package/src/constants.ts +7 -6
  36. package/src/context-doc-rules.ts +182 -54
  37. package/src/dashboard.ts +88 -23
  38. package/src/dead-code-rules.ts +153 -74
  39. package/src/discovery.ts +493 -140
  40. package/src/doc-rules.ts +602 -126
  41. package/src/duplicate-test-group-rules.ts +359 -0
  42. package/src/findings-helpers.ts +134 -109
  43. package/src/findings.ts +47 -27
  44. package/src/fixture-purpose-rules.ts +194 -50
  45. package/src/github-actions-event-guards.ts +331 -0
  46. package/src/github-actions-rules.ts +328 -121
  47. package/src/hook-contract.ts +681 -255
  48. package/src/init-config.ts +163 -118
  49. package/src/inline-suppressions.ts +340 -0
  50. package/src/limit-band.ts +56 -0
  51. package/src/line-rules.ts +331 -468
  52. package/src/migrate-config.ts +670 -0
  53. package/src/naming-pushers.ts +32 -127
  54. package/src/parsed-script.ts +868 -314
  55. package/src/pillar-summary.ts +56 -40
  56. package/src/profiles.ts +86 -74
  57. package/src/project-config-rules.ts +324 -140
  58. package/src/project-rules.ts +237 -115
  59. package/src/public-exports.ts +102 -38
  60. package/src/report-html.ts +217 -85
  61. package/src/report-renderers.ts +821 -203
  62. package/src/rule-list.ts +166 -46
  63. package/src/rules.ts +100 -104
  64. package/src/safety-rules.ts +191 -132
  65. package/src/scoring.ts +249 -55
  66. package/src/security-flow-rules.ts +791 -115
  67. package/src/selectors.ts +169 -0
  68. package/src/sensitive-data-rules.ts +460 -387
  69. package/src/sensitive-exclusions.ts +435 -0
  70. package/src/source-text.ts +378 -104
  71. package/src/static-analysis-redundant-rules.ts +298 -41
  72. package/src/test-block-rules.ts +343 -253
  73. package/src/text-scans.ts +62 -11
  74. package/src/types.ts +220 -71
  75. package/tsconfig.json +2 -1
  76. package/src/process-exec-metadata.ts +0 -163
  77. package/src/test-fixtures.ts +0 -671
@@ -1,112 +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 / interface-field walkers in cli.ts and class-rules.ts. Stable
3
- // metadata.surface distinguishes call sites while every finding uses the same fingerprint identity.
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" | "interface-field";
12
+ export type NamingSurface = "declaration" | "parameter" | "destructure";
14
13
 
15
14
  /**
16
- * Selects the report guidance for a boolean's source surface. Contract fields point users to the
17
- * complete replacement allowlist or a serialization map; local code receives a safe rename action.
18
- * @param surface Naming origin shown in finding metadata; interface-field means a contract surface.
19
- * @returns Remediation text and action metadata for the finding emitter.
20
- */
21
- export function booleanPrefixGuidance(surface: NamingSurface): { remediation: string; remediationAction: "APPLY" | "CONFIGURE"; configurationKey?: "allowlists.acceptedBooleanNames" } {
22
- // Interface and named type-literal fields may be external keys that a rename would break.
23
- if (surface === "interface-field") {
24
- return {
25
- remediation: "If this is an external contract key, include it in the complete replacement list at `allowlists.acceptedBooleanNames` or map it at the serialization boundary; otherwise rename the field with an intent-revealing prefix.",
26
- remediationAction: "CONFIGURE",
27
- configurationKey: "allowlists.acceptedBooleanNames",
28
- };
29
- }
30
- return {
31
- remediation: "Rename this boolean with an intent-revealing prefix such as is, has, can, should, or will.",
32
- remediationAction: "APPLY",
33
- };
34
- }
35
-
36
- /*
37
- * Negative-framed booleans (disableX, noX, preventX, …) read as double negations at call sites.
38
- * `negativeBooleanAllowed` is the user-curated exemption list. Reports the stable
39
- * `naming.negative-boolean` finding.
40
- */
41
- export function pushNegativeBooleanAt(file: SourceFile, line: number, name: string, config: Config, findings: Finding[], surface: NamingSurface): void {
42
- if (!/^(?:disable|no|not|prevent|skip|disallow)[A-Z]/.test(name)) {
43
- return;
44
- }
45
- if (config.negativeBooleanAllowed.has(name.toLowerCase())) {
46
- return;
47
- }
48
- findings.push(
49
- makeFinding({
50
- ruleId: "naming.negative-boolean",
51
- message: `Boolean identifier \`${name}\` is framed as a negation.`,
52
- filePath: file.displayPath,
53
- line,
54
- severity: "advisory",
55
- pillar: "naming",
56
- confidence: "medium",
57
- symbol: name,
58
- remediation: "Invert the framing so callers do not read a double negation.",
59
- metadata: { identifierName: name, surface },
60
- }),
61
- );
62
- }
63
-
64
- /*
65
- * Reports typed booleans without an accepted prefix or exact contract name. The finding keeps its
66
- * stable identity while its surface selects safe local-rename or contract configuration guidance.
67
- */
68
- export function pushBooleanPrefixAt(file: SourceFile, line: number, name: string, config: Config, findings: Finding[], surface: NamingSurface): void {
69
- if (hasBooleanPrefix(name, config.booleanPrefixes) || isAcceptedBooleanStateName(name) || isAcceptedContractBooleanName(name, config, surface)) {
70
- return;
71
- }
72
- const { remediation, ...remediationMetadata } = booleanPrefixGuidance(surface);
73
- findings.push(
74
- makeFinding({
75
- ruleId: "naming.boolean-prefix",
76
- message: `Boolean identifier \`${name}\` should use an intent-revealing prefix.`,
77
- filePath: file.displayPath,
78
- line,
79
- severity: "advisory",
80
- pillar: "naming",
81
- confidence: "medium",
82
- symbol: name,
83
- remediation,
84
- metadata: { identifierName: name, surface, ...remediationMetadata },
85
- }),
86
- );
87
- }
88
-
89
- const ACCEPTED_BOOLEAN_STATE_NAMES = new Set(["acknowledged", "exists", "validated", "detected", "resolved", "selected", "installed"]);
90
- const ACCEPTED_BOOLEAN_STATE_SUFFIXES = ["Available", "Required", "Validated", "Detected", "Resolved", "Selected", "Installed"];
91
- // Some booleans are state adjectives rather than predicate phrases. Keep this list deliberately
92
- // narrow so vague local names such as `ready` or `enabled` still get the maintainability prompt.
93
- function isAcceptedBooleanStateName(name: string): boolean {
94
- return ACCEPTED_BOOLEAN_STATE_NAMES.has(name.toLowerCase()) || ACCEPTED_BOOLEAN_STATE_SUFFIXES.some((suffix) => name.endsWith(suffix));
95
- }
96
-
97
- // CLI option bags, DTOs, and schema fields often must expose exact external key names such as
98
- // `verbose` or `ok`. Limit the exact-name exemption to contract fields so ordinary locals still
99
- // need predicate-style names.
100
- function isAcceptedContractBooleanName(name: string, config: Config, surface: NamingSurface): boolean {
101
- return surface === "interface-field" && config.acceptedBooleanNames.has(name.toLowerCase());
102
- }
103
-
104
- /*
105
- * Allows the standard `i`, `j`, `k` loop counters and anything on `acceptedAbbreviations`. Reports
106
- * `naming.short-variable` for any other one or two character name as a stable advisory finding.
107
- * `loopBodyLineCount` is the for-of caller's signal: when the binding came from a `for (const X of
108
- * Y) { … }` head and the body spans ≤ 10 lines, the rule suppresses the finding because the name's
109
- * lifetime is locally obvious. Other call sites (declaration, parameter, destructure) pass undefined.
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.
110
26
  */
111
27
  export function pushShortVariableAt(file: SourceFile, line: number, name: string, config: Config, findings: Finding[], surface: NamingSurface, loopBodyLineCount?: number): void {
112
28
  if (loopBodyLineCount !== undefined && loopBodyLineCount <= 10) {
@@ -131,10 +47,16 @@ export function pushShortVariableAt(file: SourceFile, line: number, name: string
131
47
  );
132
48
  }
133
49
 
134
- /*
135
- * Reports `naming.identifier-quality` when a name resolves to a generic variant via
136
- * `identifierQualityVariant` (placeholder names from config, or built-in low-info forms like `data`).
137
- * The stable `variant` metadata lets downstream tools group by category.
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.
138
60
  */
139
61
  export function pushIdentifierQualityAt(file: SourceFile, line: number, name: string, config: Config, findings: Finding[], surface: NamingSurface): void {
140
62
  const variant = identifierQualityVariant(name, config.placeholderNames);
@@ -157,9 +79,14 @@ export function pushIdentifierQualityAt(file: SourceFile, line: number, name: st
157
79
  );
158
80
  }
159
81
 
160
- // Returns `"generic"` for low-information names from the configured set, `"numbered"` for
161
- // numbered placeholder stems such as `foo1` / `mock2`, or undefined when the name is acceptable.
162
- // Domain tokens with meaningful digits (`adr020`, `step0`, `V110`) are not placeholder names.
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
+ */
163
90
  function identifierQualityVariant(name: string, placeholderNames: Set<string>): string | undefined {
164
91
  if (placeholderNames.has(name.toLowerCase())) {
165
92
  return "generic";
@@ -192,25 +119,3 @@ const NUMBERED_PLACEHOLDER_STEMS = new Set([
192
119
  "value",
193
120
  "var",
194
121
  ]);
195
-
196
- const BOOLEAN_PREFIX_REGEX_CACHE = new WeakMap<Set<string>, RegExp | null>();
197
-
198
- // Tests the cached prefix regex from `booleanPrefixRegex`. A null regex (empty prefix set) is
199
- // treated as "no rule configured" so the boolean-prefix check fires only when configured.
200
- function hasBooleanPrefix(name: string, prefixes: Set<string>): boolean {
201
- const regex = booleanPrefixRegex(prefixes);
202
- return regex !== null && regex.test(name);
203
- }
204
-
205
- // Cached per prefix Set via a WeakMap so each rule pass reuses the compiled regex instead of
206
- // rebuilding it for every identifier. The trailing `[A-Z_]` requirement keeps single-letter
207
- // names like `is` from falsely matching the prefix-followed-by-name pattern.
208
- function booleanPrefixRegex(prefixes: Set<string>): RegExp | null {
209
- if (BOOLEAN_PREFIX_REGEX_CACHE.has(prefixes)) {
210
- return BOOLEAN_PREFIX_REGEX_CACHE.get(prefixes) ?? null;
211
- }
212
- const escaped = [...prefixes].map(escapeRegex);
213
- const regex = prefixes.size === 0 ? null : new RegExp(`^(?:${escaped.join("|")})[A-Z_]`);
214
- BOOLEAN_PREFIX_REGEX_CACHE.set(prefixes, regex);
215
- return regex;
216
- }