archivo del paquete→ .shtukbase/packages/code-deep-modules/checks/pass_through.ts
checks/pass_through.ts
40 líneas, 2.2 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- // DM-R02 (Ousterhout, pass-through methods). A function whose whole body is one call that hands its own
- // parameters on unchanged, in the same order: `f(a, b) { return g(a, b); }`. Such a layer adds an interface
- // and no function. Constructors of values (`Self(x)`, `Ok(x)`, `Some(x)`, names in CapitalCase) are skipped,
- // so is a function without parameters, a Rust method of `impl Trait for Type` (the trait fixes its shape)
- // and a call on a fixed value (`Code::Invalid.error(why)` adds knowledge: which value).
- import { type CheckDef, runCheck } from '../../code-lib/checks/lib/run.ts';
- import { body, functions, splitTop } from '../../code-lib/checks/lib/units.ts';
- /** The callee and arguments when the body is a single forwarding call; null otherwise. */
- export function forwarded(text: string, lang: string): { callee: string; args: string[] } | null {
- let t = text.trim();
- if (lang === 'py') t = t.replace(/^(?:[rbuRBU]?(?:"""\s*"""|'''\s*'''|"\s*"|'\s*'))\s*/, '').trim(); // a docstring
- const m = /^(?:return\s+)?(?:await\s+)?((?:self\.|this\.|super\.)?[A-Za-z_$][\w$]*(?:(?:\.|::|\?\.)[A-Za-z_$][\w$]*)*)\s*\(([^()]*)\)(?:\.await)?\??\s*;?$/.exec(t);
- if (!m) return null;
- return { callee: m[1], args: splitTop(m[2]).map((a) => a.replace(/^&\s*(mut\s+)?|^\*{1,2}|^\.\.\./, '').trim()) };
- }
- export const CHECK: CheckDef = {
- pkg: 'code-deep-modules',
- rule: 'DM-R02',
- name: 'pass-through',
- defaults: {},
- scan(srcs) {
- const out = [];
- for (const s of srcs)
- for (const f of functions(s)) {
- if (!f.params.length || f.params.some((p) => !p) || f.traitImpl) continue;
- const fw = forwarded(body(s, f), s.lang);
- if (!fw) continue;
- const last = fw.callee.split(/\.|::/).pop() ?? '';
- if (/^[A-Z]/.test(last) || ['new', 'from', 'into', 'clone'].includes(last)) continue;
- if (/\b[A-Z]\w*\./.test(fw.callee)) continue; // `ErrorCode::Invalid.error(why)`: the helper adds a fixed value
- if (fw.args.join(',') !== f.params.join(',')) continue;
- out.push({ rel: s.rel, line: f.line, msg: `the function ${f.name} only passes its parameters to ${fw.callee}: remove the layer or give it a job of its own` });
- }
- return out;
- },
- };
- if (import.meta.main) runCheck(CHECK);