@blundergoat/gruff-ts 0.3.2 → 0.4.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 +13 -0
- package/docs/agent-hook.md +1 -1
- package/docs/configuration.md +15 -0
- package/docs/reports-and-ci.md +21 -0
- package/docs/rules.md +18 -9
- package/package.json +1 -1
- package/src/analyser.ts +397 -121
- package/src/baseline-options.ts +90 -0
- package/src/changed-regions.ts +21 -0
- package/src/cli-program.ts +4 -1
- package/src/cli.ts +3 -2
- package/src/constants.ts +1 -1
- package/src/dead-code-rules.ts +10 -3
- package/src/discovery.ts +27 -3
- package/src/hook-contract.ts +36 -14
- package/src/line-rules.ts +126 -19
- package/src/process-exec-metadata.ts +153 -0
- package/src/project-config-rules.ts +21 -8
- package/src/project-rules.ts +159 -42
- package/src/report-renderers.ts +3 -0
- package/src/rules.ts +5 -5
- package/src/sensitive-data-rules.ts +132 -10
- package/src/source-text.ts +47 -22
- package/src/static-analysis-redundant-rules.ts +46 -2
- package/src/types.ts +15 -0
package/src/analyser.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
// Analyser pipeline: walks discovered sources, runs every rule pass (complexity, dead-code, design,
|
|
2
2
|
// documentation, maintainability, modernisation, naming, security, sensitive-data, size, test-quality),
|
|
3
3
|
// aggregates findings into the `gruff.analysis.v2` schema, and exposes `analyse` to the CLI shell.
|
|
4
|
-
import {
|
|
4
|
+
import { Buffer } from "node:buffer";
|
|
5
|
+
import { readFileSync } from "node:fs";
|
|
5
6
|
import { cwd } from "node:process";
|
|
6
|
-
import { basename
|
|
7
|
-
import {
|
|
7
|
+
import { basename } from "node:path";
|
|
8
|
+
import { dedupeFindings, recordHistory } from "./baseline.ts";
|
|
9
|
+
import { applyBaselineOptions, type BaselineApplication } from "./baseline-options.ts";
|
|
8
10
|
import { changedRegionScope, filterChangedFindings } from "./changed-regions.ts";
|
|
9
11
|
import { loadConfig, optionNumber, ruleEnabled, ruleSeverity, threshold } from "./config.ts";
|
|
10
12
|
import { VERSION } from "./constants.ts";
|
|
@@ -12,7 +14,7 @@ import { absolutize, discoverSources, displayPath, type SourceFile } from "./dis
|
|
|
12
14
|
import { makeFinding } from "./findings.ts";
|
|
13
15
|
import { finding } from "./findings-helpers.ts";
|
|
14
16
|
import { commentRecords } from "./comment-scanner.ts";
|
|
15
|
-
import { analyseArchitectureRules, buildProjectIndex, isProductionSourcePath, isTestPath, type ProjectSource } from "./project-rules.ts";
|
|
17
|
+
import { analyseArchitectureRules, analyseCircularImportRule, buildProjectIndex, CIRCULAR_IMPORT_RULE_ID, isProductionSourcePath, isTestPath, type ProjectSource } from "./project-rules.ts";
|
|
16
18
|
import { analyseBlockRules, type BlockRuleContext, blockRuleContext, type FunctionBlock, functionBlocks, parameterNames } from "./blocks.ts";
|
|
17
19
|
import { analyseClassRules, analyseAcronymCase, analyseInconsistentCasing, analyseInterfaceFields, collectDeclaredIdentifiers } from "./class-rules.ts";
|
|
18
20
|
import { analyseDeadCode, analyseUnreachable, analyseUnusedImports } from "./dead-code-rules.ts";
|
|
@@ -24,10 +26,20 @@ import { pushBooleanPrefixAt, pushIdentifierQualityAt, pushNegativeBooleanAt, pu
|
|
|
24
26
|
import { analyseTestBlock } from "./test-block-rules.ts";
|
|
25
27
|
import { analyseGithubActionsRules } from "./github-actions-rules.ts";
|
|
26
28
|
import { analyseProjectConfigRules } from "./project-config-rules.ts";
|
|
29
|
+
import { ruleDescriptors } from "./rules.ts";
|
|
27
30
|
import { scoreReport, summarize } from "./scoring.ts";
|
|
28
31
|
import { analyseSensitiveData } from "./sensitive-data-rules.ts";
|
|
29
32
|
import { maskNonCode, maskTemplateLiteralBodies, parseDiagnostics } from "./source-text.ts";
|
|
30
|
-
import type { AnalysisOptions, AnalysisReport, Config, Finding, RunDiagnostic, SkippedPath } from "./types.ts";
|
|
33
|
+
import type { AnalysisOptions, AnalysisReport, Config, Finding, Pillar, RunDiagnostic, ScanSurfaceNote, SkippedPath } from "./types.ts";
|
|
34
|
+
|
|
35
|
+
/*
|
|
36
|
+
* Internal hook view contract: both reports come from one current-tree scan so full-file and
|
|
37
|
+
* changed-region rendering stay stable and cannot drift.
|
|
38
|
+
*/
|
|
39
|
+
export interface HookAnalysisReports {
|
|
40
|
+
currentReport: AnalysisReport;
|
|
41
|
+
scopedReport: AnalysisReport;
|
|
42
|
+
}
|
|
31
43
|
|
|
32
44
|
/**
|
|
33
45
|
* Analyse the configured paths and return the stable gruff.analysis.v2 report contract.
|
|
@@ -36,26 +48,59 @@ import type { AnalysisOptions, AnalysisReport, Config, Finding, RunDiagnostic, S
|
|
|
36
48
|
* @returns Versioned report with fingerprinted findings, diagnostics, paths, and score data.
|
|
37
49
|
*/
|
|
38
50
|
export function analyse(options: AnalysisOptions): AnalysisReport {
|
|
51
|
+
const preparation = prepareAnalysis(options);
|
|
52
|
+
const changedScope = changedRegionScope(options);
|
|
53
|
+
const run = completeAnalysis(preparation, options);
|
|
54
|
+
const changedResult = filterChangedFindings(run.baselineResult.findings, changedScope, run.scanned.sources);
|
|
55
|
+
|
|
56
|
+
if (options.historyFile) {
|
|
57
|
+
recordHistory(run.projectRoot, options.historyFile, changedResult.findings, run.diagnostics);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return reportFromRun(run, options, { ...run.baselineResult, findings: changedResult.findings }, changedResult.suppressedCount);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Builds the hook's full and changed-region reports from one scan. The diff-base replay still uses
|
|
64
|
+
// the ordinary runner because it analyses different file contents.
|
|
65
|
+
export function analyseHookReports(currentOptions: AnalysisOptions, scopedOptions: AnalysisOptions, hasChangedRegion: boolean): HookAnalysisReports {
|
|
66
|
+
const run = completeAnalysis(prepareAnalysis(currentOptions), currentOptions);
|
|
67
|
+
const currentReport = reportFromRun(run, currentOptions, run.baselineResult);
|
|
68
|
+
if (!hasChangedRegion) {
|
|
69
|
+
return { currentReport, scopedReport: currentReport };
|
|
70
|
+
}
|
|
71
|
+
const scopedChangedScope = changedRegionScope(scopedOptions);
|
|
72
|
+
const scopedChangedResult = filterChangedFindings(run.baselineResult.findings, scopedChangedScope, run.scanned.sources);
|
|
73
|
+
const scopedReport = reportFromRun(run, scopedOptions, { ...run.baselineResult, findings: scopedChangedResult.findings }, scopedChangedResult.suppressedCount);
|
|
74
|
+
return { currentReport, scopedReport };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Loads config and discovers inputs once so direct analysis and hook reuse share the same setup.
|
|
78
|
+
function prepareAnalysis(options: AnalysisOptions): AnalysisPreparation {
|
|
39
79
|
const projectRoot = cwd();
|
|
40
80
|
const config = loadConfig(projectRoot, options);
|
|
41
81
|
const diagnostics: RunDiagnostic[] = [];
|
|
42
82
|
const discovery = discoverSources(projectRoot, options, config);
|
|
43
|
-
|
|
83
|
+
return { projectRoot, config, diagnostics, discovery };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Runs per-file and project-level rules before applying baseline suppression; finding order remains stable.
|
|
87
|
+
function completeAnalysis(preparation: AnalysisPreparation, options: AnalysisOptions): AnalysisRun {
|
|
88
|
+
const { projectRoot, config, diagnostics, discovery } = preparation;
|
|
44
89
|
pushMissingPathDiagnostics(discovery.missingPaths, diagnostics);
|
|
45
90
|
|
|
46
91
|
const scanned = scanDiscoveredSources(discovery.files, config, diagnostics);
|
|
47
92
|
const allFindings = sortedUniqueFindings([
|
|
48
93
|
...scanned.findings,
|
|
49
|
-
...analyseProjectIndex(scanned.projectSources, config).filter((finding) => ruleEnabled(config, finding.ruleId)),
|
|
94
|
+
...analyseProjectIndex(projectRoot, options, discovery.files, scanned.projectSources, config).filter((finding) => ruleEnabled(config, finding.ruleId)),
|
|
50
95
|
]);
|
|
51
96
|
const baselineResult = applyBaselineOptions(projectRoot, options, allFindings);
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
recordHistory(projectRoot, options.historyFile, changedResult.findings, diagnostics);
|
|
56
|
-
}
|
|
97
|
+
const notes = [...discovery.notes, ...scanned.notes];
|
|
98
|
+
return { projectRoot, discovery, diagnostics, scanned, baselineResult, notes };
|
|
99
|
+
}
|
|
57
100
|
|
|
58
|
-
|
|
101
|
+
// Converts a completed run into a stable report contract or changed-region projection.
|
|
102
|
+
function reportFromRun(run: AnalysisRun, options: AnalysisOptions, baselineResult: BaselineApplication, suppressedCount?: number): AnalysisReport {
|
|
103
|
+
return buildAnalysisReport(run.projectRoot, options, run.discovery, run.diagnostics, baselineResult, run.notes, suppressedCount);
|
|
59
104
|
}
|
|
60
105
|
|
|
61
106
|
function buildAnalysisReport(
|
|
@@ -64,6 +109,7 @@ function buildAnalysisReport(
|
|
|
64
109
|
discovery: DiscoverySummary,
|
|
65
110
|
diagnostics: RunDiagnostic[],
|
|
66
111
|
baselineResult: BaselineApplication,
|
|
112
|
+
notes: ScanSurfaceNote[],
|
|
67
113
|
suppressedCount?: number,
|
|
68
114
|
): AnalysisReport {
|
|
69
115
|
const findings = baselineResult.findings;
|
|
@@ -84,6 +130,7 @@ function buildAnalysisReport(
|
|
|
84
130
|
missingPaths: discovery.missingPaths,
|
|
85
131
|
},
|
|
86
132
|
diagnostics,
|
|
133
|
+
...(notes.length === 0 ? {} : { notes }),
|
|
87
134
|
findings,
|
|
88
135
|
...(suppressedCount === undefined ? {} : { suppressedCount }),
|
|
89
136
|
score: scoreReport(findings),
|
|
@@ -98,31 +145,37 @@ interface DiscoverySummary {
|
|
|
98
145
|
ignoredPaths: string[];
|
|
99
146
|
skipped: SkippedPath[];
|
|
100
147
|
missingPaths: string[];
|
|
148
|
+
notes: ScanSurfaceNote[];
|
|
101
149
|
}
|
|
102
150
|
|
|
103
151
|
// Output of the per-file scan pass - both the findings produced and the cached source bodies that
|
|
104
152
|
// later project-level rules need to operate against the deterministic stable shape used by baselines.
|
|
153
|
+
// Contract invariant: `notes` records bounded deep scans without changing finding order.
|
|
105
154
|
interface SourceScanResult {
|
|
106
155
|
findings: Finding[];
|
|
107
156
|
projectSources: ProjectSource[];
|
|
108
157
|
sources: Map<string, { file: SourceFile; source: string }>;
|
|
158
|
+
notes: ScanSurfaceNote[];
|
|
109
159
|
}
|
|
110
160
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
baseline?: NonNullable<AnalysisReport["baseline"]>;
|
|
161
|
+
// Prepared inputs for one analysis run. Discovery stays separate from scanning so hook mode can
|
|
162
|
+
// reuse setup without re-reading config.
|
|
163
|
+
interface AnalysisPreparation {
|
|
164
|
+
projectRoot: string;
|
|
165
|
+
config: Config;
|
|
166
|
+
diagnostics: RunDiagnostic[];
|
|
167
|
+
discovery: DiscoverySummary;
|
|
119
168
|
}
|
|
120
169
|
|
|
121
|
-
//
|
|
122
|
-
//
|
|
123
|
-
interface
|
|
124
|
-
|
|
125
|
-
|
|
170
|
+
// Completed scan state before final report rendering. The baseline result is carried separately so
|
|
171
|
+
// changed-region filtering can project findings without losing baseline metadata.
|
|
172
|
+
interface AnalysisRun {
|
|
173
|
+
projectRoot: string;
|
|
174
|
+
discovery: DiscoverySummary;
|
|
175
|
+
diagnostics: RunDiagnostic[];
|
|
176
|
+
scanned: SourceScanResult;
|
|
177
|
+
baselineResult: BaselineApplication;
|
|
178
|
+
notes: ScanSurfaceNote[];
|
|
126
179
|
}
|
|
127
180
|
|
|
128
181
|
// Emits a `missing-path` diagnostic per path that the user requested but discovery could not
|
|
@@ -146,15 +199,21 @@ function scanDiscoveredSources(files: SourceFile[], config: Config, diagnostics:
|
|
|
146
199
|
const findings: Finding[] = [];
|
|
147
200
|
const projectSources: ProjectSource[] = [];
|
|
148
201
|
const sources = new Map<string, { file: SourceFile; source: string }>();
|
|
202
|
+
const notes: ScanSurfaceNote[] = [];
|
|
149
203
|
for (const file of files) {
|
|
150
204
|
try {
|
|
151
205
|
const source = readFileSync(file.absolutePath, "utf8");
|
|
152
206
|
sources.set(file.displayPath, { file, source });
|
|
153
|
-
|
|
154
|
-
|
|
207
|
+
const budgetNote = deepScanBudgetNote(file, source);
|
|
208
|
+
if (budgetNote) {
|
|
209
|
+
notes.push(budgetNote);
|
|
210
|
+
} else {
|
|
211
|
+
if (shouldRetainProjectSource(file, source)) {
|
|
212
|
+
projectSources.push(projectSource(file, source));
|
|
213
|
+
}
|
|
214
|
+
diagnostics.push(...parseDiagnostics(file, source));
|
|
155
215
|
}
|
|
156
|
-
|
|
157
|
-
findings.push(...analyseSource(file, source, config));
|
|
216
|
+
findings.push(...analyseSource(file, source, config, budgetNote === undefined));
|
|
158
217
|
} catch (error) {
|
|
159
218
|
diagnostics.push({
|
|
160
219
|
diagnosticType: "read-error",
|
|
@@ -164,7 +223,30 @@ function scanDiscoveredSources(files: SourceFile[], config: Config, diagnostics:
|
|
|
164
223
|
});
|
|
165
224
|
}
|
|
166
225
|
}
|
|
167
|
-
return { findings, projectSources, sources };
|
|
226
|
+
return { findings, projectSources, sources, notes };
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// Scan budget for deep (masking, block-parsing, AST-walking) script analysis. Copied perf-test
|
|
230
|
+
// fixtures of 100k+ lines time out whole scans, so files above either bound keep text-level rules.
|
|
231
|
+
const DEEP_SCAN_MAX_LINES = 20_000; // Budget limit: bounds copied perf fixtures before deep passes.
|
|
232
|
+
const DEEP_SCAN_MAX_BYTES = 2_000_000; // Budget limit: catches minified/long-line scripts too.
|
|
233
|
+
|
|
234
|
+
// Returns the bounded-deep-scan note when a script file exceeds the budget, undefined otherwise.
|
|
235
|
+
// The file still counts as analysed - silence is the failure mode this guard exists to remove.
|
|
236
|
+
function deepScanBudgetNote(file: SourceFile, source: string): ScanSurfaceNote | undefined {
|
|
237
|
+
if (!file.isScript) {
|
|
238
|
+
return undefined;
|
|
239
|
+
}
|
|
240
|
+
const lines = lineCount(source);
|
|
241
|
+
const bytes = Buffer.byteLength(source, "utf8");
|
|
242
|
+
if (lines <= DEEP_SCAN_MAX_LINES && bytes <= DEEP_SCAN_MAX_BYTES) {
|
|
243
|
+
return undefined;
|
|
244
|
+
}
|
|
245
|
+
return {
|
|
246
|
+
noteType: "bounded-deep-scan",
|
|
247
|
+
path: file.displayPath,
|
|
248
|
+
message: `File exceeds the deep-scan budget (${lines} lines, ${bytes} bytes; limits ${DEEP_SCAN_MAX_LINES} lines / ${DEEP_SCAN_MAX_BYTES} bytes). Text-level rules (size, sensitive-data, config) still ran; deep script analysis was skipped.`,
|
|
249
|
+
};
|
|
168
250
|
}
|
|
169
251
|
|
|
170
252
|
// Retains production files for exported-surface checks, tests for central-suite import coverage,
|
|
@@ -200,90 +282,252 @@ function sortedUniqueFindings(findings: Finding[]): Finding[] {
|
|
|
200
282
|
return dedupeFindings(findings);
|
|
201
283
|
}
|
|
202
284
|
|
|
285
|
+
// Per-file rule pipeline. Text rules run on every file (including config/yaml); TypeScript rules
|
|
286
|
+
// run only on scripts within the deep-scan budget. Fixed order is part of the stable fingerprint
|
|
287
|
+
// contract. Generated/copied files keep every security and sensitive-data finding but drop
|
|
288
|
+
// documentation and naming findings - generated code is not maintainer-authored source, so pushing
|
|
289
|
+
// doc/naming work at a human reviewer is unactionable noise.
|
|
290
|
+
// Contract invariant: doc/naming skips must not suppress safety pillars or change rule order.
|
|
291
|
+
function analyseSource(file: SourceFile, source: string, config: Config, isWithinDeepScanBudget: boolean): Finding[] {
|
|
292
|
+
const findings: Finding[] = [];
|
|
293
|
+
analyseTextRules(file, source, config, findings);
|
|
294
|
+
if (file.isScript && isWithinDeepScanBudget) {
|
|
295
|
+
analyseTypeScriptRules(file, source, config, findings);
|
|
296
|
+
}
|
|
297
|
+
const isGenerated = isGeneratedSource(source);
|
|
298
|
+
return findings.filter((finding) => ruleEnabled(config, finding.ruleId) && !(isGenerated && GENERATED_SKIPPED_PILLARS.has(finding.pillar)));
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// D6 generated-surface policy: docs/naming findings are skipped for generated files because no
|
|
302
|
+
// maintainer authors that text; every other pillar (security, sensitive-data, size, complexity,
|
|
303
|
+
// test-quality...) still applies - a generated file can leak a real credential.
|
|
304
|
+
const GENERATED_SKIPPED_PILLARS = new Set<Pillar>(["documentation", "naming"]);
|
|
305
|
+
|
|
306
|
+
// Marker-based generated/copied classification over the file's opening lines. Deliberately
|
|
307
|
+
// requires an explicit marker rather than path hints - a `fixtures/` path alone must never change
|
|
308
|
+
// rule behaviour, and `isDefaultIgnoredDir` stays the only directory-level exclusion surface.
|
|
309
|
+
function isGeneratedSource(source: string): boolean {
|
|
310
|
+
return source.split(/\r?\n/, 10).some((line) => /AUTO-?GENERATED|@generated\b|GENERATED FILE|Code generated|Generated by|DO NOT EDIT|Copied from/i.test(line));
|
|
311
|
+
}
|
|
312
|
+
|
|
203
313
|
/*
|
|
204
|
-
*
|
|
205
|
-
*
|
|
206
|
-
*
|
|
314
|
+
* Cross-file rule pipeline. Contract invariant: scoped runs keep `paths.analysedFiles` scoped while
|
|
315
|
+
* circular-import may build root graph context so cycles through requested files stay visible. It
|
|
316
|
+
* swallows hidden root-context read failures in `graphProjectSources` so unrelated unreadable files
|
|
317
|
+
* do not break a narrow scan.
|
|
207
318
|
*/
|
|
208
|
-
function
|
|
209
|
-
|
|
210
|
-
|
|
319
|
+
function analyseProjectIndex(projectRoot: string, options: AnalysisOptions, scopedFiles: SourceFile[], projectSources: ProjectSource[], config: Config): Finding[] {
|
|
320
|
+
const shouldUseRootCircularContext = ruleEnabled(config, CIRCULAR_IMPORT_RULE_ID) && shouldBuildRootCircularContext(projectRoot, options, scopedFiles);
|
|
321
|
+
const shouldUseScopedIndex = isAnyRuleEnabled(config, SCOPED_PROJECT_INDEX_RULE_IDS) || (ruleEnabled(config, CIRCULAR_IMPORT_RULE_ID) && !shouldUseRootCircularContext);
|
|
322
|
+
if (!shouldUseScopedIndex && !shouldUseRootCircularContext) {
|
|
323
|
+
return [];
|
|
211
324
|
}
|
|
212
|
-
|
|
213
|
-
if (
|
|
214
|
-
|
|
325
|
+
const findings: Finding[] = [];
|
|
326
|
+
if (shouldUseScopedIndex) {
|
|
327
|
+
const index = buildProjectIndex(projectSources);
|
|
328
|
+
analyseArchitectureRules(index, config, findings, { shouldSkipCircularImports: shouldUseRootCircularContext });
|
|
329
|
+
}
|
|
330
|
+
if (shouldUseRootCircularContext) {
|
|
331
|
+
findings.push(...rootCircularImportFindings(projectRoot, options, scopedFiles, config));
|
|
215
332
|
}
|
|
333
|
+
return findings;
|
|
334
|
+
}
|
|
216
335
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
336
|
+
const PROJECT_INDEX_RULE_IDS = [
|
|
337
|
+
"design.deep-relative-import",
|
|
338
|
+
CIRCULAR_IMPORT_RULE_ID,
|
|
339
|
+
"design.large-module-concentration",
|
|
340
|
+
] as const;
|
|
341
|
+
const SCOPED_PROJECT_INDEX_RULE_IDS = [
|
|
342
|
+
"design.deep-relative-import",
|
|
343
|
+
"design.large-module-concentration",
|
|
344
|
+
] as const;
|
|
345
|
+
|
|
346
|
+
// Root graph context is needed only for narrow path operands; full-root scans already have context.
|
|
347
|
+
function shouldBuildRootCircularContext(projectRoot: string, options: AnalysisOptions, scopedFiles: SourceFile[]): boolean {
|
|
348
|
+
if (scopedFiles.length === 0 || options.paths.length === 0) {
|
|
349
|
+
return false;
|
|
220
350
|
}
|
|
351
|
+
return options.paths.every((input) => displayPath(projectRoot, absolutize(projectRoot, input)) !== ".");
|
|
352
|
+
}
|
|
221
353
|
|
|
222
|
-
|
|
354
|
+
// Builds stable circular-import findings from the repository root, then filters back to requested files.
|
|
355
|
+
function rootCircularImportFindings(projectRoot: string, options: AnalysisOptions, scopedFiles: SourceFile[], config: Config): Finding[] {
|
|
356
|
+
const requestedFiles = new Set(scopedFiles.map((file) => file.displayPath));
|
|
357
|
+
const rootDiscovery = discoverSources(projectRoot, { ...options, paths: [] }, config);
|
|
358
|
+
const rootProjectSources = graphProjectSources(rootDiscovery.files);
|
|
359
|
+
const findings: Finding[] = [];
|
|
360
|
+
analyseCircularImportRule(buildProjectIndex(rootProjectSources), findings);
|
|
361
|
+
return findings.filter((finding) => circularImportFindingTouchesRequestedFile(finding, requestedFiles));
|
|
223
362
|
}
|
|
224
363
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
},
|
|
240
|
-
};
|
|
364
|
+
// Reads just the root files needed for import graph context and swallows unrelated read failures.
|
|
365
|
+
function graphProjectSources(files: SourceFile[]): ProjectSource[] {
|
|
366
|
+
const projectSources: ProjectSource[] = [];
|
|
367
|
+
for (const file of files) {
|
|
368
|
+
try {
|
|
369
|
+
const source = readFileSync(file.absolutePath, "utf8");
|
|
370
|
+
if (!deepScanBudgetNote(file, source) && shouldRetainProjectSource(file, source)) {
|
|
371
|
+
projectSources.push(projectSource(file, source));
|
|
372
|
+
}
|
|
373
|
+
} catch {
|
|
374
|
+
// ignore hidden root-context read failures; scoped scans still report requested-file errors.
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
return projectSources;
|
|
241
378
|
}
|
|
242
379
|
|
|
243
|
-
//
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
const filteredFindings = applyBaseline(selected.path, findings);
|
|
248
|
-
return {
|
|
249
|
-
findings: filteredFindings,
|
|
250
|
-
baseline: {
|
|
251
|
-
path: displayPath(projectRoot, selected.path),
|
|
252
|
-
source: selected.source,
|
|
253
|
-
suppressed: before - filteredFindings.length,
|
|
254
|
-
generated: false,
|
|
255
|
-
},
|
|
256
|
-
};
|
|
380
|
+
// Contract filter: root-context circular findings survive only when the SCC contains a requested file.
|
|
381
|
+
function circularImportFindingTouchesRequestedFile(finding: Finding, requestedFiles: Set<string>): boolean {
|
|
382
|
+
const files = Array.isArray(finding.metadata.files) ? finding.metadata.files : [finding.filePath];
|
|
383
|
+
return files.some((file) => typeof file === "string" && requestedFiles.has(file));
|
|
257
384
|
}
|
|
258
385
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
386
|
+
const RULE_IDS_BY_PILLAR = ruleIdsByPillar();
|
|
387
|
+
const COMPLEXITY_RULE_IDS = ruleIdsForPillar("complexity");
|
|
388
|
+
const DEAD_CODE_RULE_IDS = ruleIdsForPillar("dead-code");
|
|
389
|
+
const DOCUMENTATION_RULE_IDS = ruleIdsForPillar("documentation");
|
|
390
|
+
const MAINTAINABILITY_RULE_IDS = ruleIdsForPillar("maintainability");
|
|
391
|
+
const MODERNISATION_RULE_IDS = ruleIdsForPillar("modernisation");
|
|
392
|
+
const NAMING_RULE_IDS = ruleIdsForPillar("naming");
|
|
393
|
+
const SECURITY_RULE_IDS = ruleIdsForPillar("security");
|
|
394
|
+
const SENSITIVE_DATA_RULE_IDS = ruleIdsForPillar("sensitive-data");
|
|
395
|
+
const SIZE_RULE_IDS = ruleIdsForPillar("size");
|
|
396
|
+
const TEST_QUALITY_RULE_IDS = ruleIdsForPillar("test-quality");
|
|
397
|
+
|
|
398
|
+
const GITHUB_ACTIONS_RULE_IDS = [
|
|
399
|
+
"security.github-actions-broad-permissions",
|
|
400
|
+
"security.github-actions-pull-request-target",
|
|
401
|
+
"security.github-actions-remote-shell",
|
|
402
|
+
"security.github-actions-secrets-in-pr",
|
|
403
|
+
"security.github-actions-unpinned-action",
|
|
404
|
+
] as const;
|
|
405
|
+
|
|
406
|
+
const PROJECT_CONFIG_RULE_IDS = [
|
|
407
|
+
"security.remote-install-script",
|
|
408
|
+
"security.risky-lifecycle-script",
|
|
409
|
+
"security.url-dependency",
|
|
410
|
+
"waste.broad-runtime-version",
|
|
411
|
+
"design.package-bin-missing",
|
|
412
|
+
"design.package-bin-not-executable",
|
|
413
|
+
] as const;
|
|
414
|
+
|
|
415
|
+
const BLOCK_RULE_IDS = [
|
|
416
|
+
"size.function-length",
|
|
417
|
+
"size.parameter-count",
|
|
418
|
+
"complexity.cyclomatic",
|
|
419
|
+
"complexity.cognitive",
|
|
420
|
+
"naming.generic-function",
|
|
421
|
+
"docs.missing-exported-function-doc",
|
|
422
|
+
"docs.missing-internal-function-doc",
|
|
423
|
+
"waste.empty-function",
|
|
424
|
+
"waste.unused-parameter",
|
|
425
|
+
"waste.redundant-variable",
|
|
426
|
+
"waste.useless-return",
|
|
427
|
+
] as const;
|
|
428
|
+
|
|
429
|
+
const PARAMETER_NAMING_RULE_IDS = [
|
|
430
|
+
"naming.boolean-prefix",
|
|
431
|
+
"naming.generic-parameter",
|
|
432
|
+
"naming.identifier-quality",
|
|
433
|
+
"naming.negative-boolean",
|
|
434
|
+
"naming.short-variable",
|
|
435
|
+
] as const;
|
|
436
|
+
|
|
437
|
+
const DOCBLOCK_RULE_IDS = [
|
|
438
|
+
"docs.missing-param-tag",
|
|
439
|
+
"docs.missing-return-tag",
|
|
440
|
+
"docs.stale-param-tag",
|
|
441
|
+
"docs.useless-docblock",
|
|
442
|
+
] as const;
|
|
443
|
+
|
|
444
|
+
const INTERFACE_FIELD_RULE_IDS = [
|
|
445
|
+
"naming.boolean-prefix",
|
|
446
|
+
"naming.negative-boolean",
|
|
447
|
+
] as const;
|
|
448
|
+
|
|
449
|
+
// Every rule id `analyseCommentQualityRules` can emit - including `docs.fixture-purpose-missing`
|
|
450
|
+
// via `pushFixturePurposeFindings` - so the group gate never silences an enabled rule.
|
|
451
|
+
const COMMENT_QUALITY_RULE_IDS = [
|
|
452
|
+
"docs.fixture-purpose-missing",
|
|
453
|
+
"docs.magic-threshold-without-rationale",
|
|
454
|
+
"docs.missing-error-behavior-doc",
|
|
455
|
+
"docs.missing-invariant-doc",
|
|
456
|
+
"docs.missing-side-effect-doc",
|
|
457
|
+
"docs.missing-why-for-complex-code",
|
|
458
|
+
"docs.stale-comment",
|
|
459
|
+
"docs.suppression-without-rationale",
|
|
460
|
+
"docs.todo-without-tracking",
|
|
461
|
+
"docs.useless-docblock",
|
|
462
|
+
] as const;
|
|
463
|
+
|
|
464
|
+
const CLASS_RULE_IDS = [
|
|
465
|
+
"docs.missing-public-doc",
|
|
466
|
+
"naming.class-file-mismatch",
|
|
467
|
+
"modernisation.public-property",
|
|
468
|
+
"modernisation.readonly-property-candidate",
|
|
469
|
+
] as const;
|
|
470
|
+
|
|
471
|
+
const IDENTIFIER_INVENTORY_RULE_IDS = [
|
|
472
|
+
"naming.acronym-case",
|
|
473
|
+
"naming.inconsistent-casing",
|
|
474
|
+
] as const;
|
|
475
|
+
|
|
476
|
+
const SECURITY_FLOW_RULE_IDS = [
|
|
477
|
+
"security.dynamic-regexp",
|
|
478
|
+
"security.open-redirect-candidate",
|
|
479
|
+
"security.path-traversal-candidate",
|
|
480
|
+
"security.ssrf-candidate",
|
|
481
|
+
"security.unsafe-deserialization",
|
|
482
|
+
"security.xxe-candidate",
|
|
483
|
+
] as const;
|
|
484
|
+
|
|
485
|
+
const LINE_RULE_IDS = [
|
|
486
|
+
...SECURITY_RULE_IDS,
|
|
487
|
+
...MAINTAINABILITY_RULE_IDS,
|
|
488
|
+
...MODERNISATION_RULE_IDS,
|
|
489
|
+
...NAMING_RULE_IDS,
|
|
490
|
+
];
|
|
491
|
+
|
|
492
|
+
const BLOCK_DEPENDENT_RULE_IDS = [
|
|
493
|
+
...BLOCK_RULE_IDS,
|
|
494
|
+
...PARAMETER_NAMING_RULE_IDS,
|
|
495
|
+
...TEST_QUALITY_RULE_IDS,
|
|
496
|
+
...COMMENT_QUALITY_RULE_IDS,
|
|
497
|
+
...IDENTIFIER_INVENTORY_RULE_IDS,
|
|
498
|
+
];
|
|
499
|
+
|
|
500
|
+
const TYPESCRIPT_RULE_IDS = [
|
|
501
|
+
...COMPLEXITY_RULE_IDS,
|
|
502
|
+
...DEAD_CODE_RULE_IDS,
|
|
503
|
+
...DOCUMENTATION_RULE_IDS,
|
|
504
|
+
...MAINTAINABILITY_RULE_IDS,
|
|
505
|
+
...MODERNISATION_RULE_IDS,
|
|
506
|
+
...NAMING_RULE_IDS,
|
|
507
|
+
...SECURITY_RULE_IDS,
|
|
508
|
+
...SIZE_RULE_IDS,
|
|
509
|
+
...TEST_QUALITY_RULE_IDS,
|
|
510
|
+
];
|
|
511
|
+
|
|
512
|
+
// Groups rule ids from the public catalogue once so broad pass gates stay aligned with profiles.
|
|
513
|
+
function ruleIdsByPillar(): ReadonlyMap<Pillar, readonly string[]> {
|
|
514
|
+
const byPillar = new Map<Pillar, string[]>();
|
|
515
|
+
for (const descriptor of ruleDescriptors()) {
|
|
516
|
+
const ruleIds = byPillar.get(descriptor.pillar) ?? [];
|
|
517
|
+
ruleIds.push(descriptor.ruleId);
|
|
518
|
+
byPillar.set(descriptor.pillar, ruleIds);
|
|
264
519
|
}
|
|
265
|
-
|
|
266
|
-
return existsSync(defaultBaseline) ? { path: defaultBaseline, source: "default" } : undefined;
|
|
520
|
+
return byPillar;
|
|
267
521
|
}
|
|
268
522
|
|
|
269
|
-
//
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
const findings: Finding[] = [];
|
|
273
|
-
analyseTextRules(file, source, config, findings);
|
|
274
|
-
if (file.isScript) {
|
|
275
|
-
analyseTypeScriptRules(file, source, config, findings);
|
|
276
|
-
}
|
|
277
|
-
return findings.filter((finding) => ruleEnabled(config, finding.ruleId));
|
|
523
|
+
// Looks up a precomputed pillar group; missing groups are empty so callers can stay branch-free.
|
|
524
|
+
function ruleIdsForPillar(pillar: Pillar): readonly string[] {
|
|
525
|
+
return RULE_IDS_BY_PILLAR.get(pillar) ?? [];
|
|
278
526
|
}
|
|
279
527
|
|
|
280
|
-
//
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
const index = buildProjectIndex(projectSources);
|
|
284
|
-
const findings: Finding[] = [];
|
|
285
|
-
analyseArchitectureRules(index, config, findings);
|
|
286
|
-
return findings;
|
|
528
|
+
// Checks a rule-id group through the resolved config map so profiles and explicit overrides share one gate.
|
|
529
|
+
function isAnyRuleEnabled(config: Config, ruleIds: readonly string[]): boolean {
|
|
530
|
+
return ruleIds.some((ruleId) => ruleEnabled(config, ruleId));
|
|
287
531
|
}
|
|
288
532
|
|
|
289
533
|
/*
|
|
@@ -293,7 +537,7 @@ function analyseProjectIndex(projectSources: ProjectSource[], config: Config): F
|
|
|
293
537
|
*/
|
|
294
538
|
function analyseTextRules(file: SourceFile, source: string, config: Config, findings: Finding[]): void {
|
|
295
539
|
const lines = lineCount(source);
|
|
296
|
-
if (!isGeneratedLockfile(file.displayPath)) {
|
|
540
|
+
if (ruleEnabled(config, "size.file-length") && !isGeneratedLockfile(file.displayPath)) {
|
|
297
541
|
const fileLengthThreshold = threshold(config, "size.file-length", 750);
|
|
298
542
|
if (lines > fileLengthThreshold) {
|
|
299
543
|
findings.push(
|
|
@@ -312,9 +556,15 @@ function analyseTextRules(file: SourceFile, source: string, config: Config, find
|
|
|
312
556
|
}
|
|
313
557
|
}
|
|
314
558
|
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
559
|
+
if (isAnyRuleEnabled(config, SENSITIVE_DATA_RULE_IDS)) {
|
|
560
|
+
analyseSensitiveData(file, source, config, findings);
|
|
561
|
+
}
|
|
562
|
+
if (isAnyRuleEnabled(config, GITHUB_ACTIONS_RULE_IDS)) {
|
|
563
|
+
analyseGithubActionsRules(file, source, findings);
|
|
564
|
+
}
|
|
565
|
+
if (isAnyRuleEnabled(config, PROJECT_CONFIG_RULE_IDS)) {
|
|
566
|
+
analyseProjectConfigRules(file, source, findings);
|
|
567
|
+
}
|
|
318
568
|
}
|
|
319
569
|
|
|
320
570
|
// Counts the same logical lines as `source.split(/\r?\n/)` without allocating the full line array.
|
|
@@ -340,37 +590,63 @@ function isGeneratedLockfile(path: string): boolean {
|
|
|
340
590
|
* then walks every rule pack in a stable, deterministic order so reports and baselines remain reproducible.
|
|
341
591
|
*/
|
|
342
592
|
function analyseTypeScriptRules(file: SourceFile, source: string, config: Config, findings: Finding[]): void {
|
|
593
|
+
if (!isAnyRuleEnabled(config, TYPESCRIPT_RULE_IDS)) {
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
343
596
|
const codeSource = maskNonCode(source);
|
|
344
|
-
const blocks = functionBlocks(source, codeSource);
|
|
345
|
-
|
|
346
|
-
analyseFileOverviewDoc(file, source, findings);
|
|
597
|
+
const blocks = isAnyRuleEnabled(config, BLOCK_DEPENDENT_RULE_IDS) ? functionBlocks(source, codeSource) : [];
|
|
598
|
+
runRulePass(config, "docs.missing-file-overview", () => analyseFileOverviewDoc(file, source, findings));
|
|
347
599
|
analyseBlocks(file, source, codeSource, blocks, config, findings);
|
|
348
|
-
analyseUnusedImports(file, codeSource, source, findings);
|
|
349
|
-
analyseLineRules(file, source, codeSource, config, findings);
|
|
350
|
-
analyseSecurityFlow(file, source, findings);
|
|
351
|
-
analyseUnreachable(file, codeSource, findings);
|
|
352
|
-
analyseDocRules(file, source, codeSource, findings);
|
|
353
|
-
analyseInterfaceDocs(file, source, codeSource, findings);
|
|
354
|
-
analyseInterfaceFields(file, source, codeSource, config, findings);
|
|
355
|
-
analyseCommentQualityRules({ file, source, codeSource, blocks, comments, config, findings });
|
|
356
|
-
analyseClassRules(file, source, codeSource, findings);
|
|
357
|
-
analyseDeadCode(file, codeSource, findings);
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
600
|
+
runRulePass(config, "waste.unused-import", () => analyseUnusedImports(file, codeSource, source, findings));
|
|
601
|
+
runRuleGroupPass(config, LINE_RULE_IDS, () => analyseLineRules(file, source, codeSource, config, findings));
|
|
602
|
+
runRuleGroupPass(config, SECURITY_FLOW_RULE_IDS, () => analyseSecurityFlow(file, source, findings));
|
|
603
|
+
runRulePass(config, "waste.unreachable-code", () => analyseUnreachable(file, codeSource, findings));
|
|
604
|
+
runRuleGroupPass(config, DOCBLOCK_RULE_IDS, () => analyseDocRules(file, source, codeSource, findings));
|
|
605
|
+
runRulePass(config, "docs.missing-interface-doc", () => analyseInterfaceDocs(file, source, codeSource, findings));
|
|
606
|
+
runRuleGroupPass(config, INTERFACE_FIELD_RULE_IDS, () => analyseInterfaceFields(file, source, codeSource, config, findings));
|
|
607
|
+
runRuleGroupPass(config, COMMENT_QUALITY_RULE_IDS, () => analyseCommentQualityRules({ file, source, codeSource, blocks, comments: commentRecords(source), config, findings }));
|
|
608
|
+
runRuleGroupPass(config, CLASS_RULE_IDS, () => analyseClassRules(file, source, codeSource, findings));
|
|
609
|
+
runRulePass(config, "dead-code.unused-private-method", () => analyseDeadCode(file, codeSource, findings));
|
|
610
|
+
runRuleGroupPass(config, IDENTIFIER_INVENTORY_RULE_IDS, () => {
|
|
611
|
+
const inventory = collectDeclaredIdentifiers(source, codeSource, blocks);
|
|
612
|
+
runRulePass(config, "naming.inconsistent-casing", () => analyseInconsistentCasing(file, inventory, findings));
|
|
613
|
+
runRulePass(config, "naming.acronym-case", () => analyseAcronymCase(file, inventory, config, findings));
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
// Runs a single-rule pass only when that exact resolved rule id is enabled.
|
|
618
|
+
function runRulePass(config: Config, ruleId: string, action: () => void): void {
|
|
619
|
+
if (ruleEnabled(config, ruleId)) {
|
|
620
|
+
action();
|
|
621
|
+
}
|
|
361
622
|
}
|
|
362
623
|
|
|
624
|
+
// Runs a shared pass only when at least one rule it can emit is enabled.
|
|
625
|
+
function runRuleGroupPass(config: Config, ruleIds: readonly string[], action: () => void): void {
|
|
626
|
+
if (isAnyRuleEnabled(config, ruleIds)) {
|
|
627
|
+
action();
|
|
628
|
+
}
|
|
629
|
+
}
|
|
363
630
|
|
|
364
631
|
// One pass over the file's parsed callables. The naming and test-block fanouts are dispatched
|
|
365
632
|
// separately so blocks.ts can stay independent of the naming-pusher and test-block-rule modules;
|
|
366
633
|
// the per-rule emission order from `analyseBlockRules` is the stable fingerprint contract every
|
|
367
634
|
// Finding depends on for deterministic baseline matching.
|
|
368
635
|
function analyseBlocks(file: SourceFile, source: string, codeSource: string, blocks: FunctionBlock[], config: Config, findings: Finding[]): void {
|
|
636
|
+
const shouldAnalyseBlockRules = isAnyRuleEnabled(config, BLOCK_RULE_IDS);
|
|
637
|
+
const shouldAnalyseParameterNaming = isAnyRuleEnabled(config, PARAMETER_NAMING_RULE_IDS);
|
|
638
|
+
const shouldAnalyseTestQuality = isAnyRuleEnabled(config, TEST_QUALITY_RULE_IDS);
|
|
369
639
|
for (const block of blocks) {
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
640
|
+
let context: BlockRuleContext | undefined;
|
|
641
|
+
if (shouldAnalyseBlockRules) {
|
|
642
|
+
context = blockRuleContext(file, block, config, findings);
|
|
643
|
+
analyseBlockRules(context);
|
|
644
|
+
}
|
|
645
|
+
if (shouldAnalyseParameterNaming) {
|
|
646
|
+
context ??= blockRuleContext(file, block, config, findings);
|
|
647
|
+
pushParameterNamingFindings(context);
|
|
648
|
+
}
|
|
649
|
+
if (shouldAnalyseTestQuality && block.isTest) {
|
|
374
650
|
analyseTestBlock(file, block, findings, { source, codeSource, startLine: 1 });
|
|
375
651
|
}
|
|
376
652
|
}
|