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
fix: handle assignref in needsParens like assign
assignref was missing from the needsParens switch, falling through
to return false. This caused parens to be dropped in contexts where
they change semantics, e.g. $a = ($b =& $var) + 5 became
$a = $b = &$var + 5.
  • Loading branch information
jorgsowa committed Mar 25, 2026
commit 50ad046d090d09856938ef36dbaf486eb19ed000
3 changes: 2 additions & 1 deletion src/needs-parens.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,14 @@ function needsParens(path, options) {
return !!(node.key || node.value);
}
}
case "assignref":
case "assign": {
if (
parent.kind === "for" &&
(parent.init.includes(node) || parent.increment.includes(node))
) {
return false;
} else if (parent.kind === "assign") {
} else if (parent.kind === "assign" || parent.kind === "assignref") {
return false;
} else if (parent.kind === "static") {
return false;
Expand Down
6 changes: 4 additions & 2 deletions tests/assignref/__snapshots__/jsfmt.spec.mjs.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing

exports[`assignref.php 1`] = `
====================================options=====================================
Expand Down Expand Up @@ -42,6 +42,7 @@ $cached_var = &drupal_static(__FUNCTION__);

$test = ['key' => &$value];
$test = ['key' => &$value['something']];
$x = $y =& $z;

=====================================output=====================================
<?php
Expand All @@ -56,7 +57,7 @@ class Foo
}
}

$a = &$b / 100;
($a = &$b) / 100;

$var =
$arr[
Expand Down Expand Up @@ -96,6 +97,7 @@ $cached_var = &drupal_static(__FUNCTION__);

$test = ["key" => &$value];
$test = ["key" => &$value["something"]];
$x = $y = &$z;

================================================================================
`;
1 change: 1 addition & 0 deletions tests/assignref/assignref.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ public function test() {

$test = ['key' => &$value];
$test = ['key' => &$value['something']];
$x = $y =& $z;
16 changes: 8 additions & 8 deletions tests/parens/__snapshots__/jsfmt.spec.mjs.snap
Original file line number Diff line number Diff line change
Expand Up @@ -672,14 +672,14 @@ $test = $var = &$test;
$test = $var = &$test;

$a = ($b = $var) + 5;
$a = $b = &$var + 5;
$a = ($b = &$var) + 5;

$a = ($b = $var) || 5;
$a = $b = &$var || 5;
$a = ($b = &$var) || 5;

if ($foo = &$bar && count($foo) > 0) {
if (($foo = &$bar) && count($foo) > 0) {
}
if ($foo = &test1() && test2($foo) > 0) {
if (($foo = &test1()) && test2($foo) > 0) {
}

call($a = &$b);
Expand Down Expand Up @@ -720,14 +720,14 @@ $test = $var = &$test;
$test = $var = &$test;

$a = ($b = $var) + 5;
$a = $b = &$var + 5;
$a = ($b = &$var) + 5;

$a = ($b = $var) || 5;
$a = $b = &$var || 5;
$a = ($b = &$var) || 5;

if ($foo = &$bar && count($foo) > 0) {
if (($foo = &$bar) && count($foo) > 0) {
}
if ($foo = &test1() && test2($foo) > 0) {
if (($foo = &test1()) && test2($foo) > 0) {
}

call($a = &$b);
Expand Down
Loading