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
5 changes: 4 additions & 1 deletion src/needs-parens.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@ function needsParens(path, options) {
} else if (parent.kind === "silent") {
return false;
} else if (parent.kind === "call") {
return false;
// `($fn = function () {})()` needs the parentheses: without them
// the call binds to the closure instead of the assignment result.
// https://github.com/prettier/plugin-php/issues/2409
return key === "what";
}

return true;
Expand Down
35 changes: 35 additions & 0 deletions tests/assign-call/__snapshots__/jsfmt.spec.mjs.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing

exports[`assign-call.php 1`] = `
====================================options=====================================
parsers: ["php"]
printWidth: 80
| printWidth
=====================================input======================================
<?php
($fib = function () { return 42; })(10);

$result = ($fib = function ($n) use (&$fib) {
return $n <= 1 ? $n : $fib($n - 1) + $fib($n - 2);
})(10);

($fn = fn () => 42)();

call($a = $b);

=====================================output=====================================
<?php
($fib = function () {
return 42;
})(10);

$result = ($fib = function ($n) use (&$fib) {
return $n <= 1 ? $n : $fib($n - 1) + $fib($n - 2);
})(10);

($fn = fn() => 42)();

call($a = $b);

================================================================================
`;
10 changes: 10 additions & 0 deletions tests/assign-call/assign-call.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
($fib = function () { return 42; })(10);

$result = ($fib = function ($n) use (&$fib) {
return $n <= 1 ? $n : $fib($n - 1) + $fib($n - 2);
})(10);

($fn = fn () => 42)();

call($a = $b);
1 change: 1 addition & 0 deletions tests/assign-call/jsfmt.spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
run_spec(import.meta, ["php"]);
Loading