Skip to content
Merged
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 @@ -27,7 +27,7 @@
],
"dependencies": {
"linguist-languages": "^8.0.0",
"php-parser": "^3.2.4"
"php-parser": "^3.2.5"
},
"devDependencies": {
"@babel/preset-env": "^7.27.2",
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 @@ -24,6 +24,7 @@ export default {
{ value: "8.1" },
{ value: "8.2" },
{ value: "8.3" },
{ value: "8.4" },
],
},
trailingCommaPHP: {
Expand Down
6 changes: 6 additions & 0 deletions src/parser.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import engine from "php-parser";
import options from "./options.mjs";

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

const latestSupportedPhpVersion = Math.max(
...options.phpVersion.choices.map((c) => parseFloat(c.value))
);

// initialize a new parser instance
const parser = new engine({
parser: {
extractDoc: true,
version: `${latestSupportedPhpVersion}`,
},
ast: {
withPositions: true,
Expand Down
5 changes: 1 addition & 4 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
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,
};
Loading