package file→ .shtukbase/packages/code-functional-core/checks/core_imports.ts
checks/core_imports.ts
43 lines, 2.1 KB.
- // FC-R02 (Bernhardt; Seemann, "impureim sandwich"). Dependencies point inward: a core file imports only core
- // files (and outside libraries), never a project file of the shell. TypeScript/JavaScript relative imports and
- // Python imports of modules among the files read. Rust is not read here: in Rust the core is a crate, and its
- // Cargo.toml says what it may use.
- import { posix } from 'node:path';
- import { existsSync } from 'node:fs';
- import { type CheckDef, type Finding, runCheck } from '../../code-lib/checks/lib/run.ts';
- import { imports } from '../../code-lib/checks/lib/units.ts';
- import { coreOf } from './lib.ts';
- const TS_EXT = ['', '.ts', '.tsx', '.mts', '.js', '.jsx', '.mjs', '/index.ts', '/index.js'];
- export const CHECK: CheckDef = {
- pkg: 'code-functional-core',
- rule: 'FC-R02',
- name: 'core-imports',
- defaults: { core: [] },
- scan(srcs, set, root) {
- const { core, isCore, named } = coreOf(srcs, set);
- if (!named) return [];
- const out: Finding[] = [];
- for (const s of core) {
- for (const im of imports(s)) {
- if (im.typeOnly) continue;
- let target: string | null = null;
- if (s.lang === 'ts' && im.spec.startsWith('.')) {
- const base = posix.normalize(posix.join(posix.dirname(s.rel), im.spec));
- for (const st of [base, base.replace(/\.(js|mjs|jsx)$/, '')])
- for (const e of TS_EXT) if (!target && existsSync(posix.join(root, st + e)) && /\.[a-z]+$/.test(st + e)) target = st + e;
- } else if (s.lang === 'py' && im.level > 0) {
- let d = posix.dirname(s.rel);
- for (let k = 1; k < im.level; k++) d = posix.dirname(d);
- const p = posix.join(d, ...im.spec.split('.').filter(Boolean));
- 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;
- }
- 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` });
- }
- }
- return out;
- },
- };
- if (import.meta.main) runCheck(CHECK);