@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
@@ -0,0 +1,284 @@
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
+ import { createRequire } from "node:module";
6
+ import type { ParsedScript } from "./parsed-script.ts";
7
+
8
+ const require = createRequire(import.meta.url);
9
+ const typescriptSyntax = require("typescript") as typeof import("typescript");
10
+
11
+ type TsSourceFile = import("typescript").SourceFile;
12
+ type TsStatement = import("typescript").Statement;
13
+ type TsExportDeclaration = import("typescript").ExportDeclaration;
14
+ type TsExportAssignment = import("typescript").ExportAssignment;
15
+
16
+ /*
17
+ * Stable declaration categories shown inside class/file finding metadata. `default` and
18
+ * `re-export` represent public values whose local declaration kind is unavailable without type
19
+ * checking, so report users still see why a class was not treated as the sole export.
20
+ */
21
+ export type PublicExportKind = "class" | "interface" | "type" | "enum" | "function" | "default" | "re-export";
22
+
23
+ /*
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.
28
+ */
29
+ export interface PublicExportDeclaration {
30
+ kind: PublicExportKind;
31
+ name: string;
32
+ line: number;
33
+ exportedName?: string;
34
+ }
35
+
36
+ /*
37
+ * The five named declaration forms already covered by the public-doc scan. Keeping this syntax
38
+ * set explicit prevents value exports from silently widening the class/file policy beyond the
39
+ * module shapes named in the rule's user documentation.
40
+ */
41
+ type SupportedDeclaration =
42
+ | import("typescript").ClassDeclaration
43
+ | import("typescript").InterfaceDeclaration
44
+ | import("typescript").TypeAliasDeclaration
45
+ | import("typescript").EnumDeclaration
46
+ | import("typescript").FunctionDeclaration;
47
+
48
+ /**
49
+ * Builds the stable sorted public-declaration contract used by class/file naming diagnostics.
50
+ *
51
+ * @param parsed - shared syntax tree supplying the module declarations and source anchors
52
+ * @returns Sorted declarations; an empty array means the module exposes none of the supported forms.
53
+ */
54
+ export function publicExportDeclarations(parsed: ParsedScript): PublicExportDeclaration[] {
55
+ const localDeclarations = localDeclarationIndex(parsed.sourceFile);
56
+ const publicDeclarations = new Map<string, PublicExportDeclaration>();
57
+ // A second top-level pass can resolve bottom exports because every local declaration is indexed first.
58
+ for (const statement of parsed.sourceFile.statements) {
59
+ collectDirectExport(parsed.sourceFile, statement, publicDeclarations);
60
+ // Export declarations cover named, namespace, wildcard, local, and external re-export syntax.
61
+ if (typescriptSyntax.isExportDeclaration(statement)) {
62
+ collectNamedOrExternalExport(parsed.sourceFile, statement, localDeclarations, publicDeclarations);
63
+ }
64
+ // Export assignments include identifier defaults, expression defaults, and export-equals assignments.
65
+ if (typescriptSyntax.isExportAssignment(statement)) {
66
+ collectExportAssignment(parsed.sourceFile, statement, localDeclarations, publicDeclarations);
67
+ }
68
+ }
69
+ return [...publicDeclarations.values()].sort(comparePublicDeclarations);
70
+ }
71
+
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
+ */
78
+ function localDeclarationIndex(sourceFile: TsSourceFile): Map<string, PublicExportDeclaration> {
79
+ const declarations = new Map<string, PublicExportDeclaration>();
80
+ // A named export can precede the declaration it exposes, so index the whole module before resolving exports.
81
+ for (const statement of sourceFile.statements) {
82
+ const declaration = supportedDeclaration(sourceFile, statement);
83
+ // Anonymous default declarations have no local name for another export statement to reference.
84
+ if (!declaration || declaration.name === "default") {
85
+ continue;
86
+ }
87
+ const existingDeclaration = declarations.get(declaration.name);
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.
90
+ if (!existingDeclaration || declaration.kind === "class" && existingDeclaration.kind !== "class") {
91
+ declarations.set(declaration.name, declaration);
92
+ }
93
+ }
94
+ return declarations;
95
+ }
96
+
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
+ */
105
+ function collectDirectExport(sourceFile: TsSourceFile, statement: TsStatement, publicDeclarations: Map<string, PublicExportDeclaration>): void {
106
+ const declaration = supportedDeclaration(sourceFile, statement);
107
+ // A separate export list can expose a declaration that has no export modifier.
108
+ if (!declaration || !hasModifier(statement, typescriptSyntax.SyntaxKind.ExportKeyword)) {
109
+ return;
110
+ }
111
+ addPublicDeclaration(publicDeclarations, declaration);
112
+ }
113
+
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
+ */
123
+ function collectNamedOrExternalExport(sourceFile: TsSourceFile, statement: TsExportDeclaration, localDeclarations: Map<string, PublicExportDeclaration>, publicDeclarations: Map<string, PublicExportDeclaration>): void {
124
+ const exportLine = sourceLine(sourceFile, statement);
125
+ const exportClause = statement.exportClause;
126
+ // A wildcard still exposes another module surface even though syntax alone cannot name each item.
127
+ if (!exportClause) {
128
+ addPublicDeclaration(publicDeclarations, { kind: "re-export", name: "*", line: exportLine });
129
+ return;
130
+ }
131
+ // Namespace exports have one explicit public name, such as export * as tools.
132
+ if (typescriptSyntax.isNamespaceExport(exportClause)) {
133
+ addPublicDeclaration(publicDeclarations, { kind: "re-export", name: exportClause.name.text, line: exportLine });
134
+ return;
135
+ }
136
+ // Local aliases share their declaration's entry. Repeated re-exports with the same public name also share an entry.
137
+ for (const exportSpecifier of exportClause.elements) {
138
+ // For a local export, look up the original name when aliased and the public name otherwise.
139
+ const localName = (exportSpecifier.propertyName ?? exportSpecifier.name).text;
140
+ const publicName = exportSpecifier.name.text;
141
+ const localDeclaration = statement.moduleSpecifier ? undefined : localDeclarations.get(localName);
142
+ // External or unresolved names remain visible as re-exports so they still prevent sole-class advice.
143
+ addPublicDeclaration(publicDeclarations, localDeclaration
144
+ // A rename keeps the local kind and anchor but records the name consumers actually import.
145
+ ? { ...localDeclaration, ...(publicName === localDeclaration.name ? {} : { exportedName: publicName }) }
146
+ : { kind: "re-export", name: publicName, line: exportLine });
147
+ }
148
+ }
149
+
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
+ */
160
+ function collectExportAssignment(sourceFile: TsSourceFile, statement: TsExportAssignment, localDeclarations: Map<string, PublicExportDeclaration>, publicDeclarations: Map<string, PublicExportDeclaration>): void {
161
+ const localDeclaration = typescriptSyntax.isIdentifier(statement.expression) ? localDeclarations.get(statement.expression.text) : undefined;
162
+ // A named local class exported as default must retain its class name and declaration anchor.
163
+ if (localDeclaration) {
164
+ addPublicDeclaration(publicDeclarations, localDeclaration);
165
+ return;
166
+ }
167
+ const unresolvedExport = statement.isExportEquals
168
+ ? { kind: "re-export" as const, name: "export=", line: sourceLine(sourceFile, statement) }
169
+ : { kind: "default" as const, name: "default", line: sourceLine(sourceFile, statement) };
170
+ addPublicDeclaration(publicDeclarations, unresolvedExport);
171
+ }
172
+
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
+ */
181
+ function supportedDeclaration(sourceFile: TsSourceFile, statement: TsStatement): PublicExportDeclaration | undefined {
182
+ // Other top-level statements, such as imports and exported constants, do not enter this policy slice.
183
+ if (!isSupportedDeclaration(statement)) {
184
+ return undefined;
185
+ }
186
+ const kind = supportedDeclarationKind(statement);
187
+ // Named declarations retain their symbol; anonymous defaults use one stable placeholder.
188
+ const name = statement.name?.text ?? (hasModifier(statement, typescriptSyntax.SyntaxKind.DefaultKeyword) ? "default" : undefined);
189
+ // A non-default anonymous declaration has no stable symbol a class/file finding could present.
190
+ if (!name) {
191
+ return undefined;
192
+ }
193
+ return { kind, name, line: sourceLine(sourceFile, statement) };
194
+ }
195
+
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
+ */
202
+ function isSupportedDeclaration(statement: TsStatement): statement is SupportedDeclaration {
203
+ return typescriptSyntax.isClassDeclaration(statement)
204
+ || typescriptSyntax.isInterfaceDeclaration(statement)
205
+ || typescriptSyntax.isTypeAliasDeclaration(statement)
206
+ || typescriptSyntax.isEnumDeclaration(statement)
207
+ || typescriptSyntax.isFunctionDeclaration(statement);
208
+ }
209
+
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
+ */
216
+ function supportedDeclarationKind(declaration: SupportedDeclaration): Exclude<PublicExportKind, "default" | "re-export"> {
217
+ // The class/file rule uses this category to recognize a sole public class.
218
+ if (typescriptSyntax.isClassDeclaration(declaration)) {
219
+ return "class";
220
+ }
221
+ if (typescriptSyntax.isInterfaceDeclaration(declaration)) {
222
+ return "interface";
223
+ }
224
+ if (typescriptSyntax.isTypeAliasDeclaration(declaration)) {
225
+ return "type";
226
+ }
227
+ if (typescriptSyntax.isEnumDeclaration(declaration)) {
228
+ return "enum";
229
+ }
230
+ return "function";
231
+ }
232
+
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
+ */
240
+ function hasModifier(statement: TsStatement, modifierKind: import("typescript").SyntaxKind): boolean {
241
+ // Statement kinds without modifier storage behave as an empty modifier list for the user.
242
+ const modifiers = typescriptSyntax.canHaveModifiers(statement) ? typescriptSyntax.getModifiers(statement) : undefined;
243
+ return modifiers?.some((modifier) => modifier.kind === modifierKind) === true;
244
+ }
245
+
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
+ */
254
+ function addPublicDeclaration(publicDeclarations: Map<string, PublicExportDeclaration>, declaration: PublicExportDeclaration): void {
255
+ const key = `${declaration.kind}:${declaration.name}`;
256
+ const existingDeclaration = publicDeclarations.get(key);
257
+ // A line tie keeps the first recorded entry, so a later alias cannot replace its public spelling.
258
+ if (!existingDeclaration || declaration.line < existingDeclaration.line) {
259
+ publicDeclarations.set(key, declaration);
260
+ }
261
+ }
262
+
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
+ */
270
+ function sourceLine(sourceFile: TsSourceFile, statement: TsStatement): number {
271
+ return sourceFile.getLineAndCharacterOfPosition(statement.getStart(sourceFile)).line + 1;
272
+ }
273
+
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
+ */
281
+ function comparePublicDeclarations(left: PublicExportDeclaration, right: PublicExportDeclaration): number {
282
+ const labelOrder = `${left.kind}:${left.name}`.localeCompare(`${right.kind}:${right.name}`);
283
+ return labelOrder || left.line - right.line;
284
+ }