mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-25 17:41:35 +01:00
Changed-constant check stops flagging unrelated tests for one- and two-digit numbers (#4683)
Changing a small constant such as `MAX_ITEMS = 10` no longer flags every test that mentions its module and holds a `10` somewhere; only lines that use something taken from the module, or name the constant, count. Why: one-line raises of two-digit constants were blocked by dozens of false matches (#4312); on gstack itself, changing `RECEIPT_RUNS_KEPT` (30) flagged 20 test files and now flags 4. Nothing to do. Three-digit and longer values keep the old rule, and a test that imports the module and asserts the old value is still caught. Co-authored-by: Mahmoud ElNeMR <83190847+ElNeMeR70@users.noreply.github.com> Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> Review-Finding: p1/red-team#1 reader=red-team Review-Finding: p1/specialist-batched#1 reader=testing Review-Finding: p1/red-team#2 reader=red-team
This commit is contained in:
4 files changed
+119
-25
No files matched your search
@@ -97,7 +97,13 @@ function unquote(v: string): string { return /^['"`]/.test(v) ? v.slice(1, -1) :
|
||||
* in a statement that names it: on the name's line, or on a line that
|
||||
* continues it (the line before ends open, `(` `,` `.` `=`, or the line
|
||||
* starts with `.` `)` `]`), comments excluded (#3492: `$2`, "from revision 2",
|
||||
* `export default 2`). A call-site name (`attempts`, `limit`) is an ordinary
|
||||
* `export default 2`). A one- or two-digit integer (`12`, `-10`) is nearly as
|
||||
* common, so through the module it counts only in a statement that names the
|
||||
* constant or a binding the file takes from the module: an import of it
|
||||
* (`import { shards } from '…/arms'`, type-only imports excluded) or a variable
|
||||
* a line naming it declares (`const out = run('…/arms.ts')`). A comment that
|
||||
* names the module beside an unrelated `10` is not a consumer (#4312), while
|
||||
* `expect(shards()).toBe(12)` still is (GST-820). A call-site name (`attempts`, `limit`) is an ordinary
|
||||
* word, so for it the file must also name the module whatever the value's
|
||||
* length (gstack-evals#97, #4478).
|
||||
*
|
||||
@@ -128,6 +134,7 @@ export function consumersOf(repoRoot: string, testFiles: readonly string[], chan
|
||||
const moduleRe = new RegExp(`(?:^|[/\\\\])${escape(stem)}(?=[^\\w.-]|$|\\.${dts}(?:${exts})(?![\\w.-])|\\.(?!\\w))|(?<![\\w-])${escape(base)}(?![\\w-]|\\.\\w)`, 'm');
|
||||
const nameRe = new RegExp(`\\b${escape(change.name)}\\b`);
|
||||
const nameRequired = value.length < 2;
|
||||
const shortNumber = !nameRequired && /^-?\d{1,2}$/.test(value);
|
||||
const callSite = !/^[A-Z]/.test(change.name);
|
||||
const comment = (l: string) => /^\s*(?:\/\/|#|\*|\/\*|<!--)/.test(l);
|
||||
const continues = (prev: string, cur: string) => /[([{,.=+\-*/&|?:\\]\s*$/.test(prev) || /^\s*[.)\]}?:&|+]/.test(cur);
|
||||
@@ -137,11 +144,14 @@ export function consumersOf(repoRoot: string, testFiles: readonly string[], chan
|
||||
try { text = fs.readFileSync(path.join(repoRoot, rel), 'utf8'); } catch { continue; }
|
||||
if (!((nameRequired ? nameRe.test(text) : nameRe.test(text) || moduleRe.test(text)) && (!callSite || moduleRe.test(text)))) continue;
|
||||
const lines = text.split('\n');
|
||||
const bindings = shortNumber ? moduleBindings(lines, moduleRe, comment, continues) : [];
|
||||
if (shortNumber && !bindings.length && !nameRe.test(text)) continue;
|
||||
const statementRe = bindings.length ? new RegExp(`(?<![\\w$])(?:${[change.name, ...bindings].map(escape).join('|')})(?![\\w$])`) : nameRe;
|
||||
const named = (i: number) => {
|
||||
if (comment(lines[i]!)) return false;
|
||||
for (let j = i; j >= 0 && i - j < 4; j--) {
|
||||
if (j < i && (comment(lines[j]!) || !continues(lines[j]!, lines[j + 1]!))) return false;
|
||||
if (nameRe.test(lines[j]!)) return true;
|
||||
if (statementRe.test(lines[j]!)) return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
@@ -168,11 +178,42 @@ export function consumersOf(repoRoot: string, testFiles: readonly string[], chan
|
||||
return true;
|
||||
};
|
||||
const carries = (i: number) => { const m = markerIn(lines[i]!); return valueRe.test(m ? lines[i]!.slice(0, lines[i]!.indexOf('pregate-literal:')) : lines[i]!); };
|
||||
for (let i = 0; i < lines.length; i++) if (carries(i) && (!nameRequired || oldRowRe.test(lines[i]!) || named(i)) && !skip(i) && !waiver(i)) { hits.push(`${rel}:${i + 1}`); break; }
|
||||
for (let i = 0; i < lines.length; i++) if (carries(i) && (!(nameRequired || shortNumber) || oldRowRe.test(lines[i]!) || named(i)) && !skip(i) && !waiver(i)) { hits.push(`${rel}:${i + 1}`); break; }
|
||||
}
|
||||
return hits;
|
||||
}
|
||||
|
||||
/** The names a file takes from the module: the bindings of a JS/TS import whose specifier is the module (type-only ones excluded), and the variables a non-comment statement (a line plus the lines that continue it) declares or assigns from the module or from such a name (`const items = listItems()`, `out = run('bin/x.sh')` in a hook). */
|
||||
function moduleBindings(lines: readonly string[], moduleRe: RegExp, comment: (l: string) => boolean, continues: (prev: string, cur: string) => boolean): string[] {
|
||||
const code = lines.filter(l => !comment(l)).join('\n');
|
||||
const out = new Set<string>();
|
||||
const names = (list: string) => list.split(',').map(s => s.trim()).filter(s => s && !/^type\s/.test(s)).map(s => s.split(/\s+as\s+|:/).pop()!.replace(/^\.\.\./, '').trim());
|
||||
for (const m of code.matchAll(/\bimport\s+(?!type\b)([^'"`;]*?)\s*\bfrom\s*['"]([^'"]+)['"]/g)) {
|
||||
if (!moduleRe.test(m[2]!)) continue;
|
||||
const braces = /\{([^}]*)\}/.exec(m[1]!);
|
||||
if (braces) for (const n of names(braces[1]!)) out.add(n);
|
||||
for (const n of m[1]!.replace(/\{[^}]*\}/, '').split(',').map(s => s.trim().replace(/^\*\s+as\s+/, ''))) out.add(n);
|
||||
}
|
||||
const codeLines = code.split('\n');
|
||||
const statements = codeLines.map((line, i) => {
|
||||
let s = line;
|
||||
for (let j = i + 1; j < codeLines.length && j - i < 4 && continues(codeLines[j - 1]!, codeLines[j]!); j++) s += '\n' + codeLines[j];
|
||||
return s;
|
||||
});
|
||||
const declRe = /(?:\b(?:const|let|var)\s+(\{[^}]*\}|[\w$]+)|(?<![\w$.])([\w$]+))\s*=(?![=>])/;
|
||||
for (let grew = true, round = 0; grew && round < 4; round++) {
|
||||
grew = false;
|
||||
const known = [...out].filter(n => /^[\w$]+$/.test(n));
|
||||
const knownRe = known.length ? new RegExp(`(?<![\\w$])(?:${known.map(escape).join('|')})(?![\\w$])`) : null;
|
||||
for (const s of statements) {
|
||||
const m = declRe.exec(s);
|
||||
if (!m || !(moduleRe.test(s) || knownRe?.test(s.slice(m.index + m[0].length)))) continue;
|
||||
for (const n of m[1]?.startsWith('{') ? names(m[1].slice(1, -1)) : [m[1] ?? m[2]!]) if (!out.has(n)) { out.add(n); grew = true; }
|
||||
}
|
||||
}
|
||||
return [...out].filter(n => /^[\w$]+$/.test(n));
|
||||
}
|
||||
|
||||
function run(ctx: PregateContext): CheckResult {
|
||||
const candidates = ctx.touched.filter(t => (t.status === 'M' || t.status === 'R') && !isTestFile(t.path) && /\.(?:[cm]?[jt]sx?|py|rb|sh|bash)$/.test(t.path));
|
||||
const changes: ChangedLiteral[] = [];
|
||||
|
||||
Reference in new issue
Block a user