ShtukbaseStandard
package file→ .shtukbase/packages/code-functional-core/checks/effects_in_core.ts

checks/effects_in_core.ts

52 lines, 3.3 KB.

  1. // FC-R01 (Bernhardt, Functional Core, Imperative Shell). Core files do no input/output and read no clock,
  2. // randomness or environment. Looked for (in code, not in strings or comments):
  3. // TypeScript/JavaScript — imports of fs, child_process, net, http(s), os, readline, worker_threads, process;
  4. // `process.env/argv/exit/stdout/stdin/cwd`, `Date.now()`, `new Date()`, `Math.random()`, `fetch(`, `console.`,
  5. // `Bun.`, `Deno.`, `localStorage`, `document.`, `window.`, `setTimeout/setInterval`;
  6. // Python — imports of os, sys, subprocess, socket, shutil, requests, urllib, http, asyncio, random, logging,
  7. // time; `open(`, `print(`, `input(`, `datetime.now(`, `date.today(`;
  8. // Rust — `std::fs`, `std::net`, `std::process`, `std::env`, `std::thread`, `std::io::stdin/stdout/stderr`,
  9. // `tokio`, `reqwest`, `rand::`, `SystemTime::now`, `Instant::now`, `File::`, `println!`, `eprintln!`, `dbg!`.
  10. // Extra patterns: `effects:` in the settings (regular expressions, matched against code lines).
  11. import { type CheckDef, type Finding, runCheck } from '../../code-lib/checks/lib/run.ts';
  12. import { lineOf } from '../../code-lib/checks/lib/lex.ts';
  13. import { imports } from '../../code-lib/checks/lib/units.ts';
  14. import { NOT_NAMED, coreOf } from './lib.ts';
  15. const TS_MODULES = /^(node:)?(fs|fs\/promises|child_process|net|http|https|http2|os|readline|worker_threads|process|dgram|cluster)$/;
  16. const PY_MODULES = /^(os|sys|subprocess|socket|shutil|requests|urllib|http|asyncio|random|logging|time|httpx|aiohttp)(\.|$)/;
  17. const CALLS = {
  18. 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,
  19. py: /(?<![\w.])(open|print|input)\s*\(|\bdatetime\.now\s*\(|\bdate\.today\s*\(/g,
  20. 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,
  21. };
  22. export const CHECK: CheckDef = {
  23. pkg: 'code-functional-core',
  24. rule: 'FC-R01',
  25. name: 'effects-in-core',
  26. defaults: { core: [], effects: [] },
  27. scan(srcs, set) {
  28. const { core, named } = coreOf(srcs, set);
  29. if (!named) {
  30. console.log(NOT_NAMED); // a note, not a finding: the check passes
  31. return [];
  32. }
  33. const extra = (Array.isArray(set.effects) ? set.effects : []).map((x: unknown) => new RegExp(String(x), 'g'));
  34. const out: Finding[] = [];
  35. for (const s of core) {
  36. for (const im of imports(s)) {
  37. if (im.typeOnly) continue;
  38. const bad = s.lang === 'ts' ? TS_MODULES.test(im.spec) : s.lang === 'py' ? PY_MODULES.test(im.spec) && im.level === 0 : false;
  39. 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` });
  40. }
  41. for (const re of [CALLS[s.lang], ...extra]) {
  42. re.lastIndex = 0;
  43. 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` });
  44. }
  45. }
  46. return out;
  47. },
  48. };
  49. if (import.meta.main) runCheck(CHECK);