Skip to content

Commit 86ce94e

Browse files
committed
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.
1 parent cf80f5e commit 86ce94e

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

src/needs-parens.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,14 @@ function needsParens(path, options) {
158158
return !!(node.key || node.value);
159159
}
160160
}
161+
case "assignref":
161162
case "assign": {
162163
if (
163164
parent.kind === "for" &&
164165
(parent.init.includes(node) || parent.increment.includes(node))
165166
) {
166167
return false;
167-
} else if (parent.kind === "assign") {
168+
} else if (parent.kind === "assign" || parent.kind === "assignref") {
168169
return false;
169170
} else if (parent.kind === "static") {
170171
return false;

tests/assignref/__snapshots__/jsfmt.spec.mjs.snap

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

33
exports[`assignref.php 1`] = `
44
====================================options=====================================
@@ -42,6 +42,7 @@ $cached_var = &drupal_static(__FUNCTION__);
4242
4343
$test = ['key' => &$value];
4444
$test = ['key' => &$value['something']];
45+
$x = $y =& $z;
4546
4647
=====================================output=====================================
4748
<?php
@@ -56,7 +57,7 @@ class Foo
5657
}
5758
}
5859
59-
$a = &$b / 100;
60+
($a = &$b) / 100;
6061
6162
$var =
6263
$arr[
@@ -96,6 +97,7 @@ $cached_var = &drupal_static(__FUNCTION__);
9697
9798
$test = ["key" => &$value];
9899
$test = ["key" => &$value["something"]];
100+
$x = $y = &$z;
99101
100102
================================================================================
101103
`;

tests/assignref/assignref.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ public function test() {
3434

3535
$test = ['key' => &$value];
3636
$test = ['key' => &$value['something']];
37+
$x = $y =& $z;

tests/parens/__snapshots__/jsfmt.spec.mjs.snap

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -672,14 +672,14 @@ $test = $var = &$test;
672672
$test = $var = &$test;
673673
674674
$a = ($b = $var) + 5;
675-
$a = $b = &$var + 5;
675+
$a = ($b = &$var) + 5;
676676
677677
$a = ($b = $var) || 5;
678-
$a = $b = &$var || 5;
678+
$a = ($b = &$var) || 5;
679679
680-
if ($foo = &$bar && count($foo) > 0) {
680+
if (($foo = &$bar) && count($foo) > 0) {
681681
}
682-
if ($foo = &test1() && test2($foo) > 0) {
682+
if (($foo = &test1()) && test2($foo) > 0) {
683683
}
684684
685685
call($a = &$b);
@@ -720,14 +720,14 @@ $test = $var = &$test;
720720
$test = $var = &$test;
721721
722722
$a = ($b = $var) + 5;
723-
$a = $b = &$var + 5;
723+
$a = ($b = &$var) + 5;
724724
725725
$a = ($b = $var) || 5;
726-
$a = $b = &$var || 5;
726+
$a = ($b = &$var) || 5;
727727
728-
if ($foo = &$bar && count($foo) > 0) {
728+
if (($foo = &$bar) && count($foo) > 0) {
729729
}
730-
if ($foo = &test1() && test2($foo) > 0) {
730+
if (($foo = &test1()) && test2($foo) > 0) {
731731
}
732732
733733
call($a = &$b);

0 commit comments

Comments
 (0)