diff --git a/src/needs-parens.mjs b/src/needs-parens.mjs index 2bd051fc0..75fbc8cc5 100644 --- a/src/needs-parens.mjs +++ b/src/needs-parens.mjs @@ -29,6 +29,10 @@ function needsParens(path, options) { } switch (node.kind) { + case "intersectiontype": + // An intersection inside a union is a DNF type and must keep its + // parentheses: `(A&B)|C` is valid PHP, `A&B|C` is a parse error. + return parent.kind === "uniontype"; case "pre": case "post": if (parent.kind === "unary") { diff --git a/tests/dnf-types/__snapshots__/jsfmt.spec.mjs.snap b/tests/dnf-types/__snapshots__/jsfmt.spec.mjs.snap new file mode 100644 index 000000000..2f19ac6fc --- /dev/null +++ b/tests/dnf-types/__snapshots__/jsfmt.spec.mjs.snap @@ -0,0 +1,41 @@ +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing + +exports[`dnf.php 1`] = ` +====================================options===================================== +parsers: ["php"] +printWidth: 80 + | printWidth +=====================================input====================================== + $x; + +=====================================output===================================== + $x; + +================================================================================ +`; diff --git a/tests/dnf-types/dnf.php b/tests/dnf-types/dnf.php new file mode 100644 index 000000000..c68cdcce9 --- /dev/null +++ b/tests/dnf-types/dnf.php @@ -0,0 +1,14 @@ + $x; diff --git a/tests/dnf-types/jsfmt.spec.mjs b/tests/dnf-types/jsfmt.spec.mjs new file mode 100644 index 000000000..8829c872f --- /dev/null +++ b/tests/dnf-types/jsfmt.spec.mjs @@ -0,0 +1 @@ +run_spec(import.meta, ["php"]);