Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/printer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2063,6 +2063,11 @@ function printNode(path, options, print) {
}
case "foreach": {
const body = printBodyControlStructure(path, options, print, "body");
// Only a plain variable prints its own &
const value =
node.value.byref && node.value.kind !== "variable"
? ["&", print("value")]
: print("value");

// We want to keep dangling comments above the loop to stay consistent.
// Any comment positioned between the for statement and the parentheses
Expand All @@ -2086,8 +2091,8 @@ function printNode(path, options, print) {
"as ",
group(
node.key
? indent(join([" =>", line], [print("key"), print("value")]))
: print("value")
? indent(join([" =>", line], [print("key"), value]))
: value
),
]),
softline,
Expand Down
12 changes: 12 additions & 0 deletions tests/foreach/__snapshots__/jsfmt.spec.mjs.snap
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ foreach ($test as &$i) {
$test2 = $i;
}

foreach ($test as &$object->property) {
}

foreach ($test as $key => &$array[$key]) {
}

=====================================output=====================================
<?php
foreach ($test as $i) {
Expand Down Expand Up @@ -170,5 +176,11 @@ foreach ($test as &$i) {
$test2 = $i;
}

foreach ($test as &$object->property) {
}

foreach ($test as $key => &$array[$key]) {
}

================================================================================
`;
6 changes: 6 additions & 0 deletions tests/foreach/foreach.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,9 @@
foreach ($test as &$i) {
$test2 = $i;
}

foreach ($test as &$object->property) {
}

foreach ($test as $key => &$array[$key]) {
}