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

checks/lib/run.ts

61 строка, 2.8 КБ.

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

  1. // Runs one code heuristic as a package check. The working directory is the project root.
  2. // The norm's page decides how loud the check is: `mode: enforce` — findings fail the check (exit 1);
  3. // `observe` or `warn` — findings are printed and the check passes (exit 0). A norm that is not active
  4. // (or whose page is gone) is not checked. Arguments are paths: then only they are read, not the roots.
  5. // A finding is silenced in place by a comment `code-allow: <check>` (or the rule id) on its line or the line above.
  6. import { type Settings, type Source, readHead, settings, sources } from './scan.ts';
  7. export type Finding = { rel: string; line: number; msg: string };
  8. export type CheckDef = {
  9. pkg: string;
  10. rule: string; // DM-R01
  11. name: string; // the action name, also the word for `code-allow:`
  12. defaults: Settings;
  13. scan: (srcs: Source[], set: Settings, root: string) => Finding[];
  14. };
  15. export const rulePage = (pkg: string, rule: string) => `norms/rules/${pkg}/${rule}.md`;
  16. /** Findings of a check without printing: used by `runCheck` and by tests. */
  17. export function findings(def: CheckDef, root: string, args: string[]): Finding[] {
  18. const set = settings(root, def.pkg, def.defaults);
  19. const srcs = sources(root, set, args);
  20. const byRel = new Map(srcs.map((s) => [s.rel, s]));
  21. const allow = new RegExp(`code-allow:[^\\n]*\\b(${def.name}|${def.rule})\\b`);
  22. return def
  23. .scan(srcs, set, root)
  24. .filter((f) => {
  25. const s = byRel.get(f.rel);
  26. if (!s) return true;
  27. const lines = s.raw.split('\n');
  28. return !allow.test(lines[f.line - 1] ?? '') && !allow.test(lines[f.line - 2] ?? '');
  29. })
  30. .sort((a, b) => (a.rel === b.rel ? a.line - b.line : a.rel < b.rel ? -1 : 1));
  31. }
  32. export function runCheck(def: CheckDef): void {
  33. const root = process.cwd();
  34. const args = process.argv.slice(2).filter((a) => !a.startsWith('--'));
  35. const head = readHead(root, rulePage(def.pkg, def.rule));
  36. const status = String(head.status ?? '').trim();
  37. if (!Object.keys(head).length || (status && status !== 'active')) {
  38. process.exitCode = 0;
  39. return;
  40. }
  41. const mode = String(head.mode ?? 'observe').trim();
  42. const found = findings(def, root, args);
  43. for (const f of found) console.log(`${f.rel}:${f.line}: ${f.msg}`);
  44. if (found.length && mode !== 'enforce')
  45. console.log(`${def.rule} (mode: ${mode}): ${found.length} findings — the check only warns; to block, set mode: enforce in /${rulePage(def.pkg, def.rule)}`);
  46. process.exitCode = found.length && mode === 'enforce' ? 1 : 0;
  47. }
  48. /** Lines of a function that are code, trimmed, for small body tests. */
  49. export function compact(text: string): string {
  50. return text.replace(/\s+/g, ' ').trim();
  51. }
  52. export function num(v: unknown, d: number): number {
  53. const n = Number(v);
  54. return Number.isFinite(n) && n > 0 ? n : d;
  55. }