Skip to content

Commit 6183df1

Browse files
authored
fix: keep $ on nested variables in "${...}" interpolation (#2520)
Printing `"${foo[$index_var]}"` swallowed the dollar sign of the nested variable, producing `"${foo[index_var]}"` and silently changing semantics (the variable becomes a bare constant). The `variable` printer dropped `$` for any variable whose parent was an offsetlookup under a curly-simple encapsedpart, but only the base variable of the lookup (which directly follows the `${` emitted by the encapsedpart printer) may lose its `$`; a variable in the offset position is nested and keeps its own `$`. Closes #2177 Co-authored-by: Dextheking1 <Dextheking1@users.noreply.github.com>
1 parent b7711d0 commit 6183df1

3 files changed

Lines changed: 9 additions & 0 deletions

File tree

‎src/printer.mjs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2314,6 +2314,10 @@ function printNode(path, options, print) {
23142314
parent.curly) ||
23152315
(parentParent &&
23162316
parent.kind === "offsetlookup" &&
2317+
// Only the base variable of the lookup follows the `${` printed
2318+
// by the encapsedpart; a variable in the offset position is a
2319+
// nested variable and keeps its own `$`.
2320+
parent.what === node &&
23172321
parentParent.kind === "encapsedpart" &&
23182322
parentParent.syntax === "simple" &&
23192323
parentParent.curly)

‎tests/encapsed/__snapshots__/jsfmt.spec.mjs.snap‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ echo "text \${foo['foo']} text";
414414
echo "text \${foo[foo]} text";
415415
echo "text \${foo[call()]} text";
416416
echo "text \${foo[$var ? 'foo' : 'bar']} text";
417+
echo "text \${foo[$index_var]} text";
417418
418419
// T_CURLY_OPEN variable '}'
419420
echo "text {$var} text";
@@ -952,6 +953,7 @@ echo "text \${foo["foo"]} text";
952953
echo "text \${foo[foo]} text";
953954
echo "text \${foo[call()]} text";
954955
echo "text \${foo[$var ? "foo" : "bar"]} text";
956+
echo "text \${foo[$index_var]} text";
955957
956958
// T_CURLY_OPEN variable '}'
957959
echo "text {$var} text";
@@ -1449,6 +1451,7 @@ echo "text \${foo['foo']} text";
14491451
echo "text \${foo[foo]} text";
14501452
echo "text \${foo[call()]} text";
14511453
echo "text \${foo[$var ? 'foo' : 'bar']} text";
1454+
echo "text \${foo[$index_var]} text";
14521455
14531456
// T_CURLY_OPEN variable '}'
14541457
echo "text {$var} text";
@@ -1988,6 +1991,7 @@ echo "text \${foo["foo"]} text";
19881991
echo "text \${foo[foo]} text";
19891992
echo "text \${foo[call()]} text";
19901993
echo "text \${foo[$var ? "foo" : "bar"]} text";
1994+
echo "text \${foo[$index_var]} text";
19911995
19921996
// T_CURLY_OPEN variable '}'
19931997
echo "text {$var} text";

‎tests/encapsed/encapsed.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ function foo1(
405405
echo "text ${foo[foo]} text";
406406
echo "text ${foo[call()]} text";
407407
echo "text ${foo[$var ? 'foo' : 'bar']} text";
408+
echo "text ${foo[$index_var]} text";
408409

409410
// T_CURLY_OPEN variable '}'
410411
echo "text {$var} text";

0 commit comments

Comments
 (0)