refactor: simplify pass (trekexecute)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kjell Tore Guttormsen 2026-10-31 17:25:10 +01:00
commit 9b80e6c14c
Signed by: ktg
SSH key fingerprint: SHA256:JakMjO6FTBBzN0Bhfj9saOoEjaFxlSdYuZQQpM/lF9Q
3 changed files with 8 additions and 11 deletions

View file

@ -373,7 +373,7 @@ function argsCutInString(text, o) {
// Each heredoc operator in `text`, in text order, as the shell reads its word:
// { index (the operator's first <), end, word, quoted, unreadable, cutOnly }.
function argsShellOpeners(text) {
function argsShellOpeners(text, lim = argsEscapeLimit(text)) {
const n = text.length;
// The index after any run of continuations that starts at k.
const skip = (k) => {
@ -381,7 +381,6 @@ function argsShellOpeners(text) {
return k;
};
const openers = [];
const lim = argsEscapeLimit(text);
let i = text.indexOf('<');
while (i !== -1) {
if (argsEscapedLt(text, i, lim)) { i = text.indexOf('<', i + 1); continue; }
@ -477,7 +476,7 @@ if (command.replace(/\\\n/g, '').includes(ARGS_DELIM)) {
if (!quoted || !closesOnceAtEnd) blockArgs(!quoted, false);
}
let bodyFrom = Infinity;
for (const o of argsShellOpeners(command)) {
for (const o of argsShellOpeners(command, argsLim)) {
if (o.index >= bodyFrom && o.cutOnly) continue;
if (o.cutOnly && argsCutInString(command, o)) continue;
// The word is compared trimmed, as the body lines are: a word of D plus

View file

@ -14,11 +14,11 @@
import { test, after } from 'node:test';
import { strict as assert } from 'node:assert';
import { mkdtempSync, writeFileSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { writeFileSync, rmSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { runHook, runHookWithEnv } from '../helpers/hook-helper.mjs';
import { tmp } from './guard-helpers.mjs';
const HERE = dirname(fileURLToPath(import.meta.url));
const ROOT = join(HERE, '..', '..');
@ -62,9 +62,7 @@ test('[CSG] the guard checks an opener whose delimiter is split by a backslash-n
await expectUnquoted(`${PARSER_LINE} <<VOYAGE_ARGS_EOF\\\n_7F3C9E\nbrief.md $(touch PWNED)`);
// Control: an ordinary unquoted heredoc that names no delimiter passes.
const { code, stderr } = await runHook(PRE_BASH, bashInput('cat <<EOF\n$(date)\nEOF'));
assert.doesNotMatch(stderr, ARGS_RULE, `the guard refused an ordinary heredoc: ${stderr}`);
assert.strictEqual(code, 0, `the guard refused an ordinary heredoc: ${stderr}`);
await expectArgsAllowed('cat <<EOF\n$(date)\nEOF');
});
// The preload makes String.prototype.indexOf throw when it is called with '<'
@ -80,7 +78,7 @@ String.prototype.indexOf = function (search, ...rest) {
};
`;
const preloadDir = mkdtempSync(join(tmpdir(), 'voyage-ctb-'));
const preloadDir = tmp('voyage-ctb-');
after(() => rmSync(preloadDir, { recursive: true, force: true }));
test('[CTB] an error inside the shell reading blocks the command (exit 2), never lets it through', async () => {

View file

@ -161,14 +161,14 @@ const MUTATIONS = [
find: "const q1 = c === '$' ? skip(k + 1) : k + 1;",
replace: 'const q1 = k + 1;',
pins: [1, 2],
flipped: async (p, hook) => expectPasses(p, hook),
flipped: expectPasses,
},
{
name: 'pins 3 and 4 go red under the untrimmed word comparison',
find: 'o.word.trim() !== ARGS_DELIM',
replace: 'o.word !== ARGS_DELIM',
pins: [3, 4],
flipped: async (p, hook) => expectPasses(p, hook),
flipped: expectPasses,
},
{
name: 'pin 5 goes red under the untrimmed closing-line count',