ShtukbaseStandard
файл пакета→ .shtukbase/packages/code-deep-modules/checks/shallow_modules.ts

checks/shallow_modules.ts

36 строк, 1.7 КБ.

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

  1. // DM-R01 (Ousterhout, deep modules). A file that exports many functions and holds little code behind them:
  2. // at least `minExports` exported functions and fewer than `minDepth` lines of code per exported function.
  3. // Exported: TypeScript `export function` / `export const f = (…) =>`, Python top-level `def` without `_`
  4. // (or the names in `__all__`), Rust `pub fn` (`pub(crate)` is not exported).
  5. import { type CheckDef, num, runCheck } from '../../code-lib/checks/lib/run.ts';
  6. import { codeLines } from '../../code-lib/checks/lib/scan.ts';
  7. import { functions } from '../../code-lib/checks/lib/units.ts';
  8. export const CHECK: CheckDef = {
  9. pkg: 'code-deep-modules',
  10. rule: 'DM-R01',
  11. name: 'shallow-modules',
  12. defaults: { minExports: 6, minDepth: 8 },
  13. scan(srcs, set) {
  14. const minExports = num(set.minExports, 6);
  15. const minDepth = num(set.minDepth, 8);
  16. const out = [];
  17. for (const s of srcs) {
  18. let exported = functions(s).filter((f) => f.exported);
  19. if (s.lang === 'py') {
  20. const all = /^__all__\s*=\s*[[(]([^\])]*)/m.exec(s.raw);
  21. if (all) {
  22. const names = new Set((all[1].match(/['"](\w+)['"]/g) ?? []).map((x) => x.slice(1, -1)));
  23. exported = functions(s).filter((f) => f.indent === 0 && names.has(f.name));
  24. }
  25. }
  26. if (exported.length < minExports) continue;
  27. const depth = codeLines(s) / exported.length;
  28. if (depth < minDepth)
  29. out.push({ rel: s.rel, line: 1, msg: `the module is shallow: ${exported.length} public functions, ${depth.toFixed(1)} lines of code per function (threshold ${minDepth}): hide the small functions, give the module fewer and larger entry points` });
  30. }
  31. return out;
  32. },
  33. };
  34. if (import.meta.main) runCheck(CHECK);