diff --git a/src/printer.mjs b/src/printer.mjs index f789a340b..cda9bb7a1 100644 --- a/src/printer.mjs +++ b/src/printer.mjs @@ -2314,6 +2314,10 @@ function printNode(path, options, print) { parent.curly) || (parentParent && parent.kind === "offsetlookup" && + // Only the base variable of the lookup follows the `${` printed + // by the encapsedpart; a variable in the offset position is a + // nested variable and keeps its own `$`. + parent.what === node && parentParent.kind === "encapsedpart" && parentParent.syntax === "simple" && parentParent.curly) diff --git a/tests/encapsed/__snapshots__/jsfmt.spec.mjs.snap b/tests/encapsed/__snapshots__/jsfmt.spec.mjs.snap index 8c5c4dcea..0b8acbec2 100644 --- a/tests/encapsed/__snapshots__/jsfmt.spec.mjs.snap +++ b/tests/encapsed/__snapshots__/jsfmt.spec.mjs.snap @@ -414,6 +414,7 @@ echo "text \${foo['foo']} text"; echo "text \${foo[foo]} text"; echo "text \${foo[call()]} text"; echo "text \${foo[$var ? 'foo' : 'bar']} text"; +echo "text \${foo[$index_var]} text"; // T_CURLY_OPEN variable '}' echo "text {$var} text"; @@ -952,6 +953,7 @@ echo "text \${foo["foo"]} text"; echo "text \${foo[foo]} text"; echo "text \${foo[call()]} text"; echo "text \${foo[$var ? "foo" : "bar"]} text"; +echo "text \${foo[$index_var]} text"; // T_CURLY_OPEN variable '}' echo "text {$var} text"; @@ -1449,6 +1451,7 @@ echo "text \${foo['foo']} text"; echo "text \${foo[foo]} text"; echo "text \${foo[call()]} text"; echo "text \${foo[$var ? 'foo' : 'bar']} text"; +echo "text \${foo[$index_var]} text"; // T_CURLY_OPEN variable '}' echo "text {$var} text"; @@ -1988,6 +1991,7 @@ echo "text \${foo["foo"]} text"; echo "text \${foo[foo]} text"; echo "text \${foo[call()]} text"; echo "text \${foo[$var ? "foo" : "bar"]} text"; +echo "text \${foo[$index_var]} text"; // T_CURLY_OPEN variable '}' echo "text {$var} text"; diff --git a/tests/encapsed/encapsed.php b/tests/encapsed/encapsed.php index 8cb08d2a0..2ee2d204d 100644 --- a/tests/encapsed/encapsed.php +++ b/tests/encapsed/encapsed.php @@ -405,6 +405,7 @@ function foo1( echo "text ${foo[foo]} text"; echo "text ${foo[call()]} text"; echo "text ${foo[$var ? 'foo' : 'bar']} text"; +echo "text ${foo[$index_var]} text"; // T_CURLY_OPEN variable '}' echo "text {$var} text";