ShtukbaseStandard
package file→ .shtukbase/packages/code-pragmatic/checks/broken_windows.ts

checks/broken_windows.ts

43 lines, 2.3 KB.

  1. // PRG-R03 (Hunt & Thomas, broken windows). Two kinds of small rot that invite more:
  2. // - a TODO / FIXME / HACK / XXX comment with no task behind it (no `#12`, `ABC-12`, link, or `TODO(name)`);
  3. // - commented-out code: `commentedLines` or more plain line comments in a row (`//`, `#`; doc comments
  4. // `///`, `//!`, `#!` not counted) where most lines look like code.
  5. import { type CheckDef, type Finding, num, runCheck } from '../../code-lib/checks/lib/run.ts';
  6. const MARK = /\b(TODO|FIXME|HACK|XXX)\b(.*)/;
  7. const TRACKED = /^\s*\(|#\d+|\b[A-Z][A-Z0-9]+-\d+\b|https?:\/\/|\/(?:work|tasks|bugs)\/|\btask\b|\bbug\b/i;
  8. const CODEISH = /[;{}]\s*$|^\s*(let|const|var|fn|def|return|if|for|while|import|from|use|pub|print\(|console\.|await|self\.|this\.)\b|^\s*[\w.$[\]]+\s*(=|\+=|-=)\s*[^=]|^\s*[\w.$]+\([^)]*\)\s*;?\s*$/;
  9. export const CHECK: CheckDef = {
  10. pkg: 'code-pragmatic',
  11. rule: 'PRG-R03',
  12. name: 'broken-windows',
  13. defaults: { commentedLines: 3 },
  14. scan(srcs, set) {
  15. const min = num(set.commentedLines, 3);
  16. const out: Finding[] = [];
  17. for (const s of srcs) {
  18. const lineComments: { line: number; text: string }[] = [];
  19. for (const c of s.comments) {
  20. const m = MARK.exec(c.text);
  21. if (m && !TRACKED.test(m[2])) out.push({ rel: s.rel, line: c.line, msg: `${m[1]} without a task: create a task and refer to it, or do it now` });
  22. if (c.block) continue;
  23. const plain = s.lang === 'py' ? /^#(?![!:])/.test(c.text) : /^\/\/(?![/!])/.test(c.text);
  24. // only a comment alone on its line
  25. const before = s.code.slice(s.starts[c.line - 1], c.start);
  26. if (plain && !before.trim()) lineComments.push({ line: c.line, text: c.text.replace(/^(\/\/|#)\s?/, '') });
  27. }
  28. for (let k = 0; k < lineComments.length; ) {
  29. let e = k;
  30. while (e + 1 < lineComments.length && lineComments[e + 1].line === lineComments[e].line + 1) e++;
  31. const run = lineComments.slice(k, e + 1).filter((x) => x.text.trim());
  32. const codeish = run.filter((x) => CODEISH.test(x.text)).length;
  33. if (run.length >= min && codeish / run.length >= 0.6) out.push({ rel: s.rel, line: lineComments[k].line, msg: `commented-out code (${run.length} lines): delete it — it is in the git history` });
  34. k = e + 1;
  35. }
  36. }
  37. return out;
  38. },
  39. };
  40. if (import.meta.main) runCheck(CHECK);