ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/code-refactoring/checks/duplicate_blocks.ts

checks/duplicate_blocks.ts

58 líneas, 2.8 KB.

El archivo tal como está en el paquete: el código y sus comentarios no se traducen.

  1. // RF-R03 (Fowler, Duplicated Code). The same run of `minLines` significant lines in two places, after masking
  2. // comments and collapsing spaces (string contents count). Lines of only brackets, a lone word or argument
  3. // (`budget,`), imports and `use` lines are not significant. Copies are looked for within one language.
  4. // A block is reported at its later copy, with the place of the first one.
  5. import { type CheckDef, type Finding, num, runCheck } from '../../code-lib/checks/lib/run.ts';
  6. import { lineText } from '../../code-lib/checks/lib/scan.ts';
  7. const TRIVIAL = /^[\s{}()[\];,.?]*$|^[\w.&*:$]+[,)]*[;?]*$|^(import|from|use|export\s+\{|export\s+\*|pub\s+use|mod)\b|^(else|try|finally|break;?|continue;?|return;?|pass|end)$/;
  8. export const CHECK: CheckDef = {
  9. pkg: 'code-refactoring',
  10. rule: 'RF-R03',
  11. name: 'duplicate-blocks',
  12. defaults: { minLines: 8 },
  13. scan(srcs, set) {
  14. const w = num(set.minLines, 8);
  15. type Sig = { line: number; text: string };
  16. const files = srcs.map((s) => {
  17. // comments stay masked; string contents come back, so `'a' => 1` and `'b' => 1` differ
  18. const chars = s.code.split('');
  19. for (const st of s.strings) for (let k = st.start; k <= st.end && k < chars.length; k++) chars[k] = s.raw[k];
  20. const text = chars.join('');
  21. const sig: Sig[] = [];
  22. for (let l = 1; l <= s.starts.length; l++) {
  23. const t = lineText(s, l, text).replace(/\s+/g, ' ').trim();
  24. if (!TRIVIAL.test(t)) sig.push({ line: l, text: t });
  25. }
  26. return { s, sig };
  27. });
  28. const first = new Map<string, { fi: number; idx: number }>();
  29. const dup = files.map((f) => new Array<{ fi: number; idx: number } | null>(f.sig.length).fill(null));
  30. files.forEach((f, fi) => {
  31. for (let idx = 0; idx + w <= f.sig.length; idx++) {
  32. const key = f.s.lang + '\n' + f.sig.slice(idx, idx + w).map((x) => x.text).join('\n'); // within one language
  33. if (key.length < w * 12) continue; // too little text to be knowledge
  34. const seen = first.get(key);
  35. if (!seen) first.set(key, { fi, idx });
  36. else if (seen.fi !== fi || idx - seen.idx >= w) dup[fi][idx] = seen;
  37. }
  38. });
  39. const out: Finding[] = [];
  40. files.forEach((f, fi) => {
  41. for (let idx = 0; idx < f.sig.length; idx++) {
  42. const d = dup[fi][idx];
  43. if (!d || (idx > 0 && dup[fi][idx - 1])) continue;
  44. let end = idx;
  45. while (end + 1 < f.sig.length && dup[fi][end + 1]) end++;
  46. const other = files[d.fi];
  47. const lastLine = f.sig[Math.min(end + w - 1, f.sig.length - 1)].line;
  48. out.push({ rel: f.s.rel, line: f.sig[idx].line, msg: `the block of lines ${f.sig[idx].line}–${lastLine} repeats ${other.s.rel}:${other.sig[d.idx].line}: move the common part into a function` });
  49. idx = end;
  50. }
  51. });
  52. return out;
  53. },
  54. };
  55. if (import.meta.main) runCheck(CHECK);