@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,7 +1,7 @@
1
- // Syntax-backed public-export inventory for naming diagnostics. It reads the shared parsed
2
- // script, resolves direct, default, and bottom re-export forms to one declaration list, and never
3
- // parses a user's file again. The class/file rule reaches this module when deciding whether a
4
- // mismatched class is the only public declaration a CLI or report user needs to locate.
1
+ // Builds a syntax-backed public-export inventory from the shared script parse for naming checks.
2
+ //
3
+ // Resolves direct, default, and bottom `export { ... }` forms into one declaration list without re-parsing the file.
4
+ // Used by `naming.class-file-mismatch` to check whether a mismatched class is the sole public declaration in a module.
5
5
  import { createRequire } from "node:module";
6
6
  import type { ParsedScript } from "./parsed-script.ts";
7
7
 
@@ -21,11 +21,10 @@ type TsExportAssignment = import("typescript").ExportAssignment;
21
21
  export type PublicExportKind = "class" | "interface" | "type" | "enum" | "function" | "default" | "re-export";
22
22
 
23
23
  /*
24
- * One deterministic public declaration from the user's module. Resolved local re-exports retain
25
- * the local kind, name, and declaration line; unresolved external/default values use the export
26
- * statement line so the inventory remains complete without a semantic program.
27
- * `exportedName` is present only when an alias renames the declaration on its way out, because the
28
- * name a consumer imports is the one a naming rule about the public surface has to judge.
24
+ * One public declaration extracted from a module for class/file naming checks.
25
+ *
26
+ * Local re-exports preserve their local kind, name, and line; external or anonymous defaults anchor on the export statement line.
27
+ * `exportedName` is set only when an `as` alias renames the export so naming checks judge the name callers actually import.
29
28
  */
