@fmatar/archlens-skill 0.1.0-Beta-10 → 0.1.0-Beta-11

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/checker.js +282 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fmatar/archlens-skill",
3
- "version": "0.1.0-Beta-10",
3
+ "version": "0.1.0-Beta-11",
4
4
  "description": "Cross-platform CLI and installer for Archlens Clean Architecture governance policies and AI assistant skills",
5
5
  "main": "src/installer.js",
6
6
  "type": "module",
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,63 @@ 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
+ if (filePath.endsWith('.py')) {
741
+ const classMatches = content.match(/^\s*class\s+[A-Za-z_][A-Za-z0-9_]*(?:\s*\(([^)]*)\))?\s*:/gm) || [];
742
+ const hasAbstractMethod = /@abstractmethod\b/.test(content);
743
+ for (const m of classMatches) {
744
+ totalTypes++;
745
+ if (/\b(?:Protocol|ABC|ABCMeta)\b/.test(m) || (classMatches.length === 1 && hasAbstractMethod)) {
746
+ abstractTypes++;
747
+ }
748
+ }
749
+ } else {
750
+ const ifaceMatches = content.match(/\binterface\s+[A-Za-z_$][A-Za-z0-9_$]*/g) || [];
751
+ const absClassMatches = content.match(/\babstract\s+class\s+[A-Za-z_$][A-Za-z0-9_$]*/g) || [];
752
+ const concreteClassMatches =
753
+ content.replace(/\babstract\s+class\b/g, '').match(/\bclass\s+[A-Za-z_$][A-Za-z0-9_$]*/g) || [];
754
+ const enumMatches = content.match(/\benum\s+[A-Za-z_$][A-Za-z0-9_$]*/g) || [];
755
+ abstractTypes = ifaceMatches.length + absClassMatches.length;
756
+ totalTypes = abstractTypes + concreteClassMatches.length + enumMatches.length;
757
+ }
758
+ if (totalTypes === 0) {
759
+ totalTypes = 1;
760
+ }
761
+ return { totalTypes, abstractTypes };
762
+ }
763
+
764
+ export function calculateMaxMainSequenceDistance(pkgTypeStats, packageEdges) {
765
+ let maxDistance = 0.0;
766
+ const allPackages = new Set([
767
+ ...Array.from(pkgTypeStats.keys()),
768
+ ...Array.from(packageEdges.keys())
769
+ ]);
770
+
771
+ for (const pkg of allPackages) {
772
+ const ce = packageEdges.has(pkg) ? packageEdges.get(pkg).size : 0;
773
+ let ca = 0;
774
+ for (const [otherPkg, targets] of packageEdges.entries()) {
775
+ if (otherPkg !== pkg && targets.has(pkg)) {
776
+ ca++;
777
+ }
778
+ }
779
+ if (ca + ce === 0) {
780
+ continue;
781
+ }
782
+ const stats = pkgTypeStats.get(pkg) || { totalTypes: 1, abstractTypes: 0 };
783
+ const abstractness = stats.totalTypes > 0 ? stats.abstractTypes / stats.totalTypes : 0.0;
784
+ const instability = ce / (ca + ce);
785
+ const distance = Math.abs(abstractness + instability - 1.0);
786
+ if (distance > maxDistance) {
787
+ maxDistance = distance;
788
+ }
789
+ }
790
+
791
+ return Math.round(maxDistance * 100) / 100;
792
+ }
793
+
526
794
  export async function checkArchitecture(projectRoot, options = {}) {
527
795
  const root = path.resolve(projectRoot || '.');
528
796
  const policy = loadProjectPolicy(root);
@@ -552,8 +820,12 @@ export async function checkArchitecture(projectRoot, options = {}) {
552
820
  (f.endsWith('.java') ||
553
821
  f.endsWith('.kt') ||
554
822
  f.endsWith('.ts') ||
823
+ f.endsWith('.tsx') ||
555
824
  f.endsWith('.js') ||
825
+ f.endsWith('.jsx') ||
826
+ f.endsWith('.svelte') ||
556
827
  f.endsWith('.py')) &&
828
+ !f.endsWith('.d.ts') &&
557
829
  !f.startsWith('test_') &&
558
830
  !f.endsWith('_test.py'),
559
831
  500
@@ -561,6 +833,7 @@ export async function checkArchitecture(projectRoot, options = {}) {
561
833
 
562
834
  const violations = [];
563
835
  const packageEdges = new Map();
836
+ const pkgTypeStats = new Map();
564
837
 
565
838
  for (const file of sourceFiles) {
566
839
  try {
@@ -580,6 +853,13 @@ export async function checkArchitecture(projectRoot, options = {}) {
580
853
 
581
854
  if (fileLevel === null) continue;
582
855
 
856
+ const fileTypeCounts = countFileTypeAbstractions(content, file);
857
+ const prevStats = pkgTypeStats.get(filePackage) || { totalTypes: 0, abstractTypes: 0 };
858
+ pkgTypeStats.set(filePackage, {
859
+ totalTypes: prevStats.totalTypes + fileTypeCounts.totalTypes,
860
+ abstractTypes: prevStats.abstractTypes + fileTypeCounts.abstractTypes
861
+ });
862
+
583
863
  const fileImports = extractFileImports(content, file, fullSrc);
584
864
  const seenLinePkgViolations = new Set();
585
865
  for (const { imported, line } of fileImports) {
@@ -623,13 +903,14 @@ export async function checkArchitecture(projectRoot, options = {}) {
623
903
 
624
904
  const cycles = detectPackageCycles(packageEdges);
625
905
  const failOnCycles = Boolean(options.detectCycles);
906
+ const maxDistance = calculateMaxMainSequenceDistance(pkgTypeStats, packageEdges);
626
907
 
627
908
  const fitness = calculateArchitectureFitness(
628
909
  {
629
910
  violationsCount: violations.length,
630
911
  cyclesCount: cycles.length,
631
912
  screamingScore: screaming.score,
632
- maxDistance: 0.12
913
+ maxDistance
633
914
  },
634
915
  policy.fitness || {}
635
916
  );