Skip to content

Commit 031d14e

Browse files
feat: format new without parenthesis in PHP 8.4 (#2422)
* deps: bump php-parser to 3.2.5 * feat: add php 8.4 option * refactor: move isMinVersion to utils, update needsParen fn signature needsParens() is already being called with 2 params, but the fn signature didn't reflect that * test: prep snapshots for php 8.4 new w/o parens * feat: support new w/o parens in php 8.4
1 parent 14f802a commit 031d14e

File tree

12 files changed

+4505
-402
lines changed

12 files changed

+4505
-402
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
],
2828
"dependencies": {
2929
"linguist-languages": "^8.0.0",
30-
"php-parser": "^3.2.4"
30+
"php-parser": "^3.2.5"
3131
},
3232
"devDependencies": {
3333
"@babel/preset-env": "^7.27.2",

src/needs-parens.mjs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import { getPrecedence, shouldFlatten, isBitwiseOperator } from "./util.mjs";
2-
3-
function needsParens(path) {
1+
import {
2+
getPrecedence,
3+
shouldFlatten,
4+
isBitwiseOperator,
5+
isMinVersion,
6+
} from "./util.mjs";
7+
8+
function needsParens(path, options) {
49
const { parent } = path;
510

611
if (!parent) {
@@ -128,13 +133,16 @@ function needsParens(path) {
128133
}
129134
case "clone":
130135
case "new": {
136+
const requiresParens =
137+
node.kind === "clone" ||
138+
(node.kind === "new" && !isMinVersion(options.phpVersion, "8.4"));
131139
switch (parent.kind) {
132140
case "propertylookup":
133141
case "nullsafepropertylookup":
134142
case "staticlookup":
135143
case "offsetlookup":
136144
case "call":
137-
return key === "what";
145+
return key === "what" && requiresParens;
138146
default:
139147
return false;
140148
}

src/options.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export default {
2424
{ value: "8.1" },
2525
{ value: "8.2" },
2626
{ value: "8.3" },
27+
{ value: "8.4" },
2728
],
2829
},
2930
trailingCommaPHP: {

src/parser.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import engine from "php-parser";
2+
import options from "./options.mjs";
23

34
function parse(text, opts) {
45
const inMarkdown = opts && opts.parentParser === "markdown";
@@ -10,10 +11,15 @@ function parse(text, opts) {
1011
// Todo https://github.com/glayzzle/php-parser/issues/170
1112
text = text.replace(/\?>\n<\?/g, "?>\n___PSEUDO_INLINE_PLACEHOLDER___<?");
1213

14+
const latestSupportedPhpVersion = Math.max(
15+
...options.phpVersion.choices.map((c) => parseFloat(c.value))
16+
);
17+
1318
// initialize a new parser instance
1419
const parser = new engine({
1520
parser: {
1621
extractDoc: true,
22+
version: `${latestSupportedPhpVersion}`,
1723
},
1824
ast: {
1925
withPositions: true,

src/printer.mjs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
isReferenceLikeNode,
3838
normalizeMagicMethodName,
3939
isSimpleCallArgument,
40+
isMinVersion,
4041
} from "./util.mjs";
4142

4243
const {
@@ -65,10 +66,6 @@ const {
6566
isPreviousLineEmpty,
6667
} = prettierUtil;
6768

68-
function isMinVersion(actualVersion, requiredVersion) {
69-
return parseFloat(actualVersion) >= parseFloat(requiredVersion);
70-
}
71-
7269
function shouldPrintComma(options, requiredVersion) {
7370
if (!options.trailingCommaPHP) {
7471
return false;

src/util.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,10 @@ function memoize(fn) {
709709
};
710710
}
711711

712+
function isMinVersion(actualVersion, requiredVersion) {
713+
return parseFloat(actualVersion) >= parseFloat(requiredVersion);
714+
}
715+
712716
export {
713717
printNumber,
714718
getPrecedence,
@@ -740,4 +744,5 @@ export {
740744
normalizeMagicMethodName,
741745
isSimpleCallArgument,
742746
memoize,
747+
isMinVersion,
743748
};

0 commit comments

Comments
 (0)