@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.
Files changed (77) hide show
  1. package/CHANGELOG.md +123 -1
  2. package/CONTRIBUTING.md +32 -25
  3. package/README.md +55 -46
  4. package/SECURITY.md +18 -30
  5. package/UPGRADING.md +101 -0
  6. package/bin/gruff-ts +3 -0
  7. package/docs/README.md +11 -13
  8. package/docs/agent-hook.md +33 -24
  9. package/docs/ci-integration.md +38 -24
  10. package/docs/configuration.md +173 -71
  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 -157
  15. package/docs/rules.md +140 -106
  16. package/package.json +10 -8
  17. package/src/analyser.ts +886 -282
  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 +85 -55
  22. package/src/blocks.ts +579 -364
  23. package/src/changed-regions.ts +414 -65
  24. package/src/check-ignore.ts +28 -13
  25. package/src/class-rules.ts +461 -302
  26. package/src/cli-program.ts +956 -166
  27. package/src/cli.ts +9 -2
  28. package/src/comment-rules.ts +277 -414
  29. package/src/comment-scanner.ts +363 -44
  30. package/src/complexity-metrics.ts +320 -0
  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 +778 -210
  35. package/src/constants.ts +7 -6
  36. package/src/context-doc-rules.ts +188 -61
  37. package/src/dashboard.ts +88 -23
  38. package/src/dead-code-rules.ts +153 -74
  39. package/src/discovery.ts +521 -148
  40. package/src/doc-rules.ts +592 -147
  41. package/src/duplicate-test-group-rules.ts +359 -0
  42. package/src/findings-helpers.ts +240 -89
  43. package/src/findings.ts +48 -24
  44. package/src/fixture-purpose-rules.ts +226 -48
  45. package/src/github-actions-event-guards.ts +331 -0
  46. package/src/github-actions-rules.ts +336 -106
  47. package/src/hook-contract.ts +708 -211
  48. package/src/init-config.ts +169 -114
  49. package/src/inline-suppressions.ts +340 -0
  50. package/src/limit-band.ts +56 -0
  51. package/src/line-rules.ts +331 -419
  52. package/src/migrate-config.ts +670 -0
  53. package/src/naming-pushers.ts +33 -107
  54. package/src/parsed-script.ts +1206 -0
  55. package/src/pillar-summary.ts +56 -40
  56. package/src/profiles.ts +97 -71
  57. package/src/project-config-rules.ts +335 -146
  58. package/src/project-rules.ts +237 -115
  59. package/src/public-exports.ts +284 -0
  60. package/src/report-html.ts +217 -85
  61. package/src/report-renderers.ts +864 -195
  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 +255 -52
  66. package/src/security-flow-rules.ts +930 -109
  67. package/src/selectors.ts +169 -0
  68. package/src/sensitive-data-rules.ts +502 -375
  69. package/src/sensitive-exclusions.ts +435 -0
  70. package/src/source-text.ts +380 -148
  71. package/src/static-analysis-redundant-rules.ts +298 -41
  72. package/src/test-block-rules.ts +422 -228
  73. package/src/text-scans.ts +67 -9
  74. package/src/types.ts +222 -71
  75. package/tsconfig.json +2 -1
  76. package/src/process-exec-metadata.ts +0 -153
  77. package/src/test-fixtures.ts +0 -668
@@ -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 / 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
- /*
16
- * Negative-framed booleans (disableX, noX, preventX, …) read as double negations at call sites.
17
- * `negativeBooleanAllowed` is the user-curated exemption list. Reports the stable
18
- * `naming.negative-boolean` finding.
19
- */
20
- export function pushNegativeBooleanAt(file: SourceFile, line: number, name: string, config: Config, findings: Finding[], surface: NamingSurface): void {
21
- if (!/^(?:disable|no|not|prevent|skip|disallow)[A-Z]/.test(name)) {
22
- return;
23
- }
24
- if (config.negativeBooleanAllowed.has(name.toLowerCase())) {
25
- return;
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
- * Reports `naming.identifier-quality` when a name resolves to a generic variant via
115
- * `identifierQualityVariant` (placeholder names from config, or built-in low-info forms like `data`).
116
- * 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.
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
- // Returns `"generic"` for low-information names from the configured set, `"numbered"` for
140
- // numbered placeholder stems such as `foo1` / `mock2`, or undefined when the name is acceptable.
141
- // 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
+ */
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
- }