package file→ .shtukbase/packages/code-pragmatic/checks/broken_windows.ts
checks/broken_windows.ts
43 lines, 2.3 KB.
- // PRG-R03 (Hunt & Thomas, broken windows). Two kinds of small rot that invite more:
- // - a TODO / FIXME / HACK / XXX comment with no task behind it (no `#12`, `ABC-12`, link, or `TODO(name)`);
- // - commented-out code: `commentedLines` or more plain line comments in a row (`//`, `#`; doc comments
- // `///`, `//!`, `#!` not counted) where most lines look like code.
- import { type CheckDef, type Finding, num, runCheck } from '../../code-lib/checks/lib/run.ts';
- const MARK = /\b(TODO|FIXME|HACK|XXX)\b(.*)/;
- const TRACKED = /^\s*\(|#\d+|\b[A-Z][A-Z0-9]+-\d+\b|https?:\/\/|\/(?:work|tasks|bugs)\/|\btask\b|\bbug\b/i;
- 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*$/;
- export const CHECK: CheckDef = {
- pkg: 'code-pragmatic',
- rule: 'PRG-R03',
- name: 'broken-windows',
- defaults: { commentedLines: 3 },
- scan(srcs, set) {
- const min = num(set.commentedLines, 3);
- const out: Finding[] = [];
- for (const s of srcs) {
- const lineComments: { line: number; text: string }[] = [];
- for (const c of s.comments) {
- const m = MARK.exec(c.text);
- 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` });
- if (c.block) continue;
- const plain = s.lang === 'py' ? /^#(?![!:])/.test(c.text) : /^\/\/(?![/!])/.test(c.text);
- // only a comment alone on its line
- const before = s.code.slice(s.starts[c.line - 1], c.start);
- if (plain && !before.trim()) lineComments.push({ line: c.line, text: c.text.replace(/^(\/\/|#)\s?/, '') });
- }
- for (let k = 0; k < lineComments.length; ) {
- let e = k;
- while (e + 1 < lineComments.length && lineComments[e + 1].line === lineComments[e].line + 1) e++;
- const run = lineComments.slice(k, e + 1).filter((x) => x.text.trim());
- const codeish = run.filter((x) => CODEISH.test(x.text)).length;
- 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` });
- k = e + 1;
- }
- }
- return out;
- },
- };
- if (import.meta.main) runCheck(CHECK);