Skip to content

Commit 8741ddb

Browse files
claytonrcartercseufert
authored andcommitted
feat: correctly parse and format 8.4 syntax
1 parent 2391234 commit 8741ddb

File tree

5 files changed

+45
-8
lines changed

5 files changed

+45
-8
lines changed

src/parser.mjs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ function parse(text, opts) {
1010
// Todo https://github.com/glayzzle/php-parser/issues/170
1111
text = text.replace(/\?>\n<\?/g, "?>\n___PSEUDO_INLINE_PLACEHOLDER___<?");
1212

13+
const parserOpts = Object.assign(
14+
{ extractDoc: true },
15+
// only pass the version if user requested 8.4 syntax; parser is stricter
16+
// about allowed syntax than we are and currenly defaults to support for 8.3
17+
opts && opts.phpVersion === "8.4" ? { version: opts.phpVersion } : {}
18+
);
19+
1320
// initialize a new parser instance
1421
const parser = new engine({
15-
parser: {
16-
extractDoc: true,
17-
},
22+
parser: parserOpts,
1823
ast: {
1924
withPositions: true,
2025
withSource: true,

src/printer.mjs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -774,11 +774,12 @@ function printBinaryExpression(
774774
parts.push(print("left"));
775775
}
776776

777-
const shouldInline = shouldInlineLogicalExpression(node);
778-
779-
const right = shouldInline
780-
? [node.type, " ", print("right")]
781-
: [node.type, line, print("right")];
777+
const right =
778+
node.left.kind === "new" && node.type !== "."
779+
? [node.type, print("right")]
780+
: shouldInlineLogicalExpression(node)
781+
? [node.type, " ", print("right")]
782+
: [node.type, line, print("right")];
782783

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

795796
const shouldNotHaveWhitespace =
796797
isDocNode(node.left) ||
798+
(node.left.kind === "new" && node.type !== ".") ||
797799
(node.left.kind === "bin" && isDocNode(node.left.right));
798800

799801
parts.push(
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`new84.php 1`] = `
4+
====================================options=====================================
5+
parsers: ["php"]
6+
phpVersion: "8.4"
7+
printWidth: 80
8+
| printWidth
9+
=====================================input======================================
10+
<?php
11+
12+
new Foo->prop;
13+
new Foo->method();
14+
new Foo->$var;
15+
16+
=====================================output=====================================
17+
<?php
18+
19+
new Foo()->prop;
20+
new Foo()->method();
21+
new Foo()->$var;
22+
23+
================================================================================
24+
`;

tests/new84/jsfmt.spec.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
run_spec(import.meta, ["php"], { phpVersion: "8.4" });

tests/new84/new84.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
new Foo->prop;
4+
new Foo->method();
5+
new Foo->$var;

0 commit comments

Comments
 (0)