@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.
- package/CHANGELOG.md +82 -0
- package/CONTRIBUTING.md +32 -25
- package/README.md +49 -50
- package/SECURITY.md +18 -31
- package/UPGRADING.md +101 -0
- package/bin/gruff-ts +3 -0
- package/docs/README.md +11 -10
- package/docs/agent-hook.md +32 -35
- package/docs/ci-integration.md +38 -24
- package/docs/configuration.md +160 -89
- package/docs/dashboard.md +12 -13
- package/docs/output-formats.md +100 -25
- package/docs/philosophy.md +2 -2
- package/docs/reports-and-ci.md +14 -172
- package/docs/rules.md +126 -98
- package/package.json +10 -8
- package/src/analyser.ts +817 -323
- package/src/baseline-file.ts +867 -0
- package/src/baseline-identity.ts +463 -0
- package/src/baseline-options.ts +139 -34
- package/src/baseline.ts +62 -68
- package/src/blocks.ts +512 -336
- package/src/changed-regions.ts +380 -79
- package/src/check-ignore.ts +28 -13
- package/src/class-rules.ts +445 -436
- package/src/cli-program.ts +915 -172
- package/src/cli.ts +9 -3
- package/src/comment-rules.ts +236 -418
- package/src/comment-scanner.ts +339 -47
- package/src/complexity-metrics.ts +111 -35
- package/src/config-load-error.ts +17 -8
- package/src/config-parse.ts +291 -98
- package/src/config-preservation.ts +81 -26
- package/src/config.ts +751 -223
- package/src/constants.ts +7 -6
- package/src/context-doc-rules.ts +182 -54
- package/src/dashboard.ts +88 -23
- package/src/dead-code-rules.ts +153 -74
- package/src/discovery.ts +493 -140
- package/src/doc-rules.ts +602 -126
- package/src/duplicate-test-group-rules.ts +359 -0
- package/src/findings-helpers.ts +134 -109
- package/src/findings.ts +47 -27
- package/src/fixture-purpose-rules.ts +194 -50
- package/src/github-actions-event-guards.ts +331 -0
- package/src/github-actions-rules.ts +328 -121
- package/src/hook-contract.ts +681 -255
- package/src/init-config.ts +163 -118
- package/src/inline-suppressions.ts +340 -0
- package/src/limit-band.ts +56 -0
- package/src/line-rules.ts +331 -468
- package/src/migrate-config.ts +670 -0
- package/src/naming-pushers.ts +32 -127
- package/src/parsed-script.ts +868 -314
- package/src/pillar-summary.ts +56 -40
- package/src/profiles.ts +86 -74
- package/src/project-config-rules.ts +324 -140
- package/src/project-rules.ts +237 -115
- package/src/public-exports.ts +102 -38
- package/src/report-html.ts +217 -85
- package/src/report-renderers.ts +821 -203
- package/src/rule-list.ts +166 -46
- package/src/rules.ts +100 -104
- package/src/safety-rules.ts +191 -132
- package/src/scoring.ts +249 -55
- package/src/security-flow-rules.ts +791 -115
- package/src/selectors.ts +169 -0
- package/src/sensitive-data-rules.ts +460 -387
- package/src/sensitive-exclusions.ts +435 -0
- package/src/source-text.ts +378 -104
- package/src/static-analysis-redundant-rules.ts +298 -41
- package/src/test-block-rules.ts +343 -253
- package/src/text-scans.ts +62 -11
- package/src/types.ts +220 -71
- package/tsconfig.json +2 -1
- package/src/process-exec-metadata.ts +0 -163
- package/src/test-fixtures.ts +0 -671
package/src/public-exports.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
// mismatched class is the
|
|
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
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* `exportedName` is
|
|
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
|
-
*
|
|
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
|
|
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
|
-
|
|
73
|
-
|
|
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
|
-
//
|
|
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
|
-
//
|
|
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
|
-
|
|
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
|
-
//
|
|
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
|
-
|
|
104
|
-
|
|
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
|
|
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
|
-
//
|
|
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
|
-
//
|
|
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
|
-
|
|
133
|
-
|
|
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
|
-
|
|
148
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
201
|
-
|
|
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
|
-
//
|
|
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
|
-
|
|
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
|
-
|
|
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;
|