archivo del paquete→ .shtukbase/packages/code-unix/checks/silent_library.ts
checks/silent_library.ts
33 líneas, 1.5 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- // UNX-R02 (McIlroy / Raymond, Rule of Silence). Library code says nothing on stdout: printing is the business
- // of the program's entry point, which knows whether anyone listens. Counts in files that are not entries
- // (see `isEntry`: bin/, scripts/, cli.*, main.*, a shebang, `import.meta.main`, `__main__`, `fn main`):
- // TypeScript `console.log/info/debug`, `process.stdout.write`; Python `print(` without `file=`; Rust
- // `println!`, `print!`, `dbg!`. stderr (console.error, eprintln!) is for trouble and is not counted.
- import { type CheckDef, type Finding, runCheck } from '../../code-lib/checks/lib/run.ts';
- import { lineOf } from '../../code-lib/checks/lib/lex.ts';
- import { isEntry } from '../../code-lib/checks/lib/units.ts';
- const RE = {
- ts: /\bconsole\.(log|info|debug|table)\s*\(|\bprocess\.stdout\.write\s*\(/g,
- py: /(?<![\w.])print\s*\((?![^)\n]*\bfile\s*=)/g,
- rs: /(?<![\w])(println!|print!|dbg!)\s*\(/g,
- };
- export const CHECK: CheckDef = {
- pkg: 'code-unix',
- rule: 'UNX-R02',
- name: 'silent-library',
- defaults: {},
- scan(srcs) {
- const out: Finding[] = [];
- for (const s of srcs) {
- if (isEntry(s)) continue;
- const hits = [...s.code.matchAll(RE[s.lang])];
- if (!hits.length) continue;
- 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` });
- }
- return out;
- },
- };
- if (import.meta.main) runCheck(CHECK);