ShtukbaseStandard
файл пакета→ .shtukbase/packages/code-unix/checks/silent_library.ts

checks/silent_library.ts

33 строки, 1.5 КБ.

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

  1. // UNX-R02 (McIlroy / Raymond, Rule of Silence). Library code says nothing on stdout: printing is the business
  2. // of the program's entry point, which knows whether anyone listens. Counts in files that are not entries
  3. // (see `isEntry`: bin/, scripts/, cli.*, main.*, a shebang, `import.meta.main`, `__main__`, `fn main`):
  4. // TypeScript `console.log/info/debug`, `process.stdout.write`; Python `print(` without `file=`; Rust
  5. // `println!`, `print!`, `dbg!`. stderr (console.error, eprintln!) is for trouble and is not counted.
  6. import { type CheckDef, type Finding, runCheck } from '../../code-lib/checks/lib/run.ts';
  7. import { lineOf } from '../../code-lib/checks/lib/lex.ts';
  8. import { isEntry } from '../../code-lib/checks/lib/units.ts';
  9. const RE = {
  10. ts: /\bconsole\.(log|info|debug|table)\s*\(|\bprocess\.stdout\.write\s*\(/g,
  11. py: /(?<![\w.])print\s*\((?![^)\n]*\bfile\s*=)/g,
  12. rs: /(?<![\w])(println!|print!|dbg!)\s*\(/g,
  13. };
  14. export const CHECK: CheckDef = {
  15. pkg: 'code-unix',
  16. rule: 'UNX-R02',
  17. name: 'silent-library',
  18. defaults: {},
  19. scan(srcs) {
  20. const out: Finding[] = [];
  21. for (const s of srcs) {
  22. if (isEntry(s)) continue;
  23. const hits = [...s.code.matchAll(RE[s.lang])];
  24. if (!hits.length) continue;
  25. out.push({ rel: s.rel, line: lineOf(s.starts, hits[0].index ?? 0), msg: `library code prints to stdout (${hits.length} places): return the data to the caller, print at the entry point; messages about trouble go to stderr` });
  26. }
  27. return out;
  28. },
  29. };
  30. if (import.meta.main) runCheck(CHECK);