@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,47 +1,96 @@
1
- // Best-effort field extraction for `gruff-ts init --force`. The strict loader in `config.ts`
2
- // throws on schemaVersion mismatch (deliberate: pre-1.0 break, no migration shim), but the init
3
- // migration path needs to lift `paths.ignore` and `minimumSeverity` off whatever the user has on
4
- // disk so regeneration does not silently clobber curated entries. This module exists because the
5
- // strict validator is the wrong tool for that handoff - the next analyser load will run it.
1
+ // Recovers supported settings when a user regenerates an older config with `init --force`.
2
+ //
3
+ // Keeps ignored paths, command gates, and valid sensitive exclusions with their reasons.
4
+ // Empty collections mean no usable entries were recovered for that setting.
6
5
  import { objectValue, parseConfigFile } from "./config-parse.ts";
7
- import type { FailThreshold, MinimumSeverityCommand } from "./types.ts";
6
+ import { ConfigLoadError } from "./config-load-error.ts";
7
+ import { parseSensitiveExclusions } from "./sensitive-exclusions.ts";
8
+ import type { FailThreshold, MinimumSeverityCommand, SensitiveExclusion } from "./types.ts";
8
9
 
9
- // Carry-over bundle returned to `gruff-ts init --force`. Both fields are best-effort: empty
10
- // values mean nothing was preserved, not that the existing config was empty.
10
+ // Settings recovered for `init --force`. Each empty collection leaves the new config with its generated settings.
11
11
  interface PreservedConfigFields {
12
12
  ignoredPaths: string[];
13
- minimumSeverity: Map<MinimumSeverityCommand, FailThreshold>;
13
+ failOn: Map<MinimumSeverityCommand, FailThreshold>;
14
+ sensitiveExclusions: SensitiveExclusion[];
14
15
  }
15
16
 
16
- /*
17
- * Reads the existing config and returns its `paths.ignore` and `minimumSeverity` blocks. Pre-0.2.0
18
- * configs without `schemaVersion` succeed here (the strict gate is skipped) so the user's entries
19
- * survive `init --force`. Individual malformed entries inside the preserved blocks are dropped
20
- * rather than thrown - the strict validator runs at the next analyser load and surfaces errors
21
- * in the right place. Throws `ConfigLoadError` only on IO/parse failure of the file itself; the
22
- * caller handles that fallback to "preserve nothing" instead of blocking regeneration.
17
+ /**
18
+ * Recovers settings before init --force regenerates a config. Files without a schemaVersion can still supply settings.
19
+ * The regeneration caller proceeds with empty fields if reading or parsing fails.
20
+ *
21
+ * @param configPath - path to the existing config to read
22
+ * @returns - preserved ignore globs, command thresholds, and sensitive exclusions, with empty collections for unavailable entries
23
+ * @throws {ConfigLoadError} - for a missing file, unsupported extension, invalid syntax, or a root value that is not a mapping
24
+ * @throws {Error} - when another filesystem read error occurs
23
25
  */
24
26
  export function extractPreservedConfigFields(configPath: string): PreservedConfigFields {
25
27
  const raw = parseConfigFile(configPath);
26
28
  return {
27
29
  ignoredPaths: extractIgnoredPaths(raw),
28
- minimumSeverity: extractMinimumSeverity(raw),
30
+ failOn: extractFailOn(raw),
31
+ sensitiveExclusions: extractSensitiveExclusions(raw),
29
32
  };
30
33
  }
31
34
 
32
- // `paths.ignore` is a free-form list of glob strings. Non-string entries are dropped silently -
33
- // the strict path enforces the same shape at the next analyser load.
35
+ /**
36
+ * Carries existing sensitive exclusions and their written reasons into the regenerated config.
37
+ *
38
+ * @param raw - parsed config. An absent or empty sensitiveExclusions list supplies no exclusions to carry forward.
39
+ * @returns validated exclusion scopes in order. An invalid entry contributes no exclusion.
40
+ * @throws unexpected errors from the exclusion validator.
41
+ */
42
+ function extractSensitiveExclusions(raw: Record<string, unknown>): SensitiveExclusion[] {
43
+ const entries = raw.sensitiveExclusions;
44
+ // Some older projects have no exclusion list to carry over.
45
+ if (!Array.isArray(entries)) {
46
+ return [];
47
+ }
48
+ const preserved: SensitiveExclusion[] = [];
49
+ const seen = new Set<string>();
50
+ // Review each entry separately so one malformed scope does not discard the user's other exclusions.
51
+ for (const entry of entries) {
52
+ try {
53
+ const [candidate] = parseSensitiveExclusions({ sensitiveExclusions: [entry] });
54
+ // A validator result without a scope adds nothing to the regenerated file.
55
+ if (candidate === undefined) {
56
+ continue;
57
+ }
58
+ const scope = JSON.stringify([candidate.rule, candidate.path, candidate.symbol]);
59
+ // A pasted duplicate keeps the first scope and its original written reason.
60
+ if (!seen.has(scope)) {
61
+ preserved.push(candidate);
62
+ seen.add(scope);
63
+ }
64
+ } catch (error) {
65
+ // A user might have left an exclusion's scope incomplete. Skip invalid config entries and surface unexpected failures.
66
+ if (!(error instanceof ConfigLoadError)) {
67
+ throw error;
68
+ }
69
+ }
70
+ }
71
+ return preserved;
72
+ }
73
+
74
+ /**
75
+ * Extracts glob strings from `paths.ignore` in a parsed config for regeneration, skipping non-string entries.
76
+ *
77
+ * @param raw - parsed existing config mapping to inspect for a `paths.ignore` list
78
+ * @returns glob strings from `paths.ignore`, or an empty list when missing or not an array
79
+ */
34
80
  function extractIgnoredPaths(raw: Record<string, unknown>): string[] {
35
81
  const ignore = objectValue(raw.paths)?.ignore;
36
82
  return Array.isArray(ignore) ? ignore.filter((entry): entry is string => typeof entry === "string") : [];
37
83
  }
38
84
 
39
- // Permissive minimumSeverity reader: each entry is filtered through the same vocabulary the strict
40
- // validator checks (commands × thresholds). Anything off-vocabulary is dropped. The reason we
41
- // don't throw here is so a typo in one entry does not nuke preservation of the other entries.
42
- function extractMinimumSeverity(raw: Record<string, unknown>): Map<MinimumSeverityCommand, FailThreshold> {
85
+ /**
86
+ * Preserves valid per-command gates during config rewrites so that one invalid entry does not discard the others.
87
+ *
88
+ * @param raw - parsed file settings. Legacy minimumSeverity: supplies gates only when failOn supplies no mapping.
89
+ * @returns - map from command to threshold, empty when there is no mapping or no valid entry
90
+ */
91
+ function extractFailOn(raw: Record<string, unknown>): Map<MinimumSeverityCommand, FailThreshold> {
43
92
  const result = new Map<MinimumSeverityCommand, FailThreshold>();
44
- const block = objectValue(raw.minimumSeverity);
93
+ const block = objectValue(raw.failOn) ?? objectValue(raw.minimumSeverity);
45
94
  if (!block) {
46
95
  return result;
47
96
  }
@@ -51,8 +100,14 @@ function extractMinimumSeverity(raw: Record<string, unknown>): Map<MinimumSeveri
51
100
  return result;
52
101
  }
53
102
 
54
- // Per-entry filter: extracted so the outer loop stays linear and the NPath budget for the file
55
- // extractor stays small. Both gates must pass before the entry lands in the preserved map.
103
+ /**
104
+ * Stores an existing config's failure threshold for a supported command in the destination map used for regeneration.
105
+ *
106
+ * @param result - destination map mutated with the validated command threshold when both inputs are supported
107
+ * @param commandName - command name key from the parsed config; only `analyse`, `summary`, and `report` are accepted
108
+ * @param rawThreshold - candidate threshold value; only `none`, `advisory`, `warning`, and `error` are stored
109
+ * @returns Nothing. Leaves `result` unchanged when the command or threshold is unsupported.
110
+ */
56
111
  function addPreservedSeverityEntry(result: Map<MinimumSeverityCommand, FailThreshold>, commandName: string, rawThreshold: unknown): void {
57
112
  if (commandName !== "analyse" && commandName !== "summary" && commandName !== "report") {
58
113
  return;