@boyingliu01/xp-gate 0.36.1 → 0.37.1
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/adapters/cpp.sh +78 -10
- package/adapters/csharp.sh +58 -3
- package/adapters/gate-3.sh +15 -5
- package/adapters/gate-8.sh +131 -9
- package/adapters/typescript.sh +32 -8
- package/build-integrity/__tests__/import-resolver.test.ts +257 -0
- package/build-integrity/__tests__/quote-masker.test.ts +177 -0
- package/build-integrity/gate-10.ts +127 -9
- package/build-integrity/quote-masker.ts +266 -0
- package/gate-10.sh +66 -0
- package/gate-12-file-hygiene.sh +15 -7
- package/gate-13-compiler-strictness.sh +81 -1
- package/gate-3.sh +15 -5
- package/gate-6-layers.sh +375 -0
- package/gate-8.sh +131 -9
- package/hooks/gate-10.sh +66 -0
- package/hooks/gate-12-file-hygiene.sh +15 -7
- package/hooks/gate-13-compiler-strictness.sh +81 -1
- package/hooks/gate-3.sh +15 -5
- package/hooks/gate-6-layers.sh +375 -0
- package/hooks/gate-8.sh +131 -9
- package/hooks/gate-registry.sh +2 -2
- package/hooks/hook-module-resolution.sh +1 -1
- package/hooks/lib/assertion-quality.sh +8 -2
- package/hooks/lib/ccn-ratchet.sh +10 -0
- package/hooks/lib/layer-baseline.cjs +88 -0
- package/hooks/lib/typecheck.sh +28 -5
- package/hooks/pre-commit +376 -53
- package/hooks/pre-push +46 -13
- package/lib/__tests__/agent-runtime-journal.test.js +1 -1
- package/lib/__tests__/arch.test.js +9 -0
- package/lib/__tests__/audit-cli-contract.test.ts +1 -1
- package/lib/__tests__/check-alignment-reasons.test.mjs +11 -1
- package/lib/__tests__/check-all-skip-accounting.test.js +1 -1
- package/lib/__tests__/detect-deps-gate-identity.test.js +135 -0
- package/lib/__tests__/detect-deps.test.js +2 -1
- package/lib/__tests__/doctor-469-hook-env.test.js +7 -1
- package/lib/__tests__/doctor-arch-tools.test.js +169 -0
- package/lib/__tests__/doctor-delphi.test.js +1 -1
- package/lib/__tests__/doctor-drift-round2.test.js +4 -1
- package/lib/__tests__/doctor-executed-surface.test.js +45 -24
- package/lib/__tests__/doctor-module-drift.test.js +13 -7
- package/lib/__tests__/doctor-module-resolution-parity.test.js +74 -2
- package/lib/__tests__/doctor-tui-skill-freshness.test.js +21 -6
- package/lib/__tests__/doctor.test.js +17 -0
- package/lib/__tests__/gate-runner-tsx-invocation.test.js +149 -0
- package/lib/__tests__/gate-runner.test.js +23 -7
- package/lib/__tests__/init-gitignore.test.js +204 -0
- package/lib/__tests__/init.test.js +24 -3
- package/lib/__tests__/language-tools-build-file-install.test.js +82 -0
- package/lib/__tests__/language-tools-detect.test.js +61 -0
- package/lib/__tests__/mirror-sync.test.js +2 -0
- package/lib/__tests__/phase-transition.test.js +19 -1
- package/lib/__tests__/sprint-status-rework.test.js +32 -12
- package/lib/__tests__/test-alignment-range-guard.test.ts +82 -0
- package/lib/__tests__/test-alignment.test.ts +13 -3
- package/lib/__tests__/update-skill.test.js +2 -2
- package/lib/detect-deps.js +7 -1
- package/lib/doctor.js +81 -17
- package/lib/gate-runner.js +101 -7
- package/lib/hook-module-resolution.js +18 -6
- package/lib/init.js +136 -1
- package/lib/language-tools.js +217 -1
- package/lib/layer-baseline.cjs +88 -0
- package/lib/mattpocock-license-verdict.json +1 -1
- package/lib/principles.js +21 -6
- package/lib/test-alignment.ts +452 -55
- package/mock-policy/AGENTS.md +1 -1
- package/mutation/AGENTS.md +1 -1
- package/package.json +2 -1
- package/plugins/claude-code/.claude-plugin/plugin.json +1 -1
- package/plugins/claude-code/skills/delphi-review/.delphi-config.json.example +34 -24
- package/plugins/claude-code/skills/delphi-review/AGENTS.md +1 -1
- package/plugins/claude-code/skills/delphi-review/INSTALL.md +29 -1
- package/plugins/claude-code/skills/sprint-flow/AGENTS.md +1 -1
- package/plugins/claude-code/skills/test-specification-alignment/AGENTS.md +1 -1
- package/plugins/dsh/package.json +1 -1
- package/plugins/dsh/skills/delphi-review/.delphi-config.json.example +34 -24
- package/plugins/dsh/skills/delphi-review/AGENTS.md +1 -1
- package/plugins/dsh/skills/delphi-review/INSTALL.md +29 -1
- package/plugins/dsh/skills/sprint-flow/AGENTS.md +1 -1
- package/plugins/dsh/skills/test-specification-alignment/AGENTS.md +1 -1
- package/plugins/dsh/src/client.test.ts +1 -1
- package/plugins/dsh/src/command.test.ts +1 -1
- package/plugins/dsh/src/contract.test.ts +1 -1
- package/plugins/dsh/src/credentials.test.ts +1 -1
- package/plugins/opencode/package.json +1 -1
- package/plugins/opencode/skills/delphi-review/.delphi-config.json.example +34 -24
- package/plugins/opencode/skills/delphi-review/AGENTS.md +1 -1
- package/plugins/opencode/skills/delphi-review/INSTALL.md +29 -1
- package/plugins/opencode/skills/sprint-flow/AGENTS.md +1 -1
- package/plugins/opencode/skills/test-specification-alignment/AGENTS.md +1 -1
- package/plugins/qoder/plugin.json +1 -1
- package/plugins/qoder/skills/delphi-review/.delphi-config.json.example +34 -24
- package/plugins/qoder/skills/delphi-review/AGENTS.md +1 -1
- package/plugins/qoder/skills/delphi-review/INSTALL.md +29 -1
- package/plugins/qoder/skills/sprint-flow/AGENTS.md +1 -1
- package/plugins/qoder/skills/test-specification-alignment/AGENTS.md +1 -1
- package/principles/AGENTS.md +1 -1
- package/principles/__tests__/config-arch-group.test.ts +106 -0
- package/principles/__tests__/config-enforcement.test.ts +1 -1
- package/principles/adapters/control-nesting.ts +572 -0
- package/principles/adapters/csharp.ts +73 -42
- package/principles/adapters/java.ts +9 -5
- package/principles/adapters/typescript.ts +138 -176
- package/principles/config.ts +37 -2
- package/principles/index.ts +57 -4
- package/principles/reporter.ts +99 -65
- package/principles/rules/arch/__tests__/dependency.test.ts +180 -0
- package/principles/rules/arch/__tests__/naming.test.ts +133 -0
- package/principles/rules/arch/__tests__/shared.test.ts +113 -0
- package/principles/rules/arch/dependency.ts +264 -0
- package/principles/rules/arch/naming.ts +188 -0
- package/principles/rules/arch/shared.ts +169 -0
- package/principles/rules/exports.ts +26 -0
- package/principles/rules/index.ts +11 -18
- package/scripts/__tests__/sync-package-content.test.js +23 -1
- package/scripts/sync-package-content.js +8 -2
- package/skills/delphi-review/.delphi-config.json.example +34 -24
- package/skills/delphi-review/AGENTS.md +1 -1
- package/skills/delphi-review/INSTALL.md +29 -1
- package/skills/sprint-flow/AGENTS.md +1 -1
- package/skills/test-specification-alignment/AGENTS.md +1 -1
|
@@ -22,6 +22,50 @@ interface CSharpLexState {
|
|
|
22
22
|
rawStringQuoteCount: number;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
/**
|
|
26
|
+
* C# method declaration: optional modifiers, return type, name, params, then
|
|
27
|
+
* either a block `{`, an expression body `=>`, or `;` (abstract / interface
|
|
28
|
+
* members). `async` and generics (`Task<int>`) must be tolerated or every async
|
|
29
|
+
* method in a modern codebase is invisible.
|
|
30
|
+
*
|
|
31
|
+
* Module scope, not a local in `extractFunctions`: the complexity analyzer
|
|
32
|
+
* counts branches by scanning the function body text, and a regex literal is
|
|
33
|
+
* dense in `|` and `?`. Kept inline, these two patterns alone put
|
|
34
|
+
* `extractFunctions` at CCN 18 — a number that described the regex source
|
|
35
|
+
* rather than the code. The patterns are constant, so module scope is also where
|
|
36
|
+
* they belong.
|
|
37
|
+
*/
|
|
38
|
+
const METHOD_DECLARATION =
|
|
39
|
+
/(?:^|\n)[ \t]*(?:(?:public|private|protected|internal|static|async|override|virtual|sealed|abstract|extern|unsafe|new|partial)\s+)*(?:[\w.<>\[\],?]+\s+)?([A-Za-z_]\w*)\s*(?:<[^>]*>)?\s*\(([^)]*)\)\s*(?:where\s[^{;]*)?(?:=>|\{|;)/g;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Constructors: `public Foo(...)` has no return type, so METHOD_DECLARATION can
|
|
43
|
+
* miss it when the modifier list is empty or minimal. Same module-scope
|
|
44
|
+
* rationale.
|
|
45
|
+
*/
|
|
46
|
+
const CONSTRUCTOR_DECLARATION =
|
|
47
|
+
/(?:^|\n)[ \t]*(?:(?:public|private|protected|internal|static)\s+)*([A-Za-z_]\w*)\s*\(([^)]*)\)\s*(?::\s*(?:base|this)\s*\([^)]*\)\s*)?\{/g;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* A C# type declaration.
|
|
51
|
+
*
|
|
52
|
+
* The trailing group accepts BOTH shapes a type declaration takes: a body (`{`)
|
|
53
|
+
* and a body-less primary-constructor record (`record Delta(int X);`, C# 9+).
|
|
54
|
+
* Requiring `{` made every such record invisible to the god-class / large-file
|
|
55
|
+
* rules. Module scope for the same reason as METHOD_DECLARATION: a regex literal
|
|
56
|
+
* is dense in `|` and `?`, which the complexity analyzer counts as branches.
|
|
57
|
+
*/
|
|
58
|
+
const CLASS_DECLARATION =
|
|
59
|
+
/\b(?:(?:public|private|protected|internal|static|sealed|abstract|partial|readonly|unsafe|new)\s+)*(class|interface|struct|record|enum)\s+([A-Za-z_]\w*)\s*(?:<[^>]*>)?\s*(?:\([^)]*\))?\s*(?::\s*[^{\n;]+)?\s*(?:\{|;)/g;
|
|
60
|
+
|
|
61
|
+
/** One member declaration inside a type body, for the god-class rule. */
|
|
62
|
+
const MEMBER_DECLARATION = /\b(?:public|private|protected|internal)\s+[^;{}]*\([^)]*\)\s*(?:=>|\{)/g;
|
|
63
|
+
|
|
64
|
+
/** How many accessor-prefixed members a type body declares. */
|
|
65
|
+
function countMemberDeclarations(body: string): number {
|
|
66
|
+
return (body.match(MEMBER_DECLARATION) || []).length;
|
|
67
|
+
}
|
|
68
|
+
|
|
25
69
|
/**
|
|
26
70
|
* C# language adapter for the principles checker (Gate 4).
|
|
27
71
|
*
|
|
@@ -49,66 +93,53 @@ export class CSharpAdapter extends BaseAdapter implements Adapter {
|
|
|
49
93
|
return this.createParseResult('csharp');
|
|
50
94
|
}
|
|
51
95
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
96
|
+
/**
|
|
97
|
+
* Scan `regex` over the file and record one code match per surviving hit.
|
|
98
|
+
*
|
|
99
|
+
* Methods and constructors are found by two different patterns, but they
|
|
100
|
+
* produce the same shape (name, paramCount, body metrics) and share the
|
|
101
|
+
* reserved-word guard. Keeping the two loops separate duplicated that logic
|
|
102
|
+
* and raised this method's complexity for no benefit.
|
|
103
|
+
*/
|
|
104
|
+
private collectDeclarations(
|
|
105
|
+
regex: RegExp,
|
|
106
|
+
kind: 'function' | 'constructor',
|
|
107
|
+
into: unknown[],
|
|
108
|
+
): void {
|
|
109
|
+
regex.lastIndex = 0;
|
|
62
110
|
let match: RegExpExecArray | null;
|
|
63
|
-
while ((match =
|
|
111
|
+
while ((match = regex.exec(this.fileContent)) !== null) {
|
|
64
112
|
const name = match[1];
|
|
65
|
-
// Constructors and `new` expressions look like the
|
|
113
|
+
// Constructors and `new` expressions look like the patterns above; the
|
|
66
114
|
// reserved words are not method names as far as the rules care.
|
|
67
115
|
if (CSharpAdapter.RESERVED.has(name)) continue;
|
|
68
116
|
|
|
69
117
|
const params = match[2].trim();
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
this.annotate(fnMatch, 'paramCount', paramCount);
|
|
75
|
-
this.annotateBodyMetrics(fnMatch);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// Constructors: `public Foo(...)` has no return type, so the regex above
|
|
79
|
-
// can miss it when the modifier list is empty or minimal.
|
|
80
|
-
const ctorRegex = /(?:^|\n)[ \t]*(?:(?:public|private|protected|internal|static)\s+)*([A-Za-z_]\w*)\s*\(([^)]*)\)\s*(?::\s*(?:base|this)\s*\([^)]*\)\s*)?\{/g;
|
|
81
|
-
while ((match = ctorRegex.exec(this.fileContent)) !== null) {
|
|
82
|
-
const name = match[1];
|
|
83
|
-
if (CSharpAdapter.RESERVED.has(name)) continue;
|
|
84
|
-
const params = match[2].trim();
|
|
85
|
-
const paramCount = params === '' ? 0 : params.split(',').length;
|
|
86
|
-
matches.push(this.createCodeMatch(name, 'constructor', match.index));
|
|
87
|
-
const ctorMatch = matches[matches.length - 1];
|
|
88
|
-
this.annotate(ctorMatch, 'paramCount', paramCount);
|
|
89
|
-
this.annotateBodyMetrics(ctorMatch);
|
|
118
|
+
const codeMatch = this.createCodeMatch(name, kind, match.index);
|
|
119
|
+
this.annotate(codeMatch, 'paramCount', params === '' ? 0 : params.split(',').length);
|
|
120
|
+
this.annotateBodyMetrics(codeMatch);
|
|
121
|
+
into.push(codeMatch);
|
|
90
122
|
}
|
|
123
|
+
}
|
|
91
124
|
|
|
125
|
+
extractFunctions(): unknown[] {
|
|
126
|
+
const matches: unknown[] = [];
|
|
127
|
+
this.collectDeclarations(METHOD_DECLARATION, 'function', matches);
|
|
128
|
+
this.collectDeclarations(CONSTRUCTOR_DECLARATION, 'constructor', matches);
|
|
92
129
|
return matches;
|
|
93
130
|
}
|
|
94
131
|
|
|
95
132
|
extractClasses(): unknown[] {
|
|
96
133
|
const matches: unknown[] = [];
|
|
97
|
-
// The trailing group accepts BOTH shapes a type declaration takes: a body
|
|
98
|
-
// (`{`) and a body-less primary-constructor record (`record Delta(int X);`,
|
|
99
|
-
// C# 9+). Requiring `{` made every such record invisible to the god-class /
|
|
100
|
-
// large-file rules.
|
|
101
|
-
const classRegex =
|
|
102
|
-
/\b(?:(?:public|private|protected|internal|static|sealed|abstract|partial|readonly|unsafe|new)\s+)*(class|interface|struct|record|enum)\s+([A-Za-z_]\w*)\s*(?:<[^>]*>)?\s*(?:\([^)]*\))?\s*(?::\s*[^{\n;]+)?\s*(?:\{|;)/g;
|
|
103
134
|
|
|
104
135
|
let match: RegExpExecArray | null;
|
|
105
|
-
while ((match =
|
|
106
|
-
|
|
136
|
+
while ((match = CLASS_DECLARATION.exec(this.fileContent)) !== null) {
|
|
137
|
+
const codeMatch = this.createCodeMatch(match[2], match[1], match.index);
|
|
107
138
|
const body = this.extractCodeBlock(match.index);
|
|
108
139
|
// methodCount feeds the god-class rule; counting declarations in the body
|
|
109
140
|
// is the only signal available without a real parser.
|
|
110
|
-
|
|
111
|
-
|
|
141
|
+
this.annotate(codeMatch, 'methodCount', countMemberDeclarations(body));
|
|
142
|
+
matches.push(codeMatch);
|
|
112
143
|
}
|
|
113
144
|
|
|
114
145
|
return matches;
|
|
@@ -230,14 +230,18 @@ export const javaParse = {
|
|
|
230
230
|
// parameter, so the commas inside its arguments must not be counted.
|
|
231
231
|
// Which characters change depth and which separate is data, not control
|
|
232
232
|
// flow -- table lookups keep the scan to a single conditional.
|
|
233
|
+
//
|
|
234
|
+
// A comma counts only at depth zero, expressed as ONE condition rather than
|
|
235
|
+
// a nested pair: the earlier form tested `ch === ','` and then re-tested
|
|
236
|
+
// `depth === 0` inside it, which the complexity budget charged twice for a
|
|
237
|
+
// single decision. The `?? 0` default moved into the lookup itself for the
|
|
238
|
+
// same reason -- it was a third branch on a table that answers for every
|
|
239
|
+
// nesting character.
|
|
233
240
|
let depth = 0;
|
|
234
241
|
let count = 1;
|
|
235
242
|
for (const ch of trimmed) {
|
|
236
|
-
if (ch === ',')
|
|
237
|
-
|
|
238
|
-
continue;
|
|
239
|
-
}
|
|
240
|
-
depth += PARAM_NESTING[ch] ?? 0;
|
|
243
|
+
if (ch === ',' && depth === 0) count += 1;
|
|
244
|
+
depth += PARAM_NESTING[ch] || 0;
|
|
241
245
|
}
|
|
242
246
|
return count;
|
|
243
247
|
},
|
|
@@ -6,6 +6,7 @@ import { parseImportClause } from './import-clause';
|
|
|
6
6
|
import {
|
|
7
7
|
type QuoteState, newQuoteState, inQuotedState, advanceQuoteState, applyDelimiter, isQuote,
|
|
8
8
|
} from './quote-state';
|
|
9
|
+
import { maxControlNesting } from './control-nesting';
|
|
9
10
|
|
|
10
11
|
/** IO-ish callees that the missing-error-handling rule cares about. */
|
|
11
12
|
const IO_CALLEES = [
|
|
@@ -186,6 +187,123 @@ function lineOf(src: string, index: number): number {
|
|
|
186
187
|
return src.substring(0, index).split('\n').length;
|
|
187
188
|
}
|
|
188
189
|
|
|
190
|
+
/**
|
|
191
|
+
* Offsets of the literals that are the initializer of a named constant.
|
|
192
|
+
*
|
|
193
|
+
* `const NAME = 3` / `const NAME: T = 3`. The magic-number rule skips a number
|
|
194
|
+
* that IS the initializer of a named constant, because the rule's own advice is
|
|
195
|
+
* "consider using a named constant instead" — reporting the constant it just
|
|
196
|
+
* asked for made the violation unfixable.
|
|
197
|
+
*
|
|
198
|
+
* The value's OFFSET is captured rather than the value itself so the literal can
|
|
199
|
+
* be matched positionally: the same number appearing twice must be judged
|
|
200
|
+
* separately, and only the declaration occurrence is exempt.
|
|
201
|
+
*
|
|
202
|
+
* Lives at module scope: as a method it pushed `TypeScriptAdapter` to 16
|
|
203
|
+
* methods, past the god-class threshold of 15, for a pure helper that never
|
|
204
|
+
* reads instance state.
|
|
205
|
+
*/
|
|
206
|
+
function namedConstLiteralOffsets(masked: string): Set<number> {
|
|
207
|
+
const namedConstRe = /\b(?:const|readonly)\s+[A-Za-z_$][\w$]*\s*(?::[^=;]+)?=\s*(-?\d+(?:\.\d+)?)\s*;/g;
|
|
208
|
+
const offsets = new Set<number>();
|
|
209
|
+
let c: RegExpExecArray | null;
|
|
210
|
+
while ((c = namedConstRe.exec(masked)) !== null) {
|
|
211
|
+
offsets.add(c.index + c[0].lastIndexOf(c[1]));
|
|
212
|
+
}
|
|
213
|
+
return offsets;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** One `import ... from '...'` statement. */
|
|
217
|
+
const IMPORT_STATEMENT = /^[ \t]*import\s+(?:type\s+)?([\s\S]*?)\s+from\s+['"][^'"]+['"]/gm;
|
|
218
|
+
|
|
219
|
+
/** True when the statement this match belongs to is `import type ...`. */
|
|
220
|
+
const TYPE_ONLY_IMPORT = /^import\s+type\b/;
|
|
221
|
+
|
|
222
|
+
type ImportBinding = { name: string; line: number; used: boolean; type?: string };
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Every binding named by an import statement, plus the span each statement
|
|
226
|
+
* occupies.
|
|
227
|
+
*
|
|
228
|
+
* The spans are returned because usage counting needs the statements BLANKED
|
|
229
|
+
* first: an imported name obviously appears inside its own `import` line, so
|
|
230
|
+
* scanning the raw file marks every import as used. Inlined into the accessor
|
|
231
|
+
* that consumes this, the whole job scored CCN 8 against a baseline of 5.
|
|
232
|
+
*/
|
|
233
|
+
function collectImportBindings(src: string): {
|
|
234
|
+
entries: ImportBinding[];
|
|
235
|
+
importSpans: Array<[number, number]>;
|
|
236
|
+
} {
|
|
237
|
+
const entries: ImportBinding[] = [];
|
|
238
|
+
const importSpans: Array<[number, number]> = [];
|
|
239
|
+
let m: RegExpExecArray | null;
|
|
240
|
+
while ((m = IMPORT_STATEMENT.exec(src)) !== null) {
|
|
241
|
+
const line = lineOf(src, m.index);
|
|
242
|
+
const isTypeOnly = TYPE_ONLY_IMPORT.test(m[0]);
|
|
243
|
+
for (const binding of parseImportClause(m[1].trim(), isTypeOnly)) {
|
|
244
|
+
entries.push({ ...binding, line, used: false });
|
|
245
|
+
}
|
|
246
|
+
importSpans.push([m.index, m.index + m[0].length]);
|
|
247
|
+
}
|
|
248
|
+
return { entries, importSpans };
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* `text` with each span replaced by spaces.
|
|
253
|
+
*
|
|
254
|
+
* Spaces rather than deletion: line numbers and column offsets of everything
|
|
255
|
+
* after the span must keep pointing at the original characters.
|
|
256
|
+
*/
|
|
257
|
+
function blankSpans(text: string, spans: Array<[number, number]>): string {
|
|
258
|
+
let out = text;
|
|
259
|
+
// Reverse order so an earlier span's offsets stay valid while later ones are
|
|
260
|
+
// rewritten; ascending order would shift every subsequent span.
|
|
261
|
+
for (const [start, end] of [...spans].reverse()) {
|
|
262
|
+
out = out.substring(0, start) + ' '.repeat(end - start) + out.substring(end);
|
|
263
|
+
}
|
|
264
|
+
return out;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** True when `name` appears in `body` as a whole identifier, not a property. */
|
|
268
|
+
function isReferenced(name: string, body: string): boolean {
|
|
269
|
+
const escaped = name.replace(/\$/g, '\\$');
|
|
270
|
+
return new RegExp(`(?<![\\w$.])${escaped}(?![\\w$])`, 'g').test(body);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* A class member declaration: `name(...) {`. Not anchored to line start so
|
|
275
|
+
* minified single-line bodies count too.
|
|
276
|
+
*
|
|
277
|
+
* Module scope because a regex literal is dense in `|` and `?`, which the
|
|
278
|
+
* complexity analyzer counts as branches — inlined, this pattern alone is most of
|
|
279
|
+
* what made `collectClassMethods` score CCN 16 against a baseline of 6.
|
|
280
|
+
*/
|
|
281
|
+
const CLASS_MEMBER = /(?<![\w$.])(?:public\s+|private\s+|protected\s+|static\s+|async\s+|readonly\s+|get\s+|set\s+|\*\s*)*(\w+)\s*\(/g;
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Whether a CLASS_MEMBER hit is really a method, rather than a shape that only
|
|
285
|
+
* looks like one.
|
|
286
|
+
*
|
|
287
|
+
* Three rejections, each a distinct reason, so the caller's loop stays a filter:
|
|
288
|
+
* a control-flow keyword (`if (a > 0) {` matches the shape), a call site
|
|
289
|
+
* (`foo(x)` is not a declaration), and a declaration with no body brace.
|
|
290
|
+
*/
|
|
291
|
+
function isMethodShaped(masked: string, name: string, nameIndex: number, parenIndex: number): boolean {
|
|
292
|
+
if (CONTROL_KEYWORDS.has(name)) return false;
|
|
293
|
+
if (isCallPosition(masked, nameIndex)) return false;
|
|
294
|
+
// Only treat as a method when a body brace follows the parameter list.
|
|
295
|
+
return findBodyBrace(masked, parenIndex) !== -1;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* An arrow-function assignment: `const name = (...) =>` / `let name = x =>`.
|
|
300
|
+
*
|
|
301
|
+
* Module scope because a regex literal is dense in `|` and `?`, which the
|
|
302
|
+
* complexity analyzer counts as branches; inlined, this pattern was most of what
|
|
303
|
+
* made `collectArrowFunctions` score CCN 9 against a baseline of 4.
|
|
304
|
+
*/
|
|
305
|
+
const ARROW_ASSIGNMENT = /(?:const|let|var)\s+(\w+)\s*(?::[^=]+)?=\s*(?:async\s*)?/g;
|
|
306
|
+
|
|
189
307
|
/** The three quoted states shared by the parameter/paren scanners. */
|
|
190
308
|
/** Count top-level, comma-separated parameters in a raw parameter list. */
|
|
191
309
|
function countParams(rawList: string): number {
|
|
@@ -276,135 +394,6 @@ function readParenGroup(src: string, open: number): { inner: string; end: number
|
|
|
276
394
|
return { inner: src.substring(open + 1), end: src.length };
|
|
277
395
|
}
|
|
278
396
|
|
|
279
|
-
/**
|
|
280
|
-
* The lexical states `maxNesting` tracks while counting braces. Braces inside
|
|
281
|
-
* any of these are data, not structure, so they must not change the count.
|
|
282
|
-
*/
|
|
283
|
-
interface NestingScanState {
|
|
284
|
-
inSingle: boolean;
|
|
285
|
-
inDouble: boolean;
|
|
286
|
-
inTemplate: boolean;
|
|
287
|
-
inLineComment: boolean;
|
|
288
|
-
inBlockComment: boolean;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
/**
|
|
292
|
-
* One handler per lexical state. Each returns how many characters were
|
|
293
|
-
* consumed, and mutates `state` only when the construct actually ends.
|
|
294
|
-
*
|
|
295
|
-
* Split out of a single 22-CCN dispatcher (#602): every state's exit condition
|
|
296
|
-
* is independent, so keeping them in one body bought nothing but branch count.
|
|
297
|
-
*/
|
|
298
|
-
type StateAdvance = (body: string, i: number, state: NestingScanState) => number;
|
|
299
|
-
|
|
300
|
-
const advanceLineComment: StateAdvance = (body, i, state) => {
|
|
301
|
-
if (body[i] === '\n') state.inLineComment = false;
|
|
302
|
-
return 1;
|
|
303
|
-
};
|
|
304
|
-
|
|
305
|
-
const advanceBlockComment: StateAdvance = (body, i, state) => {
|
|
306
|
-
if (body[i] === '*' && body[i + 1] === '/') {
|
|
307
|
-
state.inBlockComment = false;
|
|
308
|
-
return 2;
|
|
309
|
-
}
|
|
310
|
-
return 1;
|
|
311
|
-
};
|
|
312
|
-
|
|
313
|
-
/** Shared shape of the three quoted states: honour `\`, close on the delimiter. */
|
|
314
|
-
const advanceSingleQuote: StateAdvance = (body, i, state) => {
|
|
315
|
-
if (body[i] === '\\') return 2;
|
|
316
|
-
if (body[i] === "'") state.inSingle = false;
|
|
317
|
-
return 1;
|
|
318
|
-
};
|
|
319
|
-
|
|
320
|
-
const advanceDoubleQuote: StateAdvance = (body, i, state) => {
|
|
321
|
-
if (body[i] === '\\') return 2;
|
|
322
|
-
if (body[i] === '"') state.inDouble = false;
|
|
323
|
-
return 1;
|
|
324
|
-
};
|
|
325
|
-
|
|
326
|
-
const advanceTemplate: StateAdvance = (body, i, state) => {
|
|
327
|
-
if (body[i] === '\\') return 2;
|
|
328
|
-
if (body[i] === '`') state.inTemplate = false;
|
|
329
|
-
return 1;
|
|
330
|
-
};
|
|
331
|
-
|
|
332
|
-
/**
|
|
333
|
-
* Enter a construct when the current character opens one.
|
|
334
|
-
* @returns characters consumed, or 0 when nothing was opened.
|
|
335
|
-
*/
|
|
336
|
-
function openConstruct(body: string, i: number, state: NestingScanState): number {
|
|
337
|
-
const c = body[i];
|
|
338
|
-
const next = body[i + 1];
|
|
339
|
-
if (c === '/' && next === '/') { state.inLineComment = true; return 2; }
|
|
340
|
-
if (c === '/' && next === '*') { state.inBlockComment = true; return 2; }
|
|
341
|
-
if (c === "'") { state.inSingle = true; return 1; }
|
|
342
|
-
if (c === '"') { state.inDouble = true; return 1; }
|
|
343
|
-
if (c === '`') { state.inTemplate = true; return 1; }
|
|
344
|
-
return 0;
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
/**
|
|
348
|
-
* Advance the quoting/commenting state machine by one character.
|
|
349
|
-
*
|
|
350
|
-
* Pure with respect to depth: it only updates `state` and reports how many
|
|
351
|
-
* characters were consumed. Extracted from `maxNesting` (#602) so the brace
|
|
352
|
-
* counting and the lexing can be reasoned about separately -- the combined body
|
|
353
|
-
* carried every branch of both.
|
|
354
|
-
*
|
|
355
|
-
* @returns the number of characters consumed (1, or 2 for the escape/close
|
|
356
|
-
* pairs that swallow the next character).
|
|
357
|
-
*/
|
|
358
|
-
function advanceNestingState(
|
|
359
|
-
body: string,
|
|
360
|
-
i: number,
|
|
361
|
-
state: NestingScanState,
|
|
362
|
-
): number {
|
|
363
|
-
if (state.inLineComment) return advanceLineComment(body, i, state);
|
|
364
|
-
if (state.inBlockComment) return advanceBlockComment(body, i, state);
|
|
365
|
-
if (state.inSingle) return advanceSingleQuote(body, i, state);
|
|
366
|
-
if (state.inDouble) return advanceDoubleQuote(body, i, state);
|
|
367
|
-
if (state.inTemplate) return advanceTemplate(body, i, state);
|
|
368
|
-
const opened = openConstruct(body, i, state);
|
|
369
|
-
return opened === 0 ? 1 : opened;
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
/** True when the character at `i` is structural (not inside a literal/comment). */
|
|
373
|
-
function isStructuralAt(state: NestingScanState): boolean {
|
|
374
|
-
return !state.inSingle && !state.inDouble && !state.inTemplate &&
|
|
375
|
-
!state.inLineComment && !state.inBlockComment;
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
/** Max brace nesting depth inside a function body (1 = body itself). */
|
|
379
|
-
function maxNesting(body: string): number {
|
|
380
|
-
let depth = 0;
|
|
381
|
-
let max = 0;
|
|
382
|
-
const state: NestingScanState = {
|
|
383
|
-
inSingle: false,
|
|
384
|
-
inDouble: false,
|
|
385
|
-
inTemplate: false,
|
|
386
|
-
inLineComment: false,
|
|
387
|
-
inBlockComment: false,
|
|
388
|
-
};
|
|
389
|
-
|
|
390
|
-
for (let i = 0; i < body.length;) {
|
|
391
|
-
const consumed = advanceNestingState(body, i, state);
|
|
392
|
-
if (consumed === 2) {
|
|
393
|
-
// The pair (escape, comment close, comment open) is consumed whole. A
|
|
394
|
-
// comment opener must not also be read as a brace-less character below.
|
|
395
|
-
i += 2;
|
|
396
|
-
continue;
|
|
397
|
-
}
|
|
398
|
-
if (isStructuralAt(state)) {
|
|
399
|
-
const c = body[i];
|
|
400
|
-
if (c === '{') { depth++; if (depth > max) max = depth; }
|
|
401
|
-
else if (c === '}') depth--;
|
|
402
|
-
}
|
|
403
|
-
i += 1;
|
|
404
|
-
}
|
|
405
|
-
return max;
|
|
406
|
-
}
|
|
407
|
-
|
|
408
397
|
/** Which IO callees appear in this body. */
|
|
409
398
|
function ioOperationsIn(body: string): string[] {
|
|
410
399
|
const found = new Set<string>();
|
|
@@ -627,17 +616,9 @@ export class TypeScriptAdapter extends BaseAdapter implements Adapter {
|
|
|
627
616
|
*/
|
|
628
617
|
extract(): Array<{ value: number; line: number }> {
|
|
629
618
|
const src = this.fileContent;
|
|
630
|
-
const results: Array<{ value: number; line: number }> = [];
|
|
631
619
|
const masked = this.maskedSource;
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
// literal can be matched positionally rather than by value.
|
|
635
|
-
const namedConstRe = /\b(?:const|readonly)\s+[A-Za-z_$][\w$]*\s*(?::[^=;]+)?=\s*(-?\d+(?:\.\d+)?)\s*;/g;
|
|
636
|
-
const namedConstOffsets = new Set<number>();
|
|
637
|
-
let c: RegExpExecArray | null;
|
|
638
|
-
while ((c = namedConstRe.exec(masked)) !== null) {
|
|
639
|
-
namedConstOffsets.add(c.index + c[0].lastIndexOf(c[1]));
|
|
640
|
-
}
|
|
620
|
+
const namedConstOffsets = namedConstLiteralOffsets(masked);
|
|
621
|
+
const results: Array<{ value: number; line: number }> = [];
|
|
641
622
|
|
|
642
623
|
const numRe = /(?<![\w$.])(-?\d+(?:\.\d+)?)(?![\w$])/g;
|
|
643
624
|
let m: RegExpExecArray | null;
|
|
@@ -657,30 +638,12 @@ export class TypeScriptAdapter extends BaseAdapter implements Adapter {
|
|
|
657
638
|
*/
|
|
658
639
|
get imports(): Array<{ name: string; line: number; used: boolean; type?: string }> {
|
|
659
640
|
const src = this.fileContent;
|
|
660
|
-
const
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
const line = lineOf(src, m.index);
|
|
667
|
-
const isTypeOnly = /^import\s+type\b/.test(m[0]);
|
|
668
|
-
for (const binding of parseImportClause(m[1].trim(), isTypeOnly)) {
|
|
669
|
-
entries.push({ ...binding, line, used: false });
|
|
670
|
-
}
|
|
671
|
-
importSpans.push([m.index, m.index + m[0].length]);
|
|
672
|
-
}
|
|
673
|
-
|
|
674
|
-
// Count identifier usage outside import statements.
|
|
675
|
-
let body = src;
|
|
676
|
-
for (const [start, end] of [...importSpans].reverse()) {
|
|
677
|
-
body = body.substring(0, start) + ' '.repeat(end - start) + body.substring(end);
|
|
678
|
-
}
|
|
679
|
-
for (const entry of entries) {
|
|
680
|
-
const useRe = new RegExp(`(?<![\\w$.])${entry.name.replace(/\$/g, '\\$')}(?![\\w$])`, 'g');
|
|
681
|
-
entry.used = useRe.test(body);
|
|
682
|
-
}
|
|
683
|
-
|
|
641
|
+
const { entries, importSpans } = collectImportBindings(src);
|
|
642
|
+
// Usage is judged against the file with every import statement blanked out.
|
|
643
|
+
// Inlined, this accessor scored CCN 8 against a baseline of 5 because it did
|
|
644
|
+
// three jobs at once: find the imports, mask them, then count references.
|
|
645
|
+
const body = blankSpans(src, importSpans);
|
|
646
|
+
for (const entry of entries) entry.used = isReferenced(entry.name, body);
|
|
684
647
|
return entries;
|
|
685
648
|
}
|
|
686
649
|
|
|
@@ -741,7 +704,7 @@ export class TypeScriptAdapter extends BaseAdapter implements Adapter {
|
|
|
741
704
|
endLine,
|
|
742
705
|
length: hasBraces ? Math.max(1, endLine - startLine + 1) : 1,
|
|
743
706
|
paramCount: countParams(inner),
|
|
744
|
-
nestingDepth: hasBraces ?
|
|
707
|
+
nestingDepth: hasBraces ? maxControlNesting(maskLiterals(body)) : 0,
|
|
745
708
|
// The RAW body: `on('error', ...)` needs its string literal intact, and
|
|
746
709
|
// masking blanks string bodies. try/catch is a keyword and is unaffected.
|
|
747
710
|
hasTryCatch: hasErrorHandling(body),
|
|
@@ -789,31 +752,25 @@ export class TypeScriptAdapter extends BaseAdapter implements Adapter {
|
|
|
789
752
|
*/
|
|
790
753
|
private collectClassMethods(seen: Set<string>, found: FunctionMetrics[]): void {
|
|
791
754
|
const masked = this.maskedSource;
|
|
792
|
-
const memberRe = /(?<![\w$.])(?:public\s+|private\s+|protected\s+|static\s+|async\s+|readonly\s+|get\s+|set\s+|\*\s*)*(\w+)\s*\(/g;
|
|
793
755
|
let m: RegExpExecArray | null;
|
|
794
|
-
while ((m =
|
|
795
|
-
if (CONTROL_KEYWORDS.has(m[1])) continue;
|
|
756
|
+
while ((m = CLASS_MEMBER.exec(masked)) !== null) {
|
|
796
757
|
// The pattern ends with `\(`, so the match's last character IS the opening
|
|
797
758
|
// paren. Re-searching with indexOf() could drift to a later `(` inside the
|
|
798
759
|
// parameter list or a type annotation, which corrupted the reported span.
|
|
799
760
|
const parenIndex = m.index + m[0].length - 1;
|
|
800
761
|
const nameIndex = parenIndex - m[1].length;
|
|
801
|
-
if (
|
|
802
|
-
// Only treat as a method when a body brace follows the parameter list.
|
|
803
|
-
const bodyOpen = findBodyBrace(masked, parenIndex);
|
|
804
|
-
if (bodyOpen === -1) continue;
|
|
762
|
+
if (!isMethodShaped(masked, m[1], nameIndex, parenIndex)) continue;
|
|
805
763
|
if (!claimMember(seen, m[1], nameIndex)) continue;
|
|
806
|
-
found.push(this.buildFunction(m[1], 'method', nameIndex, parenIndex,
|
|
764
|
+
found.push(this.buildFunction(m[1], 'method', nameIndex, parenIndex, findBodyBrace(masked, parenIndex)));
|
|
807
765
|
}
|
|
808
766
|
}
|
|
809
767
|
|
|
810
768
|
/** Arrow consts / assignments: `const name = (...) =>` / `name = x =>`. */
|
|
811
769
|
private collectArrowFunctions(seen: Set<string>, found: FunctionMetrics[]): void {
|
|
812
770
|
const masked = this.maskedSource;
|
|
813
|
-
const arrowRe = /(?:const|let|var)\s+(\w+)\s*(?::[^=]+)?=\s*(?:async\s*)?/g;
|
|
814
771
|
let m: RegExpExecArray | null;
|
|
815
|
-
while ((m =
|
|
816
|
-
const arrow = this.parseArrowAt(m,
|
|
772
|
+
while ((m = ARROW_ASSIGNMENT.exec(masked)) !== null) {
|
|
773
|
+
const arrow = this.parseArrowAt(m, ARROW_ASSIGNMENT.lastIndex);
|
|
817
774
|
if (!arrow) continue;
|
|
818
775
|
const nameIndex = m.index + m[0].length - m[1].length;
|
|
819
776
|
if (!claimMember(seen, m[1], nameIndex)) continue;
|
|
@@ -880,6 +837,11 @@ export class TypeScriptAdapter extends BaseAdapter implements Adapter {
|
|
|
880
837
|
|
|
881
838
|
const bodyEnd = findBlockEnd(this.maskedSource, arrow.bodyOpen);
|
|
882
839
|
const code = src.substring(arrow.bodyOpen, bodyEnd);
|
|
840
|
+
// Scan the MASKED slice, matching the named-function path above: a `}` or
|
|
841
|
+
// `)` inside a string literal would otherwise unbalance the brace pairing
|
|
842
|
+
// and drop the nesting inside that block. `code` stays raw for the reported
|
|
843
|
+
// source and the regex-based checks below.
|
|
844
|
+
const maskedCode = this.maskedSource.substring(arrow.bodyOpen, bodyEnd);
|
|
883
845
|
const endLine = lineOf(src, bodyEnd);
|
|
884
846
|
return {
|
|
885
847
|
name,
|
|
@@ -889,7 +851,7 @@ export class TypeScriptAdapter extends BaseAdapter implements Adapter {
|
|
|
889
851
|
endLine: startLine,
|
|
890
852
|
length: Math.max(1, endLine - startLine + 1),
|
|
891
853
|
paramCount: arrow.parenIndex === -1 ? 1 : countParams(readParenGroup(src, arrow.parenIndex).inner),
|
|
892
|
-
nestingDepth:
|
|
854
|
+
nestingDepth: maxControlNesting(maskedCode),
|
|
893
855
|
hasTryCatch: /\btry\s*\{/.test(code),
|
|
894
856
|
ioOperations: ioOperationsIn(code),
|
|
895
857
|
code,
|
package/principles/config.ts
CHANGED
|
@@ -31,6 +31,19 @@ interface PrinciplesConfig {
|
|
|
31
31
|
'isp': RuleConfig;
|
|
32
32
|
'dip': RuleConfig;
|
|
33
33
|
};
|
|
34
|
+
// Architecture (ARCH-07 / ARCH-12, #694 / #699). A separate group rather
|
|
35
|
+
// than more `clean-code` entries: these rules constrain package layout and
|
|
36
|
+
// dependency direction, which is neither a Clean Code smell nor a SOLID
|
|
37
|
+
// principle. Filing them under `clean-code` would give users the
|
|
38
|
+
// self-contradictory key `clean-code.arch-naming` to configure.
|
|
39
|
+
'arch': {
|
|
40
|
+
'controller': RuleConfig;
|
|
41
|
+
'service': RuleConfig;
|
|
42
|
+
'repository': RuleConfig;
|
|
43
|
+
'dto-layering': RuleConfig;
|
|
44
|
+
'domain-event': RuleConfig;
|
|
45
|
+
'util-service': RuleConfig;
|
|
46
|
+
};
|
|
34
47
|
};
|
|
35
48
|
output?: {
|
|
36
49
|
format?: string;
|
|
@@ -102,6 +115,23 @@ const DEFAULT_SOLID_RULES = {
|
|
|
102
115
|
}
|
|
103
116
|
} satisfies PrinciplesConfig['rules']['solid'];
|
|
104
117
|
|
|
118
|
+
/**
|
|
119
|
+
* Architecture rule defaults (#694 / #699).
|
|
120
|
+
*
|
|
121
|
+
* All six are `error` except `util-service`, which is `warning` by design:
|
|
122
|
+
* a helper depending on a service is a smell worth surfacing, but blocking a
|
|
123
|
+
* commit on it would fail builds over code that works (AC-699-08, AC-699-10,
|
|
124
|
+
* AC-699-11).
|
|
125
|
+
*/
|
|
126
|
+
const DEFAULT_ARCH_RULES = {
|
|
127
|
+
controller: { enabled: true, severity: 'error' },
|
|
128
|
+
service: { enabled: true, severity: 'error' },
|
|
129
|
+
repository: { enabled: true, severity: 'error' },
|
|
130
|
+
'dto-layering': { enabled: true, severity: 'error' },
|
|
131
|
+
'domain-event': { enabled: true, severity: 'error' },
|
|
132
|
+
'util-service': { enabled: true, severity: 'warning' }
|
|
133
|
+
} satisfies PrinciplesConfig['rules']['arch'];
|
|
134
|
+
|
|
105
135
|
/**
|
|
106
136
|
* Deep-clone a rule group so callers cannot mutate the module constants.
|
|
107
137
|
*
|
|
@@ -125,7 +155,8 @@ export function getDefaultConfig(): PrinciplesConfig {
|
|
|
125
155
|
return {
|
|
126
156
|
rules: {
|
|
127
157
|
'clean-code': cloneRules(DEFAULT_CLEAN_CODE_RULES),
|
|
128
|
-
'solid': cloneRules(DEFAULT_SOLID_RULES)
|
|
158
|
+
'solid': cloneRules(DEFAULT_SOLID_RULES),
|
|
159
|
+
'arch': cloneRules(DEFAULT_ARCH_RULES)
|
|
129
160
|
},
|
|
130
161
|
output: {
|
|
131
162
|
format: 'console',
|
|
@@ -241,7 +272,11 @@ function mergeConfig(
|
|
|
241
272
|
defaults.rules['clean-code'],
|
|
242
273
|
user.rules?.['clean-code']
|
|
243
274
|
),
|
|
244
|
-
'solid': mergeGroup(defaults.rules['solid'], user.rules?.['solid'])
|
|
275
|
+
'solid': mergeGroup(defaults.rules['solid'], user.rules?.['solid']),
|
|
276
|
+
// A `.principlesrc` written before the arch group existed has no `arch`
|
|
277
|
+
// key at all; `mergeGroup` then returns the defaults unchanged, which is
|
|
278
|
+
// what AC-COMPAT-01 pins down.
|
|
279
|
+
'arch': mergeGroup(defaults.rules['arch'], user.rules?.['arch'])
|
|
245
280
|
}
|
|
246
281
|
};
|
|
247
282
|
}
|