Skip to content

feat: format new without parenthesis in PHP 8.4 #2422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
],
"dependencies": {
"linguist-languages": "^7.27.0",
"php-parser": "^3.1.5"
"php-parser": "^3.2.3"
},
"devDependencies": {
"@babel/preset-env": "^7.23.6",
Expand Down
16 changes: 12 additions & 4 deletions src/needs-parens.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { getPrecedence, shouldFlatten, isBitwiseOperator } from "./util.mjs";

function needsParens(path) {
import {
getPrecedence,
shouldFlatten,
isBitwiseOperator,
isMinVersion,
} from "./util.mjs";

function needsParens(path, options) {
const { parent } = path;

if (!parent) {
Expand Down Expand Up @@ -128,13 +133,16 @@ function needsParens(path) {
}
case "clone":
case "new": {
const requiresParens =
node.kind === "clone" ||
(node.kind === "new" && !isMinVersion(options.phpVersion, "8.4"));
switch (parent.kind) {
case "propertylookup":
case "nullsafepropertylookup":
case "staticlookup":
case "offsetlookup":
case "call":
return key === "what";
return key === "what" && requiresParens;
default:
return false;
}
Expand Down
1 change: 1 addition & 0 deletions src/options.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default {
{ value: "8.0" },
{ value: "8.1" },
{ value: "8.2" },
{ value: "8.4" },
],
},
trailingCommaPHP: {
Expand Down
11 changes: 8 additions & 3 deletions src/parser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ function parse(text, opts) {
// Todo https://github.com/glayzzle/php-parser/issues/170
text = text.replace(/\?>\n<\?/g, "?>\n___PSEUDO_INLINE_PLACEHOLDER___<?");

const parserOpts = Object.assign(
{ extractDoc: true },
// only pass the version if user requested 8.4 syntax; parser is stricter
// about allowed syntax than we are and currenly defaults to support for 8.3
opts && opts.phpVersion === "8.4" ? { version: opts.phpVersion } : {}
);

// initialize a new parser instance
const parser = new engine({
parser: {
extractDoc: true,
},
parser: parserOpts,
ast: {
withPositions: true,
withSource: true,
Expand Down
17 changes: 8 additions & 9 deletions src/printer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
isReferenceLikeNode,
normalizeMagicMethodName,
isSimpleCallArgument,
isMinVersion,
} from "./util.mjs";

const {
Expand Down Expand Up @@ -65,10 +66,6 @@ const {
isPreviousLineEmpty,
} = prettierUtil;

function isMinVersion(actualVersion, requiredVersion) {
return parseFloat(actualVersion) >= parseFloat(requiredVersion);
}

function shouldPrintComma(options, requiredVersion) {
if (!options.trailingCommaPHP) {
return false;
Expand Down Expand Up @@ -777,11 +774,12 @@ function printBinaryExpression(
parts.push(print("left"));
}

const shouldInline = shouldInlineLogicalExpression(node);

const right = shouldInline
? [node.type, " ", print("right")]
: [node.type, line, print("right")];
const right =
node.left.kind === "new" && node.type !== "."
? [node.type, print("right")]
: shouldInlineLogicalExpression(node)
? [node.type, " ", print("right")]
: [node.type, line, print("right")];

// If there's only a single binary expression, we want to create a group
// in order to avoid having a small right part like -1 be on its own line.
Expand All @@ -797,6 +795,7 @@ function printBinaryExpression(

const shouldNotHaveWhitespace =
isDocNode(node.left) ||
(node.left.kind === "new" && node.type !== ".") ||
(node.left.kind === "bin" && isDocNode(node.left.right));

parts.push(
Expand Down
5 changes: 5 additions & 0 deletions src/util.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,10 @@ function memoize(fn) {
};
}

function isMinVersion(actualVersion, requiredVersion) {
return parseFloat(actualVersion) >= parseFloat(requiredVersion);
}

export {
printNumber,
getPrecedence,
Expand Down Expand Up @@ -740,4 +744,5 @@ export {
normalizeMagicMethodName,
isSimpleCallArgument,
memoize,
isMinVersion,
};
4 changes: 0 additions & 4 deletions tests/comments/__snapshots__/jsfmt.spec.mjs.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,6 @@ $var = new class {
const
// Comment 1
CONSTANT
// Comment 2
=
// Comment 3
'string';
Expand All @@ -1033,7 +1032,6 @@ $var = new class {
const
// Comment 2
CONSTANT
// Comment 3
=
// Comment 4
'string';
Expand All @@ -1058,7 +1056,6 @@ $var = new class {
$var = new class {
// Comment 1
const CONSTANT =
// Comment 2
// Comment 3
"string";
};
Expand All @@ -1067,7 +1064,6 @@ $var = new class {
// Comment 1
// Comment 2
public const CONSTANT =
// Comment 3
// Comment 4
"string";
};
Expand Down
2 changes: 0 additions & 2 deletions tests/comments/classconstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
const
// Comment 1
CONSTANT
// Comment 2
=
// Comment 3
'string';
Expand All @@ -29,7 +28,6 @@
const
// Comment 2
CONSTANT
// Comment 3
=
// Comment 4
'string';
Expand Down
Loading
Loading