Skip to content

fix: keep $ on nested variables in "${...}" interpolation - #2520

Open
Dextheking1 wants to merge 1 commit into
prettier:mainfrom
Dextheking1:fix/encapsed-nested-var-dollar
Open

Dextheking1 wants to merge 1 commit into
prettier:mainfrom
Dextheking1:fix/encapsed-nested-var-dollar

Conversation

@Dextheking1

Copy link
Copy Markdown

Bug: "${foo[$index_var]}" was printed as "${foo[index_var]}" — the $ of the nested offset variable was swallowed, silently changing semantics (variable becomes a bare constant).

Fix: in the variable printer (src/printer.mjs), the $ was dropped for any variable whose parent was an offsetlookup under a curly-simple encapsedpart. That is only correct for the base variable of the lookup, which directly follows the ${ emitted by the encapsedpart printer. The condition is now restricted to parent.what === node, so a variable in the offset position keeps its $.

Tests: added "text ${foo[$index_var]} text" to tests/encapsed/encapsed.php; snapshots updated with jest -u. Full suite: 101 suites / 595 tests pass, plus AST_COMPARE=1 round-trip on the encapsed suite. Verified output keeps $index_var and that the pre-fix code reproduced the bug ("${foo[index_var]}").

Closes #2177

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 prettier#2177
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dollar sign from variable name swallowed in double quoted Strings

1 participant