30
29
  export interface PublicExportDeclaration {
31
30
  kind: PublicExportKind;
@@ -48,7 +47,8 @@ type SupportedDeclaration =
48
47
 
49
48
  /**
50
49
  * Builds the stable sorted public-declaration contract used by class/file naming diagnostics.
51
- * @param parsed Shared parse for the user's script; callers never pass null or undefined.
50
+ *
51
+ * @param parsed - shared syntax tree supplying the module declarations and source anchors
52
52
  * @returns Sorted declarations; an empty array means the module exposes none of the supported forms.
53
53
  */
54
54
  export function publicExportDeclarations(parsed: ParsedScript): PublicExportDeclaration[] {
@@ -61,7 +61,7 @@ export function publicExportDeclarations(parsed: ParsedScript): PublicExportDecl
61
61
  if (typescriptSyntax.isExportDeclaration(statement)) {
62
62
  collectNamedOrExternalExport(parsed.sourceFile, statement, localDeclarations, publicDeclarations);
63
63
  }
64
- // Export assignments cover `export default LocalName` and expression-valued defaults.
64
+ // Export assignments include identifier defaults, expression defaults, and export-equals assignments.
65
65
  if (typescriptSyntax.isExportAssignment(statement)) {
66
66
  collectExportAssignment(parsed.sourceFile, statement, localDeclarations, publicDeclarations);
67
67
  }
@@ -69,11 +69,15 @@ export function publicExportDeclarations(parsed: ParsedScript): PublicExportDecl
69
69
  return [...publicDeclarations.values()].sort(comparePublicDeclarations);
70
70
  }
71
71
 
72
- // Indexes supported top-level declarations before export statements are resolved. Empty source
73
- // produces an empty map, which means a later external re-export remains explicitly unresolved.
72
+ /**
73
+ * Indexes supported named top-level declarations by local name before export statements are resolved.
74
+ *
75
+ * @param sourceFile - Parsed TypeScript syntax tree whose top-level statements are indexed.
76
+ * @returns Map of local names to declarations; empty when the source has no supported named declarations.
77
+ */
74
78
  function localDeclarationIndex(sourceFile: TsSourceFile): Map<string, PublicExportDeclaration> {
75
79
  const declarations = new Map<string, PublicExportDeclaration>();
76
- // Bottom-of-file exports need declarations from the entire module, including later source lines.
80
+ // A named export can precede the declaration it exposes, so index the whole module before resolving exports.
77
81
  for (const statement of sourceFile.statements) {
78
82
  const declaration = supportedDeclaration(sourceFile, statement);
79
83
  // Anonymous default declarations have no local name for another export statement to reference.
@@ -81,8 +85,8 @@ function localDeclarationIndex(sourceFile: TsSourceFile): Map<string, PublicExpo
81
85
  continue;
82
86
  }
83
87
  const existingDeclaration = declarations.get(declaration.name);
84
- // A merged class is the runtime value a bottom export exposes; otherwise retain the earliest
85
- // declaration anchor for deterministic output.
88
+ // Keep the class anchor when declarations merge because it can drive class/file naming advice.
89
+ // Otherwise retain the earliest declaration anchor for deterministic output.
86
90
  if (!existingDeclaration || declaration.kind === "class" && existingDeclaration.kind !== "class") {
87
91
  declarations.set(declaration.name, declaration);
88
92
  }
@@ -90,18 +94,32 @@ function localDeclarationIndex(sourceFile: TsSourceFile): Map<string, PublicExpo
90
94
  return declarations;
91
95
  }
92
96
 
93
- // Adds a supported declaration only when the user exported it on its declaration line.
97
+ /**
98
+ * Collects a directly exported supported declaration from a top-level statement.
99
+ *
100
+ * @param sourceFile - Parsed syntax tree used to compute source line anchors.
101
+ * @param statement - Top-level statement inspected for a supported declaration with an export modifier.
102
+ * @param publicDeclarations - Destination map updated with any qualifying declaration.
103
+ * @returns Nothing. Adds a qualifying declaration to `publicDeclarations` and leaves the map alone otherwise.
104
+ */
94
105
  function collectDirectExport(sourceFile: TsSourceFile, statement: TsStatement, publicDeclarations: Map<string, PublicExportDeclaration>): void {
95
106
  const declaration = supportedDeclaration(sourceFile, statement);
96
- // Local-only declarations are indexed for possible bottom exports but are not public yet.
107
+ // A separate export list can expose a declaration that has no export modifier.
97
108
  if (!declaration || !hasModifier(statement, typescriptSyntax.SyntaxKind.ExportKeyword)) {
98
109
  return;
99
110
  }
100
111
  addPublicDeclaration(publicDeclarations, declaration);
101
112
  }
102
113
 
103
- // Resolves one export declaration to local declaration facts when possible, otherwise records the
104
- // external public name. This is what keeps class selection syntax-independent for CLI users.
114
+ /**
115
+ * Resolves named, namespace, wildcard, and external exports for class/file naming diagnostics.
116
+ *
117
+ * @param sourceFile - Parsed syntax tree used for export line anchors.
118
+ * @param statement - Export declaration statement to resolve.
119
+ * @param localDeclarations - Index of local top-level declarations keyed by local name.
120
+ * @param publicDeclarations - Destination map updated with resolved local or re-export entries.
121
+ * @returns Nothing. Records local entries or external and unresolved names as re-exports to avoid sole-class advice.
122
+ */
105
123
  function collectNamedOrExternalExport(sourceFile: TsSourceFile, statement: TsExportDeclaration, localDeclarations: Map<string, PublicExportDeclaration>, publicDeclarations: Map<string, PublicExportDeclaration>): void {
106
124
  const exportLine = sourceLine(sourceFile, statement);
107
125
  const exportClause = statement.exportClause;
@@ -110,14 +128,14 @@ function collectNamedOrExternalExport(sourceFile: TsSourceFile, statement: TsExp
110
128
  addPublicDeclaration(publicDeclarations, { kind: "re-export", name: "*", line: exportLine });
111
129
  return;
112
130
  }
113
- // Namespace exports have one explicit public name, such as `export * as tools`.
131
+ // Namespace exports have one explicit public name, such as export * as tools.
114
132
  if (typescriptSyntax.isNamespaceExport(exportClause)) {
115
133
  addPublicDeclaration(publicDeclarations, { kind: "re-export", name: exportClause.name.text, line: exportLine });
116
134
  return;
117
135
  }
118
- // Each named export is one public declaration unless it resolves to the same local declaration.
136
+ // Local aliases share their declaration's entry. Repeated re-exports with the same public name also share an entry.
119
137
  for (const exportSpecifier of exportClause.elements) {
120
- // An alias resolves through its local source name; an unaliased export uses its public name.
138
+ // For a local export, look up the original name when aliased and the public name otherwise.
121
139
  const localName = (exportSpecifier.propertyName ?? exportSpecifier.name).text;
122
140
  const publicName = exportSpecifier.name.text;
123
141
  const localDeclaration = statement.moduleSpecifier ? undefined : localDeclarations.get(localName);
@@ -129,8 +147,16 @@ function collectNamedOrExternalExport(sourceFile: TsSourceFile, statement: TsExp
129
147
  }
130
148
  }
131
149
 
132
- // Resolves an identifier-valued default/export-equals assignment to its declaration. Expression
133
- // defaults remain one explicit public item because the user still exported a second module value.
150
+ /**
151
+ * Resolves a default or export-equals assignment so locally resolved identifiers retain their declaration kind, name, and line.
152
+ * Unresolved identifiers and expressions record a default or export-equals placeholder anchored at the assignment.
153
+ *
154
+ * @param sourceFile - Parsed syntax tree supplying source line anchors.
155
+ * @param statement - Export assignment statement (`export default` or `export =`) to resolve.
156
+ * @param localDeclarations - Index of local declarations used to resolve exported identifiers.
157
+ * @param publicDeclarations - Destination map updated with the resolved entry or placeholder.
158
+ * @returns Nothing. Adds the locally resolved declaration or a default/export-equals placeholder to `publicDeclarations`.
159
+ */
134
160
  function collectExportAssignment(sourceFile: TsSourceFile, statement: TsExportAssignment, localDeclarations: Map<string, PublicExportDeclaration>, publicDeclarations: Map<string, PublicExportDeclaration>): void {
135
161
  const localDeclaration = typescriptSyntax.isIdentifier(statement.expression) ? localDeclarations.get(statement.expression.text) : undefined;
136
162
  // A named local class exported as default must retain its class name and declaration anchor.
@@ -144,8 +170,14 @@ function collectExportAssignment(sourceFile: TsSourceFile, statement: TsExportAs
144
170
  addPublicDeclaration(publicDeclarations, unresolvedExport);
145
171
  }
146
172
 
147
- // Projects a supported top-level syntax node into the user-visible kind, name, and line tuple.
148
- // Undefined means the statement is outside the class/interface/type/enum/function policy surface.
173
+ /**
174
+ * Extracts a supported statement's kind, name, and starting line for class/file naming diagnostics.
175
+ * Anonymous default declarations use the placeholder name `default`.
176
+ *
177
+ * @param sourceFile - Parsed syntax tree supplying source line positions.
178
+ * @param statement - Top-level statement to inspect.
179
+ * @returns Declaration metadata, or undefined for unsupported statements or declarations without a usable name.
180
+ */
149
181
  function supportedDeclaration(sourceFile: TsSourceFile, statement: TsStatement): PublicExportDeclaration | undefined {
150
182
  // Other top-level statements, such as imports and exported constants, do not enter this policy slice.
151
183
  if (!isSupportedDeclaration(statement)) {
@@ -161,7 +193,12 @@ function supportedDeclaration(sourceFile: TsSourceFile, statement: TsStatement):
161
193
  return { kind, name, line: sourceLine(sourceFile, statement) };
162
194
  }
163
195
 
164
- // Narrows top-level syntax to the declaration forms named by the class/file rule policy.
196
+ /**
197
+ * Narrows top-level syntax to the declaration forms named by the class/file rule policy.
198
+ *
199
+ * @param statement - Top-level AST statement to check.
200
+ * @returns True when `statement` is a supported class, interface, type alias, enum, or function declaration.
201
+ */
165
202
  function isSupportedDeclaration(statement: TsStatement): statement is SupportedDeclaration {
166
203
  return typescriptSyntax.isClassDeclaration(statement)
167
204
  || typescriptSyntax.isInterfaceDeclaration(statement)
@@ -170,50 +207,77 @@ function isSupportedDeclaration(statement: TsStatement): statement is SupportedD
170
207
  || typescriptSyntax.isFunctionDeclaration(statement);
171
208
  }
172
209
 
173
- // Converts a narrowed syntax declaration to the stable lowercase metadata category.
210
+ /**
211
+ * Converts a narrowed syntax declaration to the stable lowercase metadata category.
212
+ *
213
+ * @param declaration - Narrowed supported AST declaration.
214
+ * @returns Stable lowercase kind string (`class`, `interface`, `type`, `enum`, or `function`).
215
+ */
174
216
  function supportedDeclarationKind(declaration: SupportedDeclaration): Exclude<PublicExportKind, "default" | "re-export"> {
175
- // The class check comes first because it is the only kind eligible for the eventual finding.
217
+ // The class/file rule uses this category to recognize a sole public class.
176
218
  if (typescriptSyntax.isClassDeclaration(declaration)) {
177
219
  return "class";
178
220
  }
179
- // Interfaces and type aliases remain distinct so users can understand a multi-export contract surface.
180
221
  if (typescriptSyntax.isInterfaceDeclaration(declaration)) {
181
222
  return "interface";
182
223
  }
183
224
  if (typescriptSyntax.isTypeAliasDeclaration(declaration)) {
184
225
  return "type";
185
226
  }
186
- // Enums keep their own category so a report explains why a module is multi-public.
187
227
  if (typescriptSyntax.isEnumDeclaration(declaration)) {
188
228
  return "enum";
189
229
  }
190
230
  return "function";
191
231
  }
192
232
 
193
- // Reads one modifier without assuming every statement kind exposes a modifier array.
233
+ /**
234
+ * Checks whether a statement carries a given TypeScript modifier using the syntax API.
235
+ *
236
+ * @param statement - Top-level statement whose modifiers are inspected.
237
+ * @param modifierKind - TypeScript syntax kind of the modifier to look up.
238
+ * @returns True when `statement` can have modifiers and includes `modifierKind`.
239
+ */
194
240
  function hasModifier(statement: TsStatement, modifierKind: import("typescript").SyntaxKind): boolean {
195
241
  // Statement kinds without modifier storage behave as an empty modifier list for the user.
196
242
  const modifiers = typescriptSyntax.canHaveModifiers(statement) ? typescriptSyntax.getModifiers(statement) : undefined;
197
243
  return modifiers?.some((modifier) => modifier.kind === modifierKind) === true;
198
244
  }
199
245
 
200
- // Adds one declaration by stable kind/name key. Repeated aliases and merged declarations stay one
201
- // public item, with the earliest source anchor retained for deterministic user output.
246
+ /**
247
+ * Records a public declaration keyed by kind and name, keeping the earliest source anchor for deterministic user output.
248
+ * Repeated aliases and same-kind merged declarations stay one public item.
249
+ *
250
+ * @param publicDeclarations - Destination map of public declarations keyed by `kind:name`.
251
+ * @param declaration - Candidate public declaration to insert if unseen or earlier in source order.
252
+ * @returns Nothing. Updates `publicDeclarations` only when the key is new or `declaration.line` is earlier.
253
+ */
202
254
  function addPublicDeclaration(publicDeclarations: Map<string, PublicExportDeclaration>, declaration: PublicExportDeclaration): void {
203
255
  const key = `${declaration.kind}:${declaration.name}`;
204
256
  const existingDeclaration = publicDeclarations.get(key);
205
- // The first source occurrence is the clearest anchor when aliases expose the same declaration twice.
257
+ // A line tie keeps the first recorded entry, so a later alias cannot replace its public spelling.
206
258
  if (!existingDeclaration || declaration.line < existingDeclaration.line) {
207
259
  publicDeclarations.set(key, declaration);
208
260
  }
209
261
  }
210
262
 
211
- // Converts a syntax start offset to the one-based source line shown in findings and metadata probes.
263
+ /**
264
+ * Computes a statement's one-based starting line for the public export inventory and class/file finding anchors.
265
+ *
266
+ * @param sourceFile - Parsed syntax tree containing the statement.
267
+ * @param statement - AST statement whose starting line is measured.
268
+ * @returns One-based line number of `statement`'s start position.
269
+ */
212
270
  function sourceLine(sourceFile: TsSourceFile, statement: TsStatement): number {
213
271
  return sourceFile.getLineAndCharacterOfPosition(statement.getStart(sourceFile)).line + 1;
214
272
  }
215
273
 
216
- // Sorts by the serialized kind:name value, then line as a deterministic tie-breaker.
274
+ /**
275
+ * Compares two public declarations by `kind:name` and then starting line for stable export output.
276
+ *
277
+ * @param left - First public declaration to compare.
278
+ * @param right - Second public declaration to compare.
279
+ * @returns Negative, zero, or positive comparison value for sorting `left` relative to `right`.
280
+ */
217
281
  function comparePublicDeclarations(left: PublicExportDeclaration, right: PublicExportDeclaration): number {
218
282
  const labelOrder = `${left.kind}:${left.name}`.localeCompare(`${right.kind}:${right.name}`);
219
283
  return labelOrder || left.line - right.line;