From f57a8d8a749afb7e3a65c6949968a4306f15573b Mon Sep 17 00:00:00 2001 From: darkzOGx Date: Tue, 27 Oct 2026 20:43:01 +0300 Subject: [PATCH 1/4] fix(hooks): let [1m] marker beat CLAUDE_CODE_AUTO_COMPACT_WINDOW CLAUDE_CODE_AUTO_COMPACT_WINDOW is the harness auto-compact trigger point (e.g. 786432 on a 1M-window model), not the context window size. Honoring it before the [1m] marker / known-family / >200k inference checks made 1M models resolve to a 786k window with the 160k standard threshold, so the strategic-compact hook fired prematurely in real Claude Code sessions where the harness sets this variable. Keep ECC_CONTEXT_WINDOW_TOKENS as the strongest override; demote CLAUDE_CODE_AUTO_COMPACT_WINDOW to a last-resort fallback. --- scripts/lib/transcript-context.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/scripts/lib/transcript-context.js b/scripts/lib/transcript-context.js index 87c6bb2d4..2fe6b6a97 100644 --- a/scripts/lib/transcript-context.js +++ b/scripts/lib/transcript-context.js @@ -138,13 +138,13 @@ function readLatestContextTokens(transcriptPath, options = {}) { * @returns {{ windowTokens: number, inferred: boolean }} */ function resolveContextWindow(tokens, model) { + const env = (typeof process !== 'undefined' && process.env) || {}; + // Explicit window override wins: 400k models (e.g. Opus 4.x) match neither the // 200k default nor the 1M marker and would otherwise report ~double usage (#2290). - // Honor ECC's own knob and Claude Code's native CLAUDE_CODE_AUTO_COMPACT_WINDOW. - const env = (typeof process !== 'undefined' && process.env) || {}; - const envWindow = Number.parseInt(env.ECC_CONTEXT_WINDOW_TOKENS || env.CLAUDE_CODE_AUTO_COMPACT_WINDOW || '', 10); - if (Number.isInteger(envWindow) && envWindow > 0) { - return { windowTokens: envWindow, inferred: false }; + const explicitWindow = Number.parseInt(env.ECC_CONTEXT_WINDOW_TOKENS || '', 10); + if (Number.isInteger(explicitWindow) && explicitWindow > 0) { + return { windowTokens: explicitWindow, inferred: false }; } if (typeof model === 'string' && model.includes(LARGE_WINDOW_MODEL_MARKER)) { @@ -164,6 +164,15 @@ function resolveContextWindow(tokens, model) { return { windowTokens: LARGE_CONTEXT_WINDOW_TOKENS, inferred: true }; } + // Claude Code's CLAUDE_CODE_AUTO_COMPACT_WINDOW is the harness auto-compact + // trigger point (e.g. 786432 on a 1M-window model), not the context window + // size. Honor it only as a last-resort fallback so it cannot mask the [1m] + // marker, known large-window families, or >200k observed tokens above. + const autoCompactWindow = Number.parseInt(env.CLAUDE_CODE_AUTO_COMPACT_WINDOW || '', 10); + if (Number.isInteger(autoCompactWindow) && autoCompactWindow > 0) { + return { windowTokens: autoCompactWindow, inferred: false }; + } + return { windowTokens: STANDARD_CONTEXT_WINDOW_TOKENS, inferred: true }; } From eec6623a61cb91b1f4da995473027ebd89fef0ea Mon Sep 17 00:00:00 2001 From: leoca Date: Thu, 29 Oct 2026 15:05:58 +0000 Subject: [PATCH 2/4] fix(hooks): close a quote that follows a backslash in shell-substitution toggleQuote refused to toggle when the previous character was a backslash, but every caller already consumes a backslash and the character after it outside single quotes. A quote reached after a backslash is therefore the end of `'C:\'` or the end of `"C:\\"`, and both stayed open, so a `(...)` group or `{ ...; }` group after them was never extracted. `echo "C:\\" ; (npm run dev)` passed the dev-server block while `echo "C:" ; (npm run dev)` was blocked. --- scripts/lib/shell-substitution.js | 17 +++++++++-------- tests/lib/shell-substitution.test.js | 9 +++++++++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/scripts/lib/shell-substitution.js b/scripts/lib/shell-substitution.js index a5dec6fbb..8c6833046 100644 --- a/scripts/lib/shell-substitution.js +++ b/scripts/lib/shell-substitution.js @@ -107,14 +107,15 @@ function parenDelta(ch) { /** * Toggle quote state for a quote character at the current position. - * Returns true when `ch` opened or closed a quote. + * Returns true when `ch` opened or closed a quote. Callers consume escapes + * first, so a quote after a backslash here still closes (`'C:\'`, `"C:\\"`). */ -function toggleQuote(ch, prev, quotes) { - if (ch === "'" && !quotes.inDouble && prev !== '\\') { +function toggleQuote(ch, quotes) { + if (ch === "'" && !quotes.inDouble) { quotes.inSingle = !quotes.inSingle; return true; } - if (ch === '"' && !quotes.inSingle && prev !== '\\') { + if (ch === '"' && !quotes.inSingle) { quotes.inDouble = !quotes.inDouble; return true; } @@ -135,7 +136,7 @@ function findParenSpanEnd(source, start) { i += 2; continue; } - if (!toggleQuote(inner, source[i - 1], quotes) && !quotes.inSingle && !quotes.inDouble) { + if (!toggleQuote(inner, quotes) && !quotes.inSingle && !quotes.inDouble) { depth += parenDelta(inner); } i += 1; @@ -167,7 +168,7 @@ function findBacktickEnd(source, start) { function skipOuterSpan(source, i, quotes) { const ch = source[i]; if (ch === '\\' && !quotes.inSingle) return i + 1; - if (toggleQuote(ch, source[i - 1], quotes)) return i; + if (toggleQuote(ch, quotes)) return i; if (quotes.inSingle || quotes.inDouble) return i; if (ch === '$' && source[i + 1] === '(') return findParenSpanEnd(source, i + 2) - 1; if (ch === '`') return findBacktickEnd(source, i + 1); @@ -192,7 +193,7 @@ function readSubshellBody(source, start) { i = Math.min(i + 2, source.length); continue; } - if (!toggleQuote(inner, source[i - 1], quotes) && !quotes.inSingle && !quotes.inDouble) { + if (!toggleQuote(inner, quotes) && !quotes.inSingle && !quotes.inDouble) { depth += parenDelta(inner); if (depth === 0) break; } @@ -279,7 +280,7 @@ function readBraceBody(source, start) { i = Math.min(i + 2, source.length); continue; } - if (toggleQuote(inner, prev, quotes) || quotes.inSingle || quotes.inDouble) { + if (toggleQuote(inner, quotes) || quotes.inSingle || quotes.inDouble) { body += inner; i += 1; continue; diff --git a/tests/lib/shell-substitution.test.js b/tests/lib/shell-substitution.test.js index f64c90419..432e7c434 100644 --- a/tests/lib/shell-substitution.test.js +++ b/tests/lib/shell-substitution.test.js @@ -154,6 +154,12 @@ test('double-quoted parens are literal (bash only honors $() there)', () => { test('extracts a bare (...) group while skipping an adjacent $()', () => { assert.deepStrictEqual(extractSubshellGroups('$(a) (b)'), ['b']); }); +test('a double-quoted string ending in an escaped backslash still closes', () => { + assert.deepStrictEqual(extractSubshellGroups('echo "C:\\\\"; (npm run dev)'), ['npm run dev']); +}); +test('a single-quoted string ending in a backslash still closes', () => { + assert.deepStrictEqual(extractSubshellGroups("echo 'C:\\'; (npm run dev)"), ['npm run dev']); +}); console.log('\nextractSubshellGroups - security-relevant:'); test('surfaces a destructive command inside a subshell', () => { @@ -173,6 +179,9 @@ console.log('\nextractBraceGroups - basics:'); test('extracts a { ...; } body', () => { assert.deepStrictEqual(extractBraceGroups('{ npm run dev; }'), [' npm run dev; ']); }); +test('extracts a { ...; } body after a string ending in an escaped backslash', () => { + assert.deepStrictEqual(extractBraceGroups('echo "C:\\\\"; { npm run dev; }'), [' npm run dev; ']); +}); test('nested brace group returns outer body then inner body', () => { assert.deepStrictEqual(extractBraceGroups('{ a; { b; }; }'), [' a; { b; }; ', ' b; ']); }); From eb9a5ca0c6b492ed9ce83423a2fb4700738966b5 Mon Sep 17 00:00:00 2001 From: leoca Date: Thu, 29 Oct 2026 18:44:54 +0000 Subject: [PATCH 3/4] fix(hooks): keep $'...' open across an escaped apostrophe Dropping the previous-character check in toggleQuote let the `'` of `\'` close an ANSI-C `$'...'` string, so `echo $'it\'s'; (rm -rf /tmp/x)` left a quote open and the group was never extracted. Track `$'...'` in the quote state and consume backslash escapes inside it, as bash does, and cover the inner scanners (`(...)`, `$(...)`, `{ ...; }` bodies) with quotes ending in a backslash. --- scripts/lib/shell-substitution.js | 51 ++++++++++++------- tests/hooks/gateguard-fact-force.test.js | 8 +++ tests/hooks/pre-bash-dev-server-block.test.js | 5 ++ tests/lib/shell-substitution.test.js | 24 +++++++++ 4 files changed, 71 insertions(+), 17 deletions(-) diff --git a/scripts/lib/shell-substitution.js b/scripts/lib/shell-substitution.js index 8c6833046..5b747f0d0 100644 --- a/scripts/lib/shell-substitution.js +++ b/scripts/lib/shell-substitution.js @@ -105,14 +105,31 @@ function parenDelta(ch) { return 0; } +// `$'...'` (ANSI-C quoting) opens only on an unescaped `$`: an odd run of +// backslashes before it makes the `$` literal and the quote a plain one. +function opensAnsiC(source, i) { + if (source[i - 1] !== '$') return false; + let backslashes = 0; + while (source[i - 2 - backslashes] === '\\') backslashes += 1; + return backslashes % 2 === 0; +} + +// Backslash escapes apply outside single quotes and inside `$'...'`. +function escapesActive(quotes) { + return !quotes.inSingle || quotes.ansiC; +} + /** - * Toggle quote state for a quote character at the current position. - * Returns true when `ch` opened or closed a quote. Callers consume escapes - * first, so a quote after a backslash here still closes (`'C:\'`, `"C:\\"`). + * Toggle quote state for the quote character at `source[i]`. + * Returns true when it opened or closed a quote. Callers consume escapes + * first, so a quote after a backslash here still closes (`'C:\'`, `"C:\\"`), + * while `\'` inside `$'...'` is consumed before it can close the string. */ -function toggleQuote(ch, quotes) { +function toggleQuote(source, i, quotes) { + const ch = source[i]; if (ch === "'" && !quotes.inDouble) { quotes.inSingle = !quotes.inSingle; + quotes.ansiC = quotes.inSingle && opensAnsiC(source, i); return true; } if (ch === '"' && !quotes.inSingle) { @@ -127,16 +144,16 @@ function toggleQuote(ch, quotes) { * is at `start`, honoring quotes and escapes inside the span. */ function findParenSpanEnd(source, start) { - const quotes = { inSingle: false, inDouble: false }; + const quotes = { inSingle: false, inDouble: false, ansiC: false }; let depth = 1; let i = start; while (i < source.length && depth > 0) { const inner = source[i]; - if (inner === '\\' && !quotes.inSingle) { + if (inner === '\\' && escapesActive(quotes)) { i += 2; continue; } - if (!toggleQuote(inner, quotes) && !quotes.inSingle && !quotes.inDouble) { + if (!toggleQuote(source, i, quotes) && !quotes.inSingle && !quotes.inDouble) { depth += parenDelta(inner); } i += 1; @@ -167,8 +184,8 @@ function findBacktickEnd(source, start) { */ function skipOuterSpan(source, i, quotes) { const ch = source[i]; - if (ch === '\\' && !quotes.inSingle) return i + 1; - if (toggleQuote(ch, quotes)) return i; + if (ch === '\\' && escapesActive(quotes)) return i + 1; + if (toggleQuote(source, i, quotes)) return i; if (quotes.inSingle || quotes.inDouble) return i; if (ch === '$' && source[i + 1] === '(') return findParenSpanEnd(source, i + 2) - 1; if (ch === '`') return findBacktickEnd(source, i + 1); @@ -180,20 +197,20 @@ function skipOuterSpan(source, i, quotes) { * the body text and the index of the closing paren (or where input ran out). */ function readSubshellBody(source, start) { - const quotes = { inSingle: false, inDouble: false }; + const quotes = { inSingle: false, inDouble: false, ansiC: false }; let depth = 1; let body = ''; let i = start; while (i < source.length && depth > 0) { const inner = source[i]; - if (inner === '\\' && !quotes.inSingle) { + if (inner === '\\' && escapesActive(quotes)) { // Keep the escape and its operand together; a trailing backslash at the // end of an unterminated span is appended once and the scan stops. body += source.slice(i, i + 2); i = Math.min(i + 2, source.length); continue; } - if (!toggleQuote(inner, quotes) && !quotes.inSingle && !quotes.inDouble) { + if (!toggleQuote(source, i, quotes) && !quotes.inSingle && !quotes.inDouble) { depth += parenDelta(inner); if (depth === 0) break; } @@ -223,7 +240,7 @@ function readSubshellBody(source, start) { function extractSubshellGroups(input) { const source = String(input || ''); const groups = []; - const quotes = { inSingle: false, inDouble: false }; + const quotes = { inSingle: false, inDouble: false, ansiC: false }; for (let i = 0; i < source.length; i++) { const skipped = skipOuterSpan(source, i, quotes); @@ -268,19 +285,19 @@ function opensBraceGroup(source, i) { * the body text and the index of the closing brace (or where input ran out). */ function readBraceBody(source, start) { - const quotes = { inSingle: false, inDouble: false }; + const quotes = { inSingle: false, inDouble: false, ansiC: false }; let depth = 1; let body = ''; let i = start; while (i < source.length && depth > 0) { const inner = source[i]; const prev = source[i - 1]; - if (inner === '\\' && !quotes.inSingle) { + if (inner === '\\' && escapesActive(quotes)) { body += source.slice(i, i + 2); i = Math.min(i + 2, source.length); continue; } - if (toggleQuote(inner, quotes) || quotes.inSingle || quotes.inDouble) { + if (toggleQuote(source, i, quotes) || quotes.inSingle || quotes.inDouble) { body += inner; i += 1; continue; @@ -330,7 +347,7 @@ function readBraceBody(source, start) { function extractBraceGroups(input) { const source = String(input || ''); const groups = []; - const quotes = { inSingle: false, inDouble: false }; + const quotes = { inSingle: false, inDouble: false, ansiC: false }; for (let i = 0; i < source.length; i++) { const skipped = skipOuterSpan(source, i, quotes); diff --git a/tests/hooks/gateguard-fact-force.test.js b/tests/hooks/gateguard-fact-force.test.js index f4ec8c0f3..7c694cf46 100644 --- a/tests/hooks/gateguard-fact-force.test.js +++ b/tests/hooks/gateguard-fact-force.test.js @@ -2963,6 +2963,14 @@ function runTests() { passed++; else failed++; + if ( + test("denies rm -rf in a (...) group after an escaped apostrophe in $'...'", () => { + expectDestructiveDeny("echo $'it\\'s'; (rm -rf /tmp/junk)", 'ANSI-C string before subshell'); + }) + ) + passed++; + else failed++; + // Negative cases — literals and non-destructive commands must NOT // be promoted to destructive by the new grouping-body walker. diff --git a/tests/hooks/pre-bash-dev-server-block.test.js b/tests/hooks/pre-bash-dev-server-block.test.js index a5d47c8e2..c7416d493 100644 --- a/tests/hooks/pre-bash-dev-server-block.test.js +++ b/tests/hooks/pre-bash-dev-server-block.test.js @@ -226,6 +226,11 @@ function runTests() { assert.strictEqual(result.code, 2, `Expected exit code 2, got ${result.code}`); }) ? passed++ : failed++); + (test("blocks echo $'it\\'s'; (npm run dev) — escaped apostrophe does not close $'...'", () => { + const result = runScript("echo $'it\\'s'; (npm run dev)"); + assert.strictEqual(result.code, 2, `Expected exit code 2, got ${result.code}`); + }) ? passed++ : failed++); + (test('allows $(echo "(npm run dev)") — () inside double-quoted substitution body is literal', () => { const result = runScript('$(echo "(npm run dev)")'); assert.strictEqual(result.code, 0, `Expected exit code 0, got ${result.code}`); diff --git a/tests/lib/shell-substitution.test.js b/tests/lib/shell-substitution.test.js index 432e7c434..8bbc2b16a 100644 --- a/tests/lib/shell-substitution.test.js +++ b/tests/lib/shell-substitution.test.js @@ -160,6 +160,24 @@ test('a double-quoted string ending in an escaped backslash still closes', () => test('a single-quoted string ending in a backslash still closes', () => { assert.deepStrictEqual(extractSubshellGroups("echo 'C:\\'; (npm run dev)"), ['npm run dev']); }); +test("an escaped apostrophe does not close a $'...' string", () => { + assert.deepStrictEqual(extractSubshellGroups("echo $'it\\'s'; (rm -rf /tmp/x)"), ['rm -rf /tmp/x']); +}); +test("an escaped $ before a quote opens a plain single-quoted string", () => { + assert.deepStrictEqual(extractSubshellGroups("echo \\$'C:\\'; (npm run dev)"), ['npm run dev']); +}); +test('a $() span ending a quote after a backslash still closes', () => { + assert.deepStrictEqual(extractSubshellGroups('x=$(echo "C:\\\\"); (npm run dev)'), ['npm run dev']); +}); +test('a (...) body with a quote ending in a backslash keeps its nested group', () => { + assert.deepStrictEqual(extractSubshellGroups("(echo 'C:\\'; (npm run dev))"), [ + "echo 'C:\\'; (npm run dev)", + 'npm run dev' + ]); +}); +test("a (...) body with an escaped apostrophe in $'...' still closes", () => { + assert.deepStrictEqual(extractSubshellGroups("(echo $'it\\'s'; npm run dev)"), ["echo $'it\\'s'; npm run dev"]); +}); console.log('\nextractSubshellGroups - security-relevant:'); test('surfaces a destructive command inside a subshell', () => { @@ -182,6 +200,12 @@ test('extracts a { ...; } body', () => { test('extracts a { ...; } body after a string ending in an escaped backslash', () => { assert.deepStrictEqual(extractBraceGroups('echo "C:\\\\"; { npm run dev; }'), [' npm run dev; ']); }); +test("extracts a { ...; } body after an escaped apostrophe in $'...'", () => { + assert.deepStrictEqual(extractBraceGroups("echo $'it\\'s'; { rm -rf /tmp/x; }"), [' rm -rf /tmp/x; ']); +}); +test('a { ...; } body with a quote ending in a backslash still closes', () => { + assert.deepStrictEqual(extractBraceGroups('{ echo "C:\\\\"; }; { npm run dev; }'), [' echo "C:\\\\"; ', ' npm run dev; ']); +}); test('nested brace group returns outer body then inner body', () => { assert.deepStrictEqual(extractBraceGroups('{ a; { b; }; }'), [' a; { b; }; ', ' b; ']); }); From b0070af9d1dfebbf80a2e8e2876b9bd4ec5e3aa4 Mon Sep 17 00:00:00 2001 From: leoca Date: Thu, 29 Oct 2026 18:58:18 +0000 Subject: [PATCH 4/4] fix(hooks): return new quote state from toggleQuote instead of mutating it AGENTS.md asks for new objects rather than mutation. toggleQuote now returns the updated quote state (or null when the character is not a quote it toggles), skipOuterSpan returns it alongside the skipped index, and each scanner reassigns its own local state. --- scripts/lib/shell-substitution.js | 70 ++++++++++++++++++------------- 1 file changed, 40 insertions(+), 30 deletions(-) diff --git a/scripts/lib/shell-substitution.js b/scripts/lib/shell-substitution.js index 5b747f0d0..efe6a9be1 100644 --- a/scripts/lib/shell-substitution.js +++ b/scripts/lib/shell-substitution.js @@ -120,23 +120,21 @@ function escapesActive(quotes) { } /** - * Toggle quote state for the quote character at `source[i]`. - * Returns true when it opened or closed a quote. Callers consume escapes + * Quote state after the quote character at `source[i]`, as a new object, or + * null when it neither opens nor closes a quote. Callers consume escapes * first, so a quote after a backslash here still closes (`'C:\'`, `"C:\\"`), * while `\'` inside `$'...'` is consumed before it can close the string. */ function toggleQuote(source, i, quotes) { const ch = source[i]; if (ch === "'" && !quotes.inDouble) { - quotes.inSingle = !quotes.inSingle; - quotes.ansiC = quotes.inSingle && opensAnsiC(source, i); - return true; + const inSingle = !quotes.inSingle; + return { ...quotes, inSingle, ansiC: inSingle && opensAnsiC(source, i) }; } if (ch === '"' && !quotes.inSingle) { - quotes.inDouble = !quotes.inDouble; - return true; + return { ...quotes, inDouble: !quotes.inDouble }; } - return false; + return null; } /** @@ -144,7 +142,7 @@ function toggleQuote(source, i, quotes) { * is at `start`, honoring quotes and escapes inside the span. */ function findParenSpanEnd(source, start) { - const quotes = { inSingle: false, inDouble: false, ansiC: false }; + let quotes = { inSingle: false, inDouble: false, ansiC: false }; let depth = 1; let i = start; while (i < source.length && depth > 0) { @@ -153,7 +151,10 @@ function findParenSpanEnd(source, start) { i += 2; continue; } - if (!toggleQuote(source, i, quotes) && !quotes.inSingle && !quotes.inDouble) { + const toggled = toggleQuote(source, i, quotes); + if (toggled) { + quotes = toggled; + } else if (!quotes.inSingle && !quotes.inDouble) { depth += parenDelta(inner); } i += 1; @@ -179,17 +180,19 @@ function findBacktickEnd(source, start) { /** * Top-level scan step shared by the group extractors: escapes, quote state, - * `$(...)` and backtick spans. Returns the last index consumed so a `for` - * loop's `i++` resumes after it, or -1 when the caller must inspect `source[i]`. + * `$(...)` and backtick spans. Returns the quote state after `source[i]` and + * `skipped`, the last index consumed so a `for` loop's `i++` resumes after it, + * or -1 when the caller must inspect `source[i]`. */ function skipOuterSpan(source, i, quotes) { const ch = source[i]; - if (ch === '\\' && escapesActive(quotes)) return i + 1; - if (toggleQuote(source, i, quotes)) return i; - if (quotes.inSingle || quotes.inDouble) return i; - if (ch === '$' && source[i + 1] === '(') return findParenSpanEnd(source, i + 2) - 1; - if (ch === '`') return findBacktickEnd(source, i + 1); - return -1; + if (ch === '\\' && escapesActive(quotes)) return { skipped: i + 1, quotes }; + const toggled = toggleQuote(source, i, quotes); + if (toggled) return { skipped: i, quotes: toggled }; + if (quotes.inSingle || quotes.inDouble) return { skipped: i, quotes }; + if (ch === '$' && source[i + 1] === '(') return { skipped: findParenSpanEnd(source, i + 2) - 1, quotes }; + if (ch === '`') return { skipped: findBacktickEnd(source, i + 1), quotes }; + return { skipped: -1, quotes }; } /** @@ -197,7 +200,7 @@ function skipOuterSpan(source, i, quotes) { * the body text and the index of the closing paren (or where input ran out). */ function readSubshellBody(source, start) { - const quotes = { inSingle: false, inDouble: false, ansiC: false }; + let quotes = { inSingle: false, inDouble: false, ansiC: false }; let depth = 1; let body = ''; let i = start; @@ -210,7 +213,10 @@ function readSubshellBody(source, start) { i = Math.min(i + 2, source.length); continue; } - if (!toggleQuote(source, i, quotes) && !quotes.inSingle && !quotes.inDouble) { + const toggled = toggleQuote(source, i, quotes); + if (toggled) { + quotes = toggled; + } else if (!quotes.inSingle && !quotes.inDouble) { depth += parenDelta(inner); if (depth === 0) break; } @@ -240,12 +246,13 @@ function readSubshellBody(source, start) { function extractSubshellGroups(input) { const source = String(input || ''); const groups = []; - const quotes = { inSingle: false, inDouble: false, ansiC: false }; + let quotes = { inSingle: false, inDouble: false, ansiC: false }; for (let i = 0; i < source.length; i++) { - const skipped = skipOuterSpan(source, i, quotes); - if (skipped !== -1) { - i = skipped; + const step = skipOuterSpan(source, i, quotes); + quotes = step.quotes; + if (step.skipped !== -1) { + i = step.skipped; continue; } if (source[i] !== '(') continue; @@ -285,7 +292,7 @@ function opensBraceGroup(source, i) { * the body text and the index of the closing brace (or where input ran out). */ function readBraceBody(source, start) { - const quotes = { inSingle: false, inDouble: false, ansiC: false }; + let quotes = { inSingle: false, inDouble: false, ansiC: false }; let depth = 1; let body = ''; let i = start; @@ -297,7 +304,9 @@ function readBraceBody(source, start) { i = Math.min(i + 2, source.length); continue; } - if (toggleQuote(source, i, quotes) || quotes.inSingle || quotes.inDouble) { + const toggled = toggleQuote(source, i, quotes); + if (toggled) quotes = toggled; + if (toggled || quotes.inSingle || quotes.inDouble) { body += inner; i += 1; continue; @@ -347,12 +356,13 @@ function readBraceBody(source, start) { function extractBraceGroups(input) { const source = String(input || ''); const groups = []; - const quotes = { inSingle: false, inDouble: false, ansiC: false }; + let quotes = { inSingle: false, inDouble: false, ansiC: false }; for (let i = 0; i < source.length; i++) { - const skipped = skipOuterSpan(source, i, quotes); - if (skipped !== -1) { - i = skipped; + const step = skipOuterSpan(source, i, quotes); + quotes = step.quotes; + if (step.skipped !== -1) { + i = step.skipped; continue; } if (source[i] === '(') {