ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/design-components/checks/no-level-up.ts

checks/no-level-up.ts

35 líneas, 1.6 KB.

El archivo tal como está en el paquete: el código y sus comentarios no se traducen.

  1. // DSC-R03. A component uses only components of its own level or below (primitive < composite < block < page), and the
  2. // uses never go in a circle. A primitive that uses a composite is the usual mistake: the dependency points up.
  3. import { LEVELS } from '../lib/header.ts';
  4. import { collect } from '../lib/project.ts';
  5. import { report } from '../lib/report.ts';
  6. export const RULE = { id: 'dsc-r03', title: 'A component does not use a component of a higher level' };
  7. export function check(root = process.cwd()): string[] {
  8. const col = collect(root);
  9. const by = new Map(col.comps.map((c) => [c.id, c]));
  10. const out: string[] = [];
  11. for (const c of col.comps)
  12. for (const u of c.header.uses) {
  13. const used = by.get(u);
  14. if (used && LEVELS.indexOf(used.header.level) > LEVELS.indexOf(c.header.level))
  15. out.push(`${c.source}: ${c.header.level} "${c.id}" uses ${used.header.level} "${u}" — a component uses only its own level or below`);
  16. }
  17. // cycles: a depth-first walk; every cycle is named once
  18. const seen = new Set<string>();
  19. const walk = (id: string, path: string[]) => {
  20. const at = path.indexOf(id);
  21. if (at >= 0) {
  22. const ring = [...path.slice(at), id];
  23. const key = ring.slice(0, -1).sort().join('>');
  24. if (!seen.has(key)) { seen.add(key); out.push(`${by.get(id)!.source}: uses go in a circle: ${ring.join(' → ')}`); }
  25. return;
  26. }
  27. for (const u of by.get(id)?.header.uses ?? []) if (by.has(u)) walk(u, [...path, id]);
  28. };
  29. for (const id of by.keys()) walk(id, []);
  30. return out;
  31. }
  32. if (import.meta.main) report(check());