fix(text): preserve indent inside substituted values in dedent (#6830)#7160
fix(text): preserve indent inside substituted values in dedent (#6830)#7160LeSingh1 wants to merge 1 commit into
Conversation
…and#6830) `dedent` computed the common indent from the joined template (with a single-char placeholder for each substitution) and then stripped that indent from the FULLY substituted string. When a multi-line substitution's lines happened to start with whitespace matching the outer template's indent, those leading spaces were stripped too — breaking the substituted value. Reproduction from denoland#6830 (and denoland#6665 partially): `inner` is dedented correctly, but interpolating it back into an outer dedent with 6-space outer indent eats the 6 spaces from `inner`'s ` ...` line, turning the rendered substitution into `...`. Restructure the tagged-template branch to apply indent stripping per literal template part (the entries in `input.raw`), then interleave substitution values verbatim. The first literal part also gets a start-of-template strip for templates that begin with content (no leading newline). String-input mode is unchanged. Two new tests cover the issue's exact V1/V2 reproducer and a narrower regression where a substituted value begins with whitespace that matches the outer indent. Full dedent suite (13 tests / 84 steps) stays green.
|
Reviewed and ran the code locally on the branch. The per-part restructuring is the right idea for #6830, and the new tests pass — but there's a blocking correctness regression: switching from the cooked template strings to 🔴 Blocking:
|
Fixes #6830 (and the residual cases from #6665).
Problem
dedentcomputed the common indent using the template joined with a single-char\"x\"placeholder, then stripped that indent from the fully substituted input string. When a multi-line substitution's lines happened to start with whitespace matching the outer template's computed indent, those leading spaces were stripped too — silently breaking the substituted value.Reproduction from the issue:
innerdedents correctly to\" [\\n ...\\n...\\n ]\". Interpolating it back into an outer dedent with 6-space outer indent eats the 6 leading spaces frominner's\" ...\"line, rendering it as\"...\". V1 and V2 in the issue body diverge even though they should be identical.Fix
Restructure the tagged-template branch to apply indent stripping per literal template part (the entries in
input.raw), then interleave substitution values verbatim. The first literal part also gets a start-of-template strip for templates that begin with content (no leading newline). String-input mode is unchanged.Concretely, instead of building the full substituted string and running
replaceAll(^indent/gmu, \"\")on it, the new code runsreplaceAll(\\n${indent}/gu, \"\\n\")(plus a once-only^${indent}for the very first part) on eachinput.raw[i]and then concatenates the substitution value after each processed part.Tests
Two new cases in
text/unstable_dedent_test.ts:preserves indentation inside multi-line substitution values (#6830)— the exact V1/V2 reproducer from the issue. Both must equal\a\n b\n${inner}``.does not strip indent that originated inside a substituted value— narrower regression:${\" leading-four-spaces\"}inside a 6-space outer template must keep its 4 leading spaces.