@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.
- package/CHANGELOG.md +123 -1
- package/CONTRIBUTING.md +32 -25
- package/README.md +55 -46
- package/SECURITY.md +18 -30
- package/UPGRADING.md +101 -0
- package/bin/gruff-ts +3 -0
- package/docs/README.md +11 -13
- package/docs/agent-hook.md +33 -24
- package/docs/ci-integration.md +38 -24
- package/docs/configuration.md +173 -71
- 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 -157
- package/docs/rules.md +140 -106
- package/package.json +10 -8
- package/src/analyser.ts +886 -282
- 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 +85 -55
- package/src/blocks.ts +579 -364
- package/src/changed-regions.ts +414 -65
- package/src/check-ignore.ts +28 -13
- package/src/class-rules.ts +461 -302
- package/src/cli-program.ts +956 -166
- package/src/cli.ts +9 -2
- package/src/comment-rules.ts +277 -414
- package/src/comment-scanner.ts +363 -44
- package/src/complexity-metrics.ts +320 -0
- 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 +778 -210
- package/src/constants.ts +7 -6
- package/src/context-doc-rules.ts +188 -61
- package/src/dashboard.ts +88 -23
- package/src/dead-code-rules.ts +153 -74
- package/src/discovery.ts +521 -148
- package/src/doc-rules.ts +592 -147
- package/src/duplicate-test-group-rules.ts +359 -0
- package/src/findings-helpers.ts +240 -89
- package/src/findings.ts +48 -24
- package/src/fixture-purpose-rules.ts +226 -48
- package/src/github-actions-event-guards.ts +331 -0
- package/src/github-actions-rules.ts +336 -106
- package/src/hook-contract.ts +708 -211
- package/src/init-config.ts +169 -114
- package/src/inline-suppressions.ts +340 -0
- package/src/limit-band.ts +56 -0
- package/src/line-rules.ts +331 -419
- package/src/migrate-config.ts +670 -0
- package/src/naming-pushers.ts +33 -107
- package/src/parsed-script.ts +1206 -0
- package/src/pillar-summary.ts +56 -40
- package/src/profiles.ts +97 -71
- package/src/project-config-rules.ts +335 -146
- package/src/project-rules.ts +237 -115
- package/src/public-exports.ts +284 -0
- package/src/report-html.ts +217 -85
- package/src/report-renderers.ts +864 -195
- package/src/rule-list.ts +166 -46
- package/src/rules.ts +100 -104
- package/src/safety-rules.ts +191 -132
- package/src/scoring.ts +255 -52
- package/src/security-flow-rules.ts +930 -109
- package/src/selectors.ts +169 -0
- package/src/sensitive-data-rules.ts +502 -375
- package/src/sensitive-exclusions.ts +435 -0
- package/src/source-text.ts +380 -148
- package/src/static-analysis-redundant-rules.ts +298 -41
- package/src/test-block-rules.ts +422 -228
- package/src/text-scans.ts +67 -9
- package/src/types.ts +222 -71
- package/tsconfig.json +2 -1
- package/src/process-exec-metadata.ts +0 -153
- package/src/test-fixtures.ts +0 -668
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
// Measures syntax-aware cyclomatic and cognitive complexity for each callable in the shared script parse.
|
|
2
|
+
//
|
|
3
|
+
// Feeds `complexity.cyclomatic` findings and the `docs.missing-why-for-complex-code` threshold without re-parsing files.
|
|
4
|
+
// Leaves defaulting (`??`), optional access (`?.`), object literals, and owned nested callables out of the enclosing score.
|
|
5
|
+
import { createRequire } from "node:module";
|
|
6
|
+
|
|
7
|
+
// TypeScript is CommonJS at runtime; this module only walks nodes from the existing shared parse.
|
|
8
|
+
const require = createRequire(import.meta.url);
|
|
9
|
+
const typescriptSyntax = require("typescript") as typeof import("typescript");
|
|
10
|
+
|
|
11
|
+
// Syntax node identity links measurements to the callable inventory from the shared parser.
|
|
12
|
+
// Nodes never leave the internal scan pipeline or appear in JSON and UI report payloads.
|
|
13
|
+
// Equality is object identity within one parse, which preserves single ownership.
|
|
14
|
+
type TypeScriptNode = import("typescript").Node;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Stable per-kind decision counts attached to complexity findings for CLI and JSON report consumers.
|
|
18
|
+
*
|
|
19
|
+
* Every key is always emitted (including zeros) so JSON snapshots stay deterministic:
|
|
20
|
+
* - `case` counts one decision per switch with a non-default case, regardless of case count;
|
|
21
|
+
* - `maxNesting` measures control-flow nesting only, never object or callback braces.
|
|
22
|
+
*/
|
|
23
|
+
export interface ComplexityBreakdown {
|
|
24
|
+
if: number;
|
|
25
|
+
loop: number;
|
|
26
|
+
catch: number;
|
|
27
|
+
case: number;
|
|
28
|
+
ternary: number;
|
|
29
|
+
logicalAnd: number;
|
|
30
|
+
logicalOr: number;
|
|
31
|
+
maxNesting: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* One callable's shared complexity result used by both findings and documentation checks.
|
|
36
|
+
* Cyclomatic starts at one; cognitive adds maximum control-flow nesting to that value.
|
|
37
|
+
* A CLI user reaches these measurements when a configured threshold produces a finding.
|
|
38
|
+
*/
|
|
39
|
+
export interface ComplexityMetrics {
|
|
40
|
+
cyclomatic: number;
|
|
41
|
+
cognitive: number;
|
|
42
|
+
maximumControlFlowNesting: number;
|
|
43
|
+
breakdown: ComplexityBreakdown;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Mutable counters used only during one syntax-tree walk for one reported callable.
|
|
48
|
+
* The accumulator is converted to an immutable report-shaped result after traversal.
|
|
49
|
+
* No state survives between files or CLI scans.
|
|
50
|
+
*/
|
|
51
|
+
interface ComplexityAccumulator {
|
|
52
|
+
ifCount: number;
|
|
53
|
+
loopCount: number;
|
|
54
|
+
catchCount: number;
|
|
55
|
+
caseCount: number;
|
|
56
|
+
ternaryCount: number;
|
|
57
|
+
logicalAndCount: number;
|
|
58
|
+
logicalOrCount: number;
|
|
59
|
+
maximumControlFlowNesting: number;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Context threaded through the recursive walk so nested callable bodies keep one owner.
|
|
64
|
+
* `callableNode` is the node being measured; `ownedCallableNodes` includes nested callables measured separately.
|
|
65
|
+
* The shared accumulator becomes metadata visible to report and hook consumers.
|
|
66
|
+
*/
|
|
67
|
+
interface ComplexityWalkContext {
|
|
68
|
+
callableNode: TypeScriptNode;
|
|
69
|
+
ownedCallableNodes: ReadonlySet<TypeScriptNode>;
|
|
70
|
+
accumulator: ComplexityAccumulator;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Base measurement used only when a caller has no parsed script, such as a legacy span probe.
|
|
75
|
+
*
|
|
76
|
+
* @returns Base complexity metrics with cyclomatic and cognitive complexity of 1, 0 nesting, and a zeroed breakdown.
|
|
77
|
+
*/
|
|
78
|
+
export function baseComplexityMetrics(): ComplexityMetrics {
|
|
79
|
+
return {
|
|
80
|
+
cyclomatic: 1,
|
|
81
|
+
cognitive: 1,
|
|
82
|
+
maximumControlFlowNesting: 0,
|
|
83
|
+
breakdown: emptyBreakdown(),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Measures the supplied parsed callable for complexity findings and documentation checks.
|
|
89
|
+
*
|
|
90
|
+
* @param callableNode - Root parsed callable syntax node to measure.
|
|
91
|
+
* @param ownedCallableNodes - Ownership set identifying separately reported nested callables to skip.
|
|
92
|
+
* @returns Cyclomatic and cognitive complexity, maximum nesting, and a breakdown of the counts.
|
|
93
|
+
*/
|
|
94
|
+
export function complexityMetrics(callableNode: TypeScriptNode, ownedCallableNodes: ReadonlySet<TypeScriptNode>): ComplexityMetrics {
|
|
95
|
+
const accumulator: ComplexityAccumulator = {
|
|
96
|
+
ifCount: 0,
|
|
97
|
+
loopCount: 0,
|
|
98
|
+
catchCount: 0,
|
|
99
|
+
caseCount: 0,
|
|
100
|
+
ternaryCount: 0,
|
|
101
|
+
logicalAndCount: 0,
|
|
102
|
+
logicalOrCount: 0,
|
|
103
|
+
maximumControlFlowNesting: 0,
|
|
104
|
+
};
|
|
105
|
+
const context: ComplexityWalkContext = { callableNode, ownedCallableNodes, accumulator };
|
|
106
|
+
// The root callable owns its parameters and body; separately reported nested callables are pruned below.
|
|
107
|
+
callableNode.forEachChild((child) => walkComplexityNode(child, 0, context));
|
|
108
|
+
const decisionCount = accumulator.ifCount
|
|
109
|
+
+ accumulator.loopCount
|
|
110
|
+
+ accumulator.catchCount
|
|
111
|
+
+ accumulator.caseCount
|
|
112
|
+
+ accumulator.ternaryCount
|
|
113
|
+
+ accumulator.logicalAndCount
|
|
114
|
+
+ accumulator.logicalOrCount;
|
|
115
|
+
const cyclomatic = decisionCount + 1;
|
|
116
|
+
const breakdown: ComplexityBreakdown = {
|
|
117
|
+
if: accumulator.ifCount,
|
|
118
|
+
loop: accumulator.loopCount,
|
|
119
|
+
catch: accumulator.catchCount,
|
|
120
|
+
case: accumulator.caseCount,
|
|
121
|
+
ternary: accumulator.ternaryCount,
|
|
122
|
+
logicalAnd: accumulator.logicalAndCount,
|
|
123
|
+
logicalOr: accumulator.logicalOrCount,
|
|
124
|
+
maxNesting: accumulator.maximumControlFlowNesting,
|
|
125
|
+
};
|
|
126
|
+
return {
|
|
127
|
+
cyclomatic,
|
|
128
|
+
cognitive: cyclomatic + accumulator.maximumControlFlowNesting,
|
|
129
|
+
maximumControlFlowNesting: accumulator.maximumControlFlowNesting,
|
|
130
|
+
breakdown,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Measures the supplied syntax node and its owned descendants from the current control-flow depth,
|
|
136
|
+
* pruning reported nested callables to preserve separate totals.
|
|
137
|
+
*
|
|
138
|
+
* @param node - Current syntax node being visited during traversal.
|
|
139
|
+
* @param controlFlowDepth - Current control-flow nesting depth at this node.
|
|
140
|
+
* @param context - Walk state carrying the root callable, ownership set, and mutable accumulator.
|
|
141
|
+
* @returns Nothing. Updates counts and maximum nesting in the walk context.
|
|
142
|
+
*/
|
|
143
|
+
function walkComplexityNode(node: TypeScriptNode, controlFlowDepth: number, context: ComplexityWalkContext): void {
|
|
144
|
+
// A separately reported nested callable contributes to its own measurements.
|
|
145
|
+
if (node !== context.callableNode && context.ownedCallableNodes.has(node)) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
// An if adds one decision; an early-exit guard keeps the current nesting level.
|
|
149
|
+
if (typescriptSyntax.isIfStatement(node)) {
|
|
150
|
+
walkIfStatement(node, controlFlowDepth, context);
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
// Every loop form adds one decision and one control-flow nesting level.
|
|
154
|
+
if (isLoopStatement(node)) {
|
|
155
|
+
context.accumulator.loopCount += 1;
|
|
156
|
+
walkNestedControlFlow(node, controlFlowDepth, context);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
// A switch adds one nesting level. A switch with any case clause also adds one dispatch decision.
|
|
160
|
+
if (typescriptSyntax.isSwitchStatement(node)) {
|
|
161
|
+
if (node.caseBlock.clauses.some((clause) => typescriptSyntax.isCaseClause(clause))) {
|
|
162
|
+
context.accumulator.caseCount += 1;
|
|
163
|
+
}
|
|
164
|
+
walkNestedControlFlow(node, controlFlowDepth, context);
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
// Catch represents one recovery path and one control-flow nesting level.
|
|
168
|
+
if (typescriptSyntax.isCatchClause(node)) {
|
|
169
|
+
context.accumulator.catchCount += 1;
|
|
170
|
+
walkNestedControlFlow(node, controlFlowDepth, context);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
// A conditional expression adds one decision and one control-flow nesting level.
|
|
174
|
+
if (typescriptSyntax.isConditionalExpression(node)) {
|
|
175
|
+
context.accumulator.ternaryCount += 1;
|
|
176
|
+
walkNestedControlFlow(node, controlFlowDepth, context);
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
// Logical AND and OR add decisions at the current nesting depth.
|
|
180
|
+
if (typescriptSyntax.isBinaryExpression(node)) {
|
|
181
|
+
walkBinaryExpression(node, controlFlowDepth, context);
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
// Optional access, nullish coalescing, ordinary blocks, and presentation syntax keep the same depth.
|
|
185
|
+
walkChildrenAtDepth(node, controlFlowDepth, context);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Records the supplied if statement and its branches in the walk context's complexity totals.
|
|
190
|
+
* Uses the incoming depth for an early-exit guard and else-if peers; other branch bodies add a nesting level.
|
|
191
|
+
*
|
|
192
|
+
* @param node - Parsed `if` statement whose condition and branches are visited.
|
|
193
|
+
* @param controlFlowDepth - Incoming control-flow nesting depth before entering this statement.
|
|
194
|
+
* @param context - Walk state holding the mutable complexity accumulator and ownership set.
|
|
195
|
+
* @returns Nothing. Increments `ifCount` and recurses through the condition, then, and else branches.
|
|
196
|
+
*/
|
|
197
|
+
function walkIfStatement(node: import("typescript").IfStatement, controlFlowDepth: number, context: ComplexityWalkContext): void {
|
|
198
|
+
context.accumulator.ifCount += 1;
|
|
199
|
+
const nestedDepth = isEarlyExitGuard(node) ? controlFlowDepth : enterControlFlow(controlFlowDepth, context.accumulator);
|
|
200
|
+
walkComplexityNode(node.expression, nestedDepth, context);
|
|
201
|
+
walkComplexityNode(node.thenStatement, nestedDepth, context);
|
|
202
|
+
if (!node.elseStatement) {
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
// Visit an else-if at the incoming depth so the chain stays a set of peer decisions.
|
|
206
|
+
if (typescriptSyntax.isIfStatement(node.elseStatement)) {
|
|
207
|
+
walkComplexityNode(node.elseStatement, controlFlowDepth, context);
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
walkComplexityNode(node.elseStatement, nestedDepth, context);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Counts a logical AND or OR in the supplied walk context and visits both operands at the supplied depth.
|
|
215
|
+
* Other binary operators add no decision of their own.
|
|
216
|
+
*
|
|
217
|
+
* @param node - Binary expression syntax node to inspect for logical operators.
|
|
218
|
+
* @param controlFlowDepth - Current control-flow nesting depth passed to both operands.
|
|
219
|
+
* @param context - Walk state holding the accumulator updated with logical operator counts.
|
|
220
|
+
* @returns Nothing. Updates `logicalAndCount` or `logicalOrCount` in `context` and walks child operands.
|
|
221
|
+
*/
|
|
222
|
+
function walkBinaryExpression(node: import("typescript").BinaryExpression, controlFlowDepth: number, context: ComplexityWalkContext): void {
|
|
223
|
+
if (node.operatorToken.kind === typescriptSyntax.SyntaxKind.AmpersandAmpersandToken) {
|
|
224
|
+
context.accumulator.logicalAndCount += 1;
|
|
225
|
+
}
|
|
226
|
+
if (node.operatorToken.kind === typescriptSyntax.SyntaxKind.BarBarToken) {
|
|
227
|
+
context.accumulator.logicalOrCount += 1;
|
|
228
|
+
}
|
|
229
|
+
walkChildrenAtDepth(node, controlFlowDepth, context);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Visits a control-flow construct's children one level deeper than the supplied depth.
|
|
234
|
+
*
|
|
235
|
+
* @param node - Control-flow construct whose children execute at increased nesting depth.
|
|
236
|
+
* @param controlFlowDepth - Control-flow nesting depth prior to entering this construct.
|
|
237
|
+
* @param context - Walk state whose accumulator tracks maximum nesting and child decisions.
|
|
238
|
+
* @returns Nothing. Updates maximum nesting and accumulates the children's decisions in the walk context.
|
|
239
|
+
*/
|
|
240
|
+
function walkNestedControlFlow(node: TypeScriptNode, controlFlowDepth: number, context: ComplexityWalkContext): void {
|
|
241
|
+
const nestedDepth = enterControlFlow(controlFlowDepth, context.accumulator);
|
|
242
|
+
walkChildrenAtDepth(node, nestedDepth, context);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Visits the supplied node's children at the given depth so ordinary braces add no nesting.
|
|
247
|
+
*
|
|
248
|
+
* @param node - Parent syntax node whose direct children are traversed.
|
|
249
|
+
* @param controlFlowDepth - Control-flow nesting depth preserved across the child traversal.
|
|
250
|
+
* @param context - Walk state used to enforce callable ownership and accumulate child measurements.
|
|
251
|
+
* @returns Nothing. Accumulates the children's measurements into the supplied walk context.
|
|
252
|
+
*/
|
|
253
|
+
function walkChildrenAtDepth(node: TypeScriptNode, controlFlowDepth: number, context: ComplexityWalkContext): void {
|
|
254
|
+
// Every child stays with the nearest reported callable unless the ownership guard prunes it.
|
|
255
|
+
node.forEachChild((child) => walkComplexityNode(child, controlFlowDepth, context));
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Enters a nested control-flow level and raises the supplied accumulator's maximum if the new depth exceeds it.
|
|
260
|
+
*
|
|
261
|
+
* @param controlFlowDepth - Current control-flow nesting depth before entering the construct.
|
|
262
|
+
* @param accumulator - Mutable complexity accumulator whose `maximumControlFlowNesting` is updated.
|
|
263
|
+
* @returns Current control-flow depth plus one.
|
|
264
|
+
*/
|
|
265
|
+
function enterControlFlow(controlFlowDepth: number, accumulator: ComplexityAccumulator): number {
|
|
266
|
+
const nestedDepth = controlFlowDepth + 1;
|
|
267
|
+
accumulator.maximumControlFlowNesting = Math.max(accumulator.maximumControlFlowNesting, nestedDepth);
|
|
268
|
+
return nestedDepth;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Determines whether an `if` statement is an early-exit guard clause.
|
|
273
|
+
* A guard clause has no else and a body of one return, throw, break, or continue, braced or not.
|
|
274
|
+
*
|
|
275
|
+
* @param node - Parsed `if` statement to check for early-exit guard structure.
|
|
276
|
+
* @returns True when `node` has no `elseStatement` and a single exit statement body; otherwise false.
|
|
277
|
+
*/
|
|
278
|
+
function isEarlyExitGuard(node: import("typescript").IfStatement): boolean {
|
|
279
|
+
if (node.elseStatement) {
|
|
280
|
+
return false;
|
|
281
|
+
}
|
|
282
|
+
const body = typescriptSyntax.isBlock(node.thenStatement) ? node.thenStatement.statements : [node.thenStatement];
|
|
283
|
+
const exit = body.length === 1 ? body[0] : undefined;
|
|
284
|
+
return exit !== undefined && (typescriptSyntax.isReturnStatement(exit)
|
|
285
|
+
|| typescriptSyntax.isThrowStatement(exit)
|
|
286
|
+
|| typescriptSyntax.isBreakStatement(exit)
|
|
287
|
+
|| typescriptSyntax.isContinueStatement(exit));
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Recognizes every loop syntax counted by the public complexity policy.
|
|
292
|
+
*
|
|
293
|
+
* @param node - Syntax node to test against `for`, `for..in`, `for..of`, `while`, and `do..while` forms.
|
|
294
|
+
* @returns True when `node` is one of the counted loop statement kinds; otherwise false.
|
|
295
|
+
*/
|
|
296
|
+
function isLoopStatement(node: TypeScriptNode): boolean {
|
|
297
|
+
return typescriptSyntax.isForStatement(node)
|
|
298
|
+
|| typescriptSyntax.isForInStatement(node)
|
|
299
|
+
|| typescriptSyntax.isForOfStatement(node)
|
|
300
|
+
|| typescriptSyntax.isWhileStatement(node)
|
|
301
|
+
|| typescriptSyntax.isDoStatement(node);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Creates a fresh zeroed breakdown for the base measurement used when a callable has no computed metrics.
|
|
306
|
+
*
|
|
307
|
+
* @returns Fresh breakdown with every decision counter and `maxNesting` set to zero.
|
|
308
|
+
*/
|
|
309
|
+
function emptyBreakdown(): ComplexityBreakdown {
|
|
310
|
+
return {
|
|
311
|
+
if: 0,
|
|
312
|
+
loop: 0,
|
|
313
|
+
catch: 0,
|
|
314
|
+
case: 0,
|
|
315
|
+
ternary: 0,
|
|
316
|
+
logicalAnd: 0,
|
|
317
|
+
logicalOr: 0,
|
|
318
|
+
maxNesting: 0,
|
|
319
|
+
};
|
|
320
|
+
}
|
package/src/config-load-error.ts
CHANGED
|
@@ -1,18 +1,27 @@
|
|
|
1
1
|
/*
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* Reports a tagged error that the CLI layer recognises via `instanceof`.
|
|
2
|
+
* User-facing configuration and flag-conflict error caught by CLI command handlers to exit 2 cleanly.
|
|
3
|
+
*
|
|
4
|
+
* Carries a structured `suggestion` alongside the message so broken `.gruff-ts.yaml` files or invalid selectors print actionable guidance
|
|
5
|
+
* instead of a raw Node stack trace, and lets machine consumers distinguish load failures from CLI flag refusals via `isConfigLoadFailure`.
|
|
7
6
|
*/
|
|
8
7
|
export class ConfigLoadError extends Error {
|
|
9
8
|
readonly suggestion: string;
|
|
10
9
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
/*
|
|
11
|
+
* True when the configuration itself could not be loaded, which is what a machine caller reads as a
|
|
12
|
+
* `config-error` diagnostic.
|
|
13
|
+
*
|
|
14
|
+
* False marks a refusal the caller asked for by combining flags. Nothing failed to load there, so there is no
|
|
15
|
+
* run-invalidating diagnostic to publish, and a report the run did produce must not be replaced by an empty one.
|
|
16
|
+
*/
|
|
17
|
+
readonly isConfigLoadFailure: boolean;
|
|
18
|
+
|
|
19
|
+
// Creates the CLI error from the display message and suggested next step.
|
|
20
|
+
// The failure flag defaults to true. Pass false for a flag-related refusal that should omit the config-error diagnostic.
|
|
21
|
+
constructor(message: string, suggestion: string, isConfigLoadFailure = true) {
|
|
14
22
|
super(message);
|
|
15
23
|
this.name = "ConfigLoadError";
|
|
16
24
|
this.suggestion = suggestion;
|
|
25
|
+
this.isConfigLoadFailure = isConfigLoadFailure;
|
|
17
26
|
}
|
|
18
27
|
}
|