diff --git a/src/needs-parens.mjs b/src/needs-parens.mjs index 2bd051fc0..5803925b4 100644 --- a/src/needs-parens.mjs +++ b/src/needs-parens.mjs @@ -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; diff --git a/tests/assign-call/__snapshots__/jsfmt.spec.mjs.snap b/tests/assign-call/__snapshots__/jsfmt.spec.mjs.snap new file mode 100644 index 000000000..7ef47e3be --- /dev/null +++ b/tests/assign-call/__snapshots__/jsfmt.spec.mjs.snap @@ -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====================================== + 42)(); + +call($a = $b); + +=====================================output===================================== + 42)(); + +call($a = $b); + +================================================================================ +`; diff --git a/tests/assign-call/assign-call.php b/tests/assign-call/assign-call.php new file mode 100644 index 000000000..019946d0e --- /dev/null +++ b/tests/assign-call/assign-call.php @@ -0,0 +1,10 @@ + 42)(); + +call($a = $b); diff --git a/tests/assign-call/jsfmt.spec.mjs b/tests/assign-call/jsfmt.spec.mjs new file mode 100644 index 000000000..8829c872f --- /dev/null +++ b/tests/assign-call/jsfmt.spec.mjs @@ -0,0 +1 @@ +run_spec(import.meta, ["php"]);