ShtukbaseStandard
файл пакета→ .shtukbase/packages/code-functional-core/checks/core_imports.ts

checks/core_imports.ts

43 строки, 2.1 КБ.

Файл как есть в пакете: код и комментарии в нём не переводятся.

  1. // FC-R02 (Bernhardt; Seemann, "impureim sandwich"). Dependencies point inward: a core file imports only core
  2. // files (and outside libraries), never a project file of the shell. TypeScript/JavaScript relative imports and
  3. // Python imports of modules among the files read. Rust is not read here: in Rust the core is a crate, and its
  4. // Cargo.toml says what it may use.
  5. import { posix } from 'node:path';
  6. import { existsSync } from 'node:fs';
  7. import { type CheckDef, type Finding, runCheck } from '../../code-lib/checks/lib/run.ts';
  8. import { imports } from '../../code-lib/checks/lib/units.ts';
  9. import { coreOf } from './lib.ts';
  10. const TS_EXT = ['', '.ts', '.tsx', '.mts', '.js', '.jsx', '.mjs', '/index.ts', '/index.js'];
  11. export const CHECK: CheckDef = {
  12. pkg: 'code-functional-core',
  13. rule: 'FC-R02',
  14. name: 'core-imports',
  15. defaults: { core: [] },
  16. scan(srcs, set, root) {
  17. const { core, isCore, named } = coreOf(srcs, set);
  18. if (!named) return [];
  19. const out: Finding[] = [];
  20. for (const s of core) {
  21. for (const im of imports(s)) {
  22. if (im.typeOnly) continue;
  23. let target: string | null = null;
  24. if (s.lang === 'ts' && im.spec.startsWith('.')) {
  25. const base = posix.normalize(posix.join(posix.dirname(s.rel), im.spec));
  26. for (const st of [base, base.replace(/\.(js|mjs|jsx)$/, '')])
  27. for (const e of TS_EXT) if (!target && existsSync(posix.join(root, st + e)) && /\.[a-z]+$/.test(st + e)) target = st + e;
  28. } else if (s.lang === 'py' && im.level > 0) {
  29. let d = posix.dirname(s.rel);
  30. for (let k = 1; k < im.level; k++) d = posix.dirname(d);
  31. const p = posix.join(d, ...im.spec.split('.').filter(Boolean));
  32. for (const c of [...im.names.map((n) => posix.join(p, n) + '.py'), p + '.py', p + '/__init__.py']) if (!target && existsSync(posix.join(root, c))) target = c;
  33. }
  34. if (target && !isCore(target)) out.push({ rel: s.rel, line: im.line, msg: `the core depends on the shell: ${target}: move what is needed into the core or pass it as a value` });
  35. }
  36. }
  37. return out;
  38. },
  39. };
  40. if (import.meta.main) runCheck(CHECK);