@fmatar/archlens-skill 0.1.0-Beta-10 → 1.1.0-dev.58
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/README.md +33 -10
- package/bin/index.js +14 -2
- package/llms.txt +17 -3
- package/package.json +1 -1
- package/skills/archlens/SKILL.md +14 -1
- package/src/analyzer.js +135 -0
- package/src/checker.js +445 -5
- package/src/docker-runner.js +108 -5
- package/src/mcp-registry.js +185 -7
- package/src/mcp-server.js +339 -8
package/src/checker.js
CHANGED
|
@@ -458,7 +458,218 @@ function extractPythonStatements(content) {
|
|
|
458
458
|
return statements;
|
|
459
459
|
}
|
|
460
460
|
|
|
461
|
+
export function extractTsJsStatements(content) {
|
|
462
|
+
const statements = [];
|
|
463
|
+
const rawLines = content.split(/\r?\n/);
|
|
464
|
+
const strippedLines = [];
|
|
465
|
+
let inBlockComment = false;
|
|
466
|
+
|
|
467
|
+
for (let lineIdx = 0; lineIdx < rawLines.length; lineIdx++) {
|
|
468
|
+
const line = rawLines[lineIdx];
|
|
469
|
+
const lineNo = lineIdx + 1;
|
|
470
|
+
let clean = '';
|
|
471
|
+
let inSingle = false;
|
|
472
|
+
let inDouble = false;
|
|
473
|
+
let inTemplate = false;
|
|
474
|
+
let i = 0;
|
|
475
|
+
|
|
476
|
+
while (i < line.length) {
|
|
477
|
+
if (inBlockComment) {
|
|
478
|
+
const endIdx = line.indexOf('*/', i);
|
|
479
|
+
if (endIdx === -1) {
|
|
480
|
+
i = line.length;
|
|
481
|
+
} else {
|
|
482
|
+
inBlockComment = false;
|
|
483
|
+
i = endIdx + 2;
|
|
484
|
+
}
|
|
485
|
+
continue;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
const c = line[i];
|
|
489
|
+
if ((inSingle || inDouble || inTemplate) && c === '\\' && i + 1 < line.length) {
|
|
490
|
+
clean += c + line[i + 1];
|
|
491
|
+
i += 2;
|
|
492
|
+
continue;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
if (!inDouble && !inTemplate && c === "'") {
|
|
496
|
+
inSingle = !inSingle;
|
|
497
|
+
clean += c;
|
|
498
|
+
i++;
|
|
499
|
+
continue;
|
|
500
|
+
}
|
|
501
|
+
if (!inSingle && !inTemplate && c === '"') {
|
|
502
|
+
inDouble = !inDouble;
|
|
503
|
+
clean += c;
|
|
504
|
+
i++;
|
|
505
|
+
continue;
|
|
506
|
+
}
|
|
507
|
+
if (!inSingle && !inDouble && c === '`') {
|
|
508
|
+
inTemplate = !inTemplate;
|
|
509
|
+
clean += c;
|
|
510
|
+
i++;
|
|
511
|
+
continue;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
if (!inSingle && !inDouble && !inTemplate && i + 1 < line.length) {
|
|
515
|
+
const next = line[i + 1];
|
|
516
|
+
if (c === '/' && next === '/') break;
|
|
517
|
+
if (c === '/' && next === '*') {
|
|
518
|
+
inBlockComment = true;
|
|
519
|
+
i += 2;
|
|
520
|
+
continue;
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
clean += c;
|
|
525
|
+
i++;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
const trimmed = clean.trim();
|
|
529
|
+
if (trimmed) {
|
|
530
|
+
strippedLines.push({ text: trimmed, line: lineNo });
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
function countBraces(str) {
|
|
535
|
+
let delta = 0;
|
|
536
|
+
let inS = false;
|
|
537
|
+
let inD = false;
|
|
538
|
+
let inT = false;
|
|
539
|
+
for (let i = 0; i < str.length; i++) {
|
|
540
|
+
const c = str[i];
|
|
541
|
+
if (c === '\\' && i + 1 < str.length) {
|
|
542
|
+
i++;
|
|
543
|
+
continue;
|
|
544
|
+
}
|
|
545
|
+
if (!inD && !inT && c === "'") inS = !inS;
|
|
546
|
+
else if (!inS && !inT && c === '"') inD = !inD;
|
|
547
|
+
else if (!inS && !inD && c === '`') inT = !inT;
|
|
548
|
+
else if (!inS && !inD && !inT) {
|
|
549
|
+
if (c === '{') delta++;
|
|
550
|
+
else if (c === '}') delta--;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
return delta;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
function isIncompleteTsImportOrExport(str) {
|
|
557
|
+
const t = str.trim();
|
|
558
|
+
if (/^import\s*(?:['"]|\()/.test(t)) return false;
|
|
559
|
+
if (/^import(?:\s+|\{)/.test(t)) {
|
|
560
|
+
if (countBraces(t) > 0) return true;
|
|
561
|
+
return !/\bfrom\s*['"][^'"]+['"]\s*;?\s*$/.test(t) && !t.endsWith(';');
|
|
562
|
+
}
|
|
563
|
+
if (/^export\s+(?:type\s+)?(?:\*|\{)/.test(t)) {
|
|
564
|
+
if (countBraces(t) > 0) return true;
|
|
565
|
+
if (/\bfrom\s*$/.test(t) || /\bfrom\s+['"][^'"]*$/.test(t)) return true;
|
|
566
|
+
}
|
|
567
|
+
return false;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
let acc = '';
|
|
571
|
+
let accLine = 1;
|
|
572
|
+
let accumulating = false;
|
|
573
|
+
|
|
574
|
+
for (const item of strippedLines) {
|
|
575
|
+
if (!accumulating) {
|
|
576
|
+
if (isIncompleteTsImportOrExport(item.text)) {
|
|
577
|
+
accumulating = true;
|
|
578
|
+
acc = item.text;
|
|
579
|
+
accLine = item.line;
|
|
580
|
+
} else {
|
|
581
|
+
statements.push(item);
|
|
582
|
+
}
|
|
583
|
+
} else {
|
|
584
|
+
acc += ' ' + item.text;
|
|
585
|
+
if (!isIncompleteTsImportOrExport(acc)) {
|
|
586
|
+
statements.push({ text: acc, line: accLine });
|
|
587
|
+
acc = '';
|
|
588
|
+
accumulating = false;
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
if (acc) {
|
|
594
|
+
statements.push({ text: acc, line: accLine });
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
return statements;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
function resolveTsJsImportSpec(spec, filePath, fullSrc) {
|
|
601
|
+
let raw = spec.trim();
|
|
602
|
+
if (raw.startsWith('.')) {
|
|
603
|
+
let relDir = '';
|
|
604
|
+
if (fullSrc && filePath) {
|
|
605
|
+
let relFile = path.relative(fullSrc, filePath).replace(/\\/g, '/');
|
|
606
|
+
if (relFile.startsWith('src/')) relFile = relFile.slice(4);
|
|
607
|
+
const dir = path.posix.dirname(relFile);
|
|
608
|
+
relDir = dir === '.' ? '' : dir;
|
|
609
|
+
}
|
|
610
|
+
const combined = relDir ? `${relDir}/${raw}` : raw;
|
|
611
|
+
return path.posix
|
|
612
|
+
.normalize(combined)
|
|
613
|
+
.replace(/\.(svelte\.ts|svelte\.js|ts|tsx|js|jsx|svelte)$/, '')
|
|
614
|
+
.replace(/\/index$/, '')
|
|
615
|
+
.replace(/\//g, '.');
|
|
616
|
+
}
|
|
617
|
+
if (raw.startsWith('@/') || raw.startsWith('~/') || raw.startsWith('#/')) {
|
|
618
|
+
return raw.slice(2).replace(/\.(ts|tsx|js|jsx)$/, '').replace(/\//g, '.');
|
|
619
|
+
}
|
|
620
|
+
if (raw.startsWith('#lib/') || raw.startsWith('$lib/')) {
|
|
621
|
+
return 'lib.' + raw.slice(5).replace(/\.(ts|tsx|js|jsx)$/, '').replace(/\//g, '.');
|
|
622
|
+
}
|
|
623
|
+
if (raw.startsWith('@workspace/')) {
|
|
624
|
+
return raw.slice(11).replace(/\.(ts|tsx|js|jsx)$/, '').replace(/\//g, '.');
|
|
625
|
+
}
|
|
626
|
+
return raw.replace(/\//g, '.');
|
|
627
|
+
}
|
|
628
|
+
|
|
461
629
|
export function extractFileImports(content, filePath, fullSrc) {
|
|
630
|
+
if (/\.(ts|tsx|js|jsx|svelte)$/.test(filePath)) {
|
|
631
|
+
const results = [];
|
|
632
|
+
const importFromRegex = /^import\s+(?:type\s+)?(.+?)\s+from\s+['"]([^'"]+)['"]\s*;?$/;
|
|
633
|
+
const sideEffectRegex = /^import\s+['"]([^'"]+)['"]\s*;?$/;
|
|
634
|
+
const exportFromRegex =
|
|
635
|
+
/^export\s+(?:type\s+)?(\*\s+as\s+[a-zA-Z0-9_$]+|\*|\{[^}]*\})\s*from\s+['"]([^'"]+)['"]\s*;?$/;
|
|
636
|
+
const reqRegex = /\brequire\s*\(\s*['"]([^'"]+)['"]\s*\)/g;
|
|
637
|
+
const dynRegex = /\bimport\s*\(\s*['"]([^'"]+)['"]\s*\)/g;
|
|
638
|
+
|
|
639
|
+
for (const { text, line } of extractTsJsStatements(content)) {
|
|
640
|
+
const sideMatch = sideEffectRegex.exec(text);
|
|
641
|
+
if (sideMatch) {
|
|
642
|
+
results.push({ imported: resolveTsJsImportSpec(sideMatch[1], filePath, fullSrc), line });
|
|
643
|
+
continue;
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
const impMatch = importFromRegex.exec(text);
|
|
647
|
+
if (impMatch) {
|
|
648
|
+
const resolvedMod = resolveTsJsImportSpec(impMatch[2], filePath, fullSrc);
|
|
649
|
+
results.push({ imported: resolvedMod, line });
|
|
650
|
+
continue;
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
const expMatch = exportFromRegex.exec(text);
|
|
654
|
+
if (expMatch) {
|
|
655
|
+
const resolvedMod = resolveTsJsImportSpec(expMatch[2], filePath, fullSrc);
|
|
656
|
+
results.push({ imported: resolvedMod, line });
|
|
657
|
+
continue;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
let m;
|
|
661
|
+
reqRegex.lastIndex = 0;
|
|
662
|
+
while ((m = reqRegex.exec(text)) !== null) {
|
|
663
|
+
results.push({ imported: resolveTsJsImportSpec(m[1], filePath, fullSrc), line });
|
|
664
|
+
}
|
|
665
|
+
dynRegex.lastIndex = 0;
|
|
666
|
+
while ((m = dynRegex.exec(text)) !== null) {
|
|
667
|
+
results.push({ imported: resolveTsJsImportSpec(m[1], filePath, fullSrc), line });
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
return results;
|
|
671
|
+
}
|
|
672
|
+
|
|
462
673
|
if (!filePath.endsWith('.py')) {
|
|
463
674
|
const results = [];
|
|
464
675
|
const importRegex = /import\s+(?:static\s+)?([a-zA-Z0-9_.]+)/;
|
|
@@ -523,6 +734,87 @@ export function extractFileImports(content, filePath, fullSrc) {
|
|
|
523
734
|
return results;
|
|
524
735
|
}
|
|
525
736
|
|
|
737
|
+
function countFileTypeAbstractions(content, filePath) {
|
|
738
|
+
let totalTypes = 0;
|
|
739
|
+
let abstractTypes = 0;
|
|
740
|
+
let recordTypes = 0;
|
|
741
|
+
if (filePath.endsWith('.py')) {
|
|
742
|
+
const classMatches = content.match(/^\s*class\s+[A-Za-z_][A-Za-z0-9_]*(?:\s*\(([^)]*)\))?\s*:/gm) || [];
|
|
743
|
+
const hasAbstractMethod = /@abstractmethod\b/.test(content);
|
|
744
|
+
for (const m of classMatches) {
|
|
745
|
+
totalTypes++;
|
|
746
|
+
if (/\b(?:Protocol|ABC|ABCMeta)\b/.test(m) || (classMatches.length === 1 && hasAbstractMethod)) {
|
|
747
|
+
abstractTypes++;
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
} else {
|
|
751
|
+
const cleaned = content
|
|
752
|
+
.replace(/\/\*[\s\S]*?\*\//g, ' ')
|
|
753
|
+
.replace(/\/\/.*$/gm, ' ')
|
|
754
|
+
.replace(/"""[\s\S]*?"""/g, ' ')
|
|
755
|
+
.replace(/"(?:\\.|[^"\\])*"/g, ' ')
|
|
756
|
+
.replace(/'(?:\\.|[^'\\])*'/g, ' ');
|
|
757
|
+
const ifaceMatches = cleaned.match(/\binterface\s+[A-Za-z_$][A-Za-z0-9_$]*/g) || [];
|
|
758
|
+
const absClassMatches = cleaned.match(/\babstract\s+class\s+[A-Za-z_$][A-Za-z0-9_$]*/g) || [];
|
|
759
|
+
const recordMatches = cleaned.match(/\brecord\s+[A-Za-z_$][A-Za-z0-9_$]*/g) || [];
|
|
760
|
+
const concreteClassMatches =
|
|
761
|
+
cleaned.replace(/\babstract\s+class\b/g, '').match(/\bclass\s+[A-Za-z_$][A-Za-z0-9_$]*/g) || [];
|
|
762
|
+
const enumMatches = cleaned.match(/\benum\s+[A-Za-z_$][A-Za-z0-9_$]*/g) || [];
|
|
763
|
+
abstractTypes = ifaceMatches.length + absClassMatches.length;
|
|
764
|
+
recordTypes = recordMatches.length;
|
|
765
|
+
totalTypes = abstractTypes + recordTypes + concreteClassMatches.length + enumMatches.length;
|
|
766
|
+
}
|
|
767
|
+
if (totalTypes === 0) {
|
|
768
|
+
totalTypes = 1;
|
|
769
|
+
}
|
|
770
|
+
return { totalTypes, abstractTypes, recordTypes };
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
export function calculateMaxMainSequenceDistance(pkgTypeStats, packageEdges, pkgCouplingCounts = null) {
|
|
774
|
+
let maxDistance = 0.0;
|
|
775
|
+
const allPackages = new Set([
|
|
776
|
+
...Array.from(pkgTypeStats.keys()),
|
|
777
|
+
...Array.from(packageEdges.keys())
|
|
778
|
+
]);
|
|
779
|
+
|
|
780
|
+
for (const pkg of allPackages) {
|
|
781
|
+
let ce = 0;
|
|
782
|
+
let ca = 0;
|
|
783
|
+
if (pkgCouplingCounts && pkgCouplingCounts.has(pkg)) {
|
|
784
|
+
const counts = pkgCouplingCounts.get(pkg);
|
|
785
|
+
ca = counts.ca || 0;
|
|
786
|
+
ce = counts.ce || 0;
|
|
787
|
+
} else {
|
|
788
|
+
ce = packageEdges.has(pkg) ? packageEdges.get(pkg).size : 0;
|
|
789
|
+
for (const [otherPkg, targets] of packageEdges.entries()) {
|
|
790
|
+
if (otherPkg !== pkg && targets.has(pkg)) {
|
|
791
|
+
ca++;
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
if (ca + ce === 0) {
|
|
796
|
+
continue;
|
|
797
|
+
}
|
|
798
|
+
const stats = pkgTypeStats.get(pkg) || { totalTypes: 1, abstractTypes: 0, recordTypes: 0 };
|
|
799
|
+
const instability = ce / (ca + ce);
|
|
800
|
+
const strictAbstractness = stats.totalTypes > 0 ? stats.abstractTypes / stats.totalTypes : 0.0;
|
|
801
|
+
const immutableAbstractness =
|
|
802
|
+
stats.totalTypes > 0
|
|
803
|
+
? (stats.abstractTypes + (stats.recordTypes || 0)) / stats.totalTypes
|
|
804
|
+
: 0.0;
|
|
805
|
+
const abstractness = Math.min(
|
|
806
|
+
immutableAbstractness,
|
|
807
|
+
Math.max(strictAbstractness, 1.0 - instability)
|
|
808
|
+
);
|
|
809
|
+
const distance = Math.abs(abstractness + instability - 1.0);
|
|
810
|
+
if (distance > maxDistance) {
|
|
811
|
+
maxDistance = distance;
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
return Math.round(maxDistance * 100) / 100;
|
|
816
|
+
}
|
|
817
|
+
|
|
526
818
|
export async function checkArchitecture(projectRoot, options = {}) {
|
|
527
819
|
const root = path.resolve(projectRoot || '.');
|
|
528
820
|
const policy = loadProjectPolicy(root);
|
|
@@ -544,6 +836,7 @@ export async function checkArchitecture(projectRoot, options = {}) {
|
|
|
544
836
|
levels.forEach((tier, lvl) => {
|
|
545
837
|
tier.forEach((p) => pkgToLevel.set(p.toLowerCase(), lvl));
|
|
546
838
|
});
|
|
839
|
+
const sortedPkgEntries = Array.from(pkgToLevel.entries()).sort((a, b) => b[0].length - a[0].length);
|
|
547
840
|
|
|
548
841
|
const fullSrc = path.join(root, policy.src || 'src');
|
|
549
842
|
const sourceFiles = findMatchingFiles(
|
|
@@ -552,8 +845,12 @@ export async function checkArchitecture(projectRoot, options = {}) {
|
|
|
552
845
|
(f.endsWith('.java') ||
|
|
553
846
|
f.endsWith('.kt') ||
|
|
554
847
|
f.endsWith('.ts') ||
|
|
848
|
+
f.endsWith('.tsx') ||
|
|
555
849
|
f.endsWith('.js') ||
|
|
850
|
+
f.endsWith('.jsx') ||
|
|
851
|
+
f.endsWith('.svelte') ||
|
|
556
852
|
f.endsWith('.py')) &&
|
|
853
|
+
!f.endsWith('.d.ts') &&
|
|
557
854
|
!f.startsWith('test_') &&
|
|
558
855
|
!f.endsWith('_test.py'),
|
|
559
856
|
500
|
|
@@ -561,17 +858,20 @@ export async function checkArchitecture(projectRoot, options = {}) {
|
|
|
561
858
|
|
|
562
859
|
const violations = [];
|
|
563
860
|
const packageEdges = new Map();
|
|
861
|
+
const pkgTypeStats = new Map();
|
|
862
|
+
const pkgCouplingCounts = new Map();
|
|
564
863
|
|
|
565
864
|
for (const file of sourceFiles) {
|
|
566
865
|
try {
|
|
567
866
|
const content = fs.readFileSync(file, 'utf8');
|
|
568
867
|
const relPath = path.relative(root, file);
|
|
569
868
|
const lowerRel = relPath.toLowerCase();
|
|
869
|
+
const normalizedRel = lowerRel.replace(/[\\/]+/g, '.');
|
|
570
870
|
|
|
571
871
|
let fileLevel = null;
|
|
572
872
|
let filePackage = null;
|
|
573
|
-
for (const [pkg, lvl] of
|
|
574
|
-
if (lowerRel.includes(pkg)) {
|
|
873
|
+
for (const [pkg, lvl] of sortedPkgEntries) {
|
|
874
|
+
if (lowerRel.includes(pkg) || normalizedRel.includes(`.${pkg}.`)) {
|
|
575
875
|
fileLevel = lvl;
|
|
576
876
|
filePackage = pkg;
|
|
577
877
|
break;
|
|
@@ -580,17 +880,38 @@ export async function checkArchitecture(projectRoot, options = {}) {
|
|
|
580
880
|
|
|
581
881
|
if (fileLevel === null) continue;
|
|
582
882
|
|
|
883
|
+
const fileTypeCounts = countFileTypeAbstractions(content, file);
|
|
884
|
+
const prevStats = pkgTypeStats.get(filePackage) || {
|
|
885
|
+
totalTypes: 0,
|
|
886
|
+
abstractTypes: 0,
|
|
887
|
+
recordTypes: 0
|
|
888
|
+
};
|
|
889
|
+
pkgTypeStats.set(filePackage, {
|
|
890
|
+
totalTypes: prevStats.totalTypes + fileTypeCounts.totalTypes,
|
|
891
|
+
abstractTypes: prevStats.abstractTypes + fileTypeCounts.abstractTypes,
|
|
892
|
+
recordTypes: (prevStats.recordTypes || 0) + (fileTypeCounts.recordTypes || 0)
|
|
893
|
+
});
|
|
894
|
+
|
|
583
895
|
const fileImports = extractFileImports(content, file, fullSrc);
|
|
584
896
|
const seenLinePkgViolations = new Set();
|
|
585
897
|
for (const { imported, line } of fileImports) {
|
|
586
898
|
const lowerImport = imported.toLowerCase();
|
|
587
|
-
|
|
588
|
-
|
|
899
|
+
const normalizedImport = lowerImport.replace(/[\\/]+/g, '.');
|
|
900
|
+
for (const [targetPkg, targetLvl] of sortedPkgEntries) {
|
|
901
|
+
if (lowerImport.includes(targetPkg) || normalizedImport.includes(targetPkg)) {
|
|
589
902
|
if (filePackage && filePackage !== targetPkg) {
|
|
590
903
|
if (!packageEdges.has(filePackage)) {
|
|
591
904
|
packageEdges.set(filePackage, new Set());
|
|
592
905
|
}
|
|
593
906
|
packageEdges.get(filePackage).add(targetPkg);
|
|
907
|
+
|
|
908
|
+
const srcCounts = pkgCouplingCounts.get(filePackage) || { ca: 0, ce: 0 };
|
|
909
|
+
srcCounts.ce += 1;
|
|
910
|
+
pkgCouplingCounts.set(filePackage, srcCounts);
|
|
911
|
+
|
|
912
|
+
const dstCounts = pkgCouplingCounts.get(targetPkg) || { ca: 0, ce: 0 };
|
|
913
|
+
dstCounts.ca += 1;
|
|
914
|
+
pkgCouplingCounts.set(targetPkg, dstCounts);
|
|
594
915
|
}
|
|
595
916
|
if (fileLevel < targetLvl) {
|
|
596
917
|
const dedupKey = `${line}:${targetPkg}`;
|
|
@@ -599,8 +920,10 @@ export async function checkArchitecture(projectRoot, options = {}) {
|
|
|
599
920
|
const baseName = path.basename(file, path.extname(file));
|
|
600
921
|
violations.push({
|
|
601
922
|
fromFile: relPath,
|
|
923
|
+
fromPkg: filePackage || `L${fileLevel}`,
|
|
602
924
|
fromTier: tierNames[fileLevel] || `Level ${fileLevel}`,
|
|
603
925
|
toImport: imported,
|
|
926
|
+
toPkg: targetPkg,
|
|
604
927
|
toTier: tierNames[targetLvl] || `Level ${targetLvl}`,
|
|
605
928
|
portName: `${baseName}Port`,
|
|
606
929
|
line
|
|
@@ -623,13 +946,14 @@ export async function checkArchitecture(projectRoot, options = {}) {
|
|
|
623
946
|
|
|
624
947
|
const cycles = detectPackageCycles(packageEdges);
|
|
625
948
|
const failOnCycles = Boolean(options.detectCycles);
|
|
949
|
+
const maxDistance = calculateMaxMainSequenceDistance(pkgTypeStats, packageEdges, pkgCouplingCounts);
|
|
626
950
|
|
|
627
951
|
const fitness = calculateArchitectureFitness(
|
|
628
952
|
{
|
|
629
953
|
violationsCount: violations.length,
|
|
630
954
|
cyclesCount: cycles.length,
|
|
631
955
|
screamingScore: screaming.score,
|
|
632
|
-
maxDistance
|
|
956
|
+
maxDistance
|
|
633
957
|
},
|
|
634
958
|
policy.fitness || {}
|
|
635
959
|
);
|
|
@@ -655,9 +979,18 @@ export async function checkArchitecture(projectRoot, options = {}) {
|
|
|
655
979
|
? suggestFeatureClusters(allKnownPackages, sourceFiles)
|
|
656
980
|
: null;
|
|
657
981
|
|
|
982
|
+
const serializedEdges = [];
|
|
983
|
+
for (const [fromPkg, targets] of packageEdges.entries()) {
|
|
984
|
+
for (const toPkg of targets) {
|
|
985
|
+
serializedEdges.push({ from: fromPkg, to: toPkg });
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
|
|
658
989
|
return {
|
|
659
990
|
passed,
|
|
660
991
|
projectTitle: policy.title || path.basename(root),
|
|
992
|
+
levels: policy.levels || [],
|
|
993
|
+
edges: serializedEdges,
|
|
661
994
|
sourceFilesCount: sourceFiles.length,
|
|
662
995
|
packagesCount: (policy.order || []).length,
|
|
663
996
|
violationsCount: violations.length,
|
|
@@ -928,3 +1261,110 @@ export function generateSarifReport(report, options = {}) {
|
|
|
928
1261
|
export function generateJsonReport(report) {
|
|
929
1262
|
return JSON.stringify(report, null, 2);
|
|
930
1263
|
}
|
|
1264
|
+
|
|
1265
|
+
const MERMAID_RING_TITLES = [
|
|
1266
|
+
'Ring L0: Domain Core',
|
|
1267
|
+
'Ring L1: Application',
|
|
1268
|
+
'Ring L2: Adapters',
|
|
1269
|
+
'Ring L3: Infrastructure'
|
|
1270
|
+
];
|
|
1271
|
+
|
|
1272
|
+
function sanitizeMermaidId(raw) {
|
|
1273
|
+
return String(raw || 'node').replace(/[^a-zA-Z0-9_]/g, '_');
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
function escapeMermaidLabel(raw) {
|
|
1277
|
+
return String(raw || '').replace(/"/g, "'").replace(/\n/g, ' ');
|
|
1278
|
+
}
|
|
1279
|
+
|
|
1280
|
+
/**
|
|
1281
|
+
* Generate Mermaid flowchart TB diagram representing Concentric Rings or DIP Refactoring topology.
|
|
1282
|
+
*/
|
|
1283
|
+
export function generateMermaidReport(report, options = {}) {
|
|
1284
|
+
if (!report) {
|
|
1285
|
+
return 'flowchart TB\n Empty["No Architecture Report Available"]\n';
|
|
1286
|
+
}
|
|
1287
|
+
const diagramType = String(options.diagramType || 'COMPONENT_RINGS').toUpperCase();
|
|
1288
|
+
const violations = report.violations || [];
|
|
1289
|
+
|
|
1290
|
+
if (diagramType.includes('DIP')) {
|
|
1291
|
+
const v = violations[0] || {};
|
|
1292
|
+
const srcName = v.fromFile ? path.basename(v.fromFile, path.extname(v.fromFile)) : 'InnerDomainService';
|
|
1293
|
+
const tgtName = v.toImport ? v.toImport.split('.').pop() : 'OuterAdapter';
|
|
1294
|
+
const portName = v.portName || `${tgtName}Port`;
|
|
1295
|
+
return [
|
|
1296
|
+
'flowchart TB',
|
|
1297
|
+
' subgraph Before["❌ Before: Outward Dependency Violation"]',
|
|
1298
|
+
' direction TB',
|
|
1299
|
+
` B_Src["${escapeMermaidLabel(srcName)}"] -->|"❌ Violates Dependency Rule"| B_Tgt["${escapeMermaidLabel(tgtName)}"]`,
|
|
1300
|
+
' end',
|
|
1301
|
+
' subgraph After["✅ After: DIP Port Inversion"]',
|
|
1302
|
+
' direction TB',
|
|
1303
|
+
` A_Src["${escapeMermaidLabel(srcName)}"] -->|"depends on"| A_Port["<<interface>> ${escapeMermaidLabel(portName)}"]`,
|
|
1304
|
+
` A_Tgt["${escapeMermaidLabel(tgtName)}"] -.->|"implements"| A_Port`,
|
|
1305
|
+
' end',
|
|
1306
|
+
' linkStyle 0 stroke:#ef4444,stroke-width:2.5px,color:#fca5a5',
|
|
1307
|
+
' linkStyle 1 stroke:#10b981,stroke-width:2px,color:#6ee7b7',
|
|
1308
|
+
' linkStyle 2 stroke:#10b981,stroke-width:2px,color:#6ee7b7',
|
|
1309
|
+
''
|
|
1310
|
+
].join('\n');
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1313
|
+
const levels = Array.isArray(report.levels) && report.levels.length > 0
|
|
1314
|
+
? report.levels
|
|
1315
|
+
: [['domain'], ['application'], ['adapters'], ['infrastructure']];
|
|
1316
|
+
|
|
1317
|
+
const lines = ['flowchart TB'];
|
|
1318
|
+
levels.forEach((pkgs, lvlIdx) => {
|
|
1319
|
+
if (!Array.isArray(pkgs) || pkgs.length === 0) return;
|
|
1320
|
+
const title = MERMAID_RING_TITLES[lvlIdx] || `Ring L${lvlIdx}`;
|
|
1321
|
+
lines.push(` subgraph Ring_L${lvlIdx}["${title}"]`);
|
|
1322
|
+
for (const pkg of pkgs) {
|
|
1323
|
+
lines.push(` ${sanitizeMermaidId('comp_' + pkg)}["${escapeMermaidLabel(pkg)}"]`);
|
|
1324
|
+
}
|
|
1325
|
+
lines.push(' end');
|
|
1326
|
+
});
|
|
1327
|
+
|
|
1328
|
+
const bundledViolations = new Map();
|
|
1329
|
+
for (const v of violations) {
|
|
1330
|
+
const fromPkg = v.fromPkg || v.fromTier || 'inner';
|
|
1331
|
+
const toPkg = v.toPkg || v.toTier || 'outer';
|
|
1332
|
+
const key = `${fromPkg}->${toPkg}`;
|
|
1333
|
+
bundledViolations.set(key, (bundledViolations.get(key) || 0) + 1);
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1336
|
+
let edgeIdx = 0;
|
|
1337
|
+
const violationEdgeIndices = [];
|
|
1338
|
+
const conformingEdgeIndices = [];
|
|
1339
|
+
const emittedPairs = new Set();
|
|
1340
|
+
|
|
1341
|
+
for (const [key, count] of bundledViolations.entries()) {
|
|
1342
|
+
const [fromPkg, toPkg] = key.split('->');
|
|
1343
|
+
const fromId = sanitizeMermaidId('comp_' + fromPkg);
|
|
1344
|
+
const toId = sanitizeMermaidId('comp_' + toPkg);
|
|
1345
|
+
lines.push(` ${fromId} -->|"❌ VIOLATION (${count})"| ${toId}`);
|
|
1346
|
+
emittedPairs.add(key);
|
|
1347
|
+
violationEdgeIndices.push(edgeIdx++);
|
|
1348
|
+
}
|
|
1349
|
+
|
|
1350
|
+
if (Array.isArray(report.edges)) {
|
|
1351
|
+
for (const edge of report.edges) {
|
|
1352
|
+
const key = `${edge.from}->${edge.to}`;
|
|
1353
|
+
if (emittedPairs.has(key)) continue;
|
|
1354
|
+
emittedPairs.add(key);
|
|
1355
|
+
const fromId = sanitizeMermaidId('comp_' + edge.from);
|
|
1356
|
+
const toId = sanitizeMermaidId('comp_' + edge.to);
|
|
1357
|
+
lines.push(` ${fromId} --> ${toId}`);
|
|
1358
|
+
conformingEdgeIndices.push(edgeIdx++);
|
|
1359
|
+
}
|
|
1360
|
+
}
|
|
1361
|
+
|
|
1362
|
+
for (const idx of violationEdgeIndices) {
|
|
1363
|
+
lines.push(` linkStyle ${idx} stroke:#ef4444,stroke-width:2.5px,color:#fca5a5`);
|
|
1364
|
+
}
|
|
1365
|
+
for (const idx of conformingEdgeIndices) {
|
|
1366
|
+
lines.push(` linkStyle ${idx} stroke:#64748b,stroke-width:1.5px`);
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1369
|
+
return lines.join('\n') + '\n';
|
|
1370
|
+
}
|