包文件→ .shtukbase/packages/code-functional-core/checks/effects_in_core.ts
checks/effects_in_core.ts
52 行, 3.3 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // FC-R01 (Bernhardt, Functional Core, Imperative Shell). Core files do no input/output and read no clock,
- // randomness or environment. Looked for (in code, not in strings or comments):
- // TypeScript/JavaScript — imports of fs, child_process, net, http(s), os, readline, worker_threads, process;
- // `process.env/argv/exit/stdout/stdin/cwd`, `Date.now()`, `new Date()`, `Math.random()`, `fetch(`, `console.`,
- // `Bun.`, `Deno.`, `localStorage`, `document.`, `window.`, `setTimeout/setInterval`;
- // Python — imports of os, sys, subprocess, socket, shutil, requests, urllib, http, asyncio, random, logging,
- // time; `open(`, `print(`, `input(`, `datetime.now(`, `date.today(`;
- // Rust — `std::fs`, `std::net`, `std::process`, `std::env`, `std::thread`, `std::io::stdin/stdout/stderr`,
- // `tokio`, `reqwest`, `rand::`, `SystemTime::now`, `Instant::now`, `File::`, `println!`, `eprintln!`, `dbg!`.
- // Extra patterns: `effects:` in the settings (regular expressions, matched against code lines).
- import { type CheckDef, type Finding, runCheck } from '../../code-lib/checks/lib/run.ts';
- import { lineOf } from '../../code-lib/checks/lib/lex.ts';
- import { imports } from '../../code-lib/checks/lib/units.ts';
- import { NOT_NAMED, coreOf } from './lib.ts';
- const TS_MODULES = /^(node:)?(fs|fs\/promises|child_process|net|http|https|http2|os|readline|worker_threads|process|dgram|cluster)$/;
- const PY_MODULES = /^(os|sys|subprocess|socket|shutil|requests|urllib|http|asyncio|random|logging|time|httpx|aiohttp)(\.|$)/;
- const CALLS = {
- ts: /\bprocess\.(env|argv|exit|stdout|stdin|stderr|cwd)\b|\bDate\.now\s*\(|\bnew\s+Date\s*\(\s*\)|\bMath\.random\s*\(|(?<![\w.$])fetch\s*\(|\bconsole\.\w+\s*\(|\bBun\.\w+|\bDeno\.\w+|\blocalStorage\b|\bdocument\.\w+|\bwindow\.\w+|(?<![\w.$])set(Timeout|Interval)\s*\(/g,
- py: /(?<![\w.])(open|print|input)\s*\(|\bdatetime\.now\s*\(|\bdate\.today\s*\(/g,
- rs: /\bstd::(fs|net|process|env|thread)\b|\bstd::io::(stdin|stdout|stderr)\b|\btokio::|\breqwest::|\brand::|\bSystemTime::now\b|\bInstant::now\b|\bFile::|(?<![\w])(println!|print!|eprintln!|eprint!|dbg!)/g,
- };
- export const CHECK: CheckDef = {
- pkg: 'code-functional-core',
- rule: 'FC-R01',
- name: 'effects-in-core',
- defaults: { core: [], effects: [] },
- scan(srcs, set) {
- const { core, named } = coreOf(srcs, set);
- if (!named) {
- console.log(NOT_NAMED); // a note, not a finding: the check passes
- return [];
- }
- const extra = (Array.isArray(set.effects) ? set.effects : []).map((x: unknown) => new RegExp(String(x), 'g'));
- const out: Finding[] = [];
- for (const s of core) {
- for (const im of imports(s)) {
- if (im.typeOnly) continue;
- const bad = s.lang === 'ts' ? TS_MODULES.test(im.spec) : s.lang === 'py' ? PY_MODULES.test(im.spec) && im.level === 0 : false;
- if (bad) out.push({ rel: s.rel, line: im.line, msg: `the core imports ${im.spec}: input and output belong in the shell, pass ready values to the core` });
- }
- for (const re of [CALLS[s.lang], ...extra]) {
- re.lastIndex = 0;
- for (const m of s.code.matchAll(re)) out.push({ rel: s.rel, line: lineOf(s.starts, m.index ?? 0), msg: `an effect "${m[0].trim()}" in the core: time, chance, environment and input/output belong in the shell, pass a value to the core` });
- }
- }
- return out;
- },
- };
- if (import.meta.main) runCheck(CHECK);