Skip to content
Open
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
23 changes: 22 additions & 1 deletion src/printer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1622,6 +1622,27 @@ function printAssignmentRight(
return [" ", ref, printedRight];
}

// Without parentheses, `new` only accepts a class name or a variable,
// possibly with property and static property lookups. Anything else, like
// `new ($a ?? $b)()`, must keep them or it changes meaning or stops parsing.
function isNewClassReference(node) {
switch (node.kind) {
case "name":
case "variable":
case "selfreference":
case "staticreference":
case "parentreference":
return true;
case "propertylookup":
case "nullsafepropertylookup":
return isNewClassReference(node.what);
case "staticlookup":
return node.offset.kind === "variable" && isNewClassReference(node.what);
default:
return false;
}
}

function needsHardlineAfterDanglingComment(node) {
if (!node.comments) {
return false;
Expand Down Expand Up @@ -2223,7 +2244,7 @@ function printNode(path, options, print) {
group(print("what"))
);
} else {
const isExpression = ["call", "offsetlookup"].includes(node.what.kind);
const isExpression = !isNewClassReference(node.what);
const printed = [
isExpression ? "(" : "",
print("what"),
Expand Down
77 changes: 77 additions & 0 deletions tests/new/__snapshots__/jsfmt.spec.mjs.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,82 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing

exports[`class-expression.php 1`] = `
====================================options=====================================
parsers: ["php"]
phpVersion: "8.3"
printWidth: 80
| printWidth
=====================================input======================================
<?php
$a = new ($x ?? $y)($z);
$b = new ($x . "Suffix")();
$c = new (Foo::BAR)();
$d = new ("Foo")();
$e = new ((string) $x)();
$f = new ($cond ? A::class : B::class)();
$g = new ($a->factory()->class)();
$h = new (static::$classes[$k] ?? Fallback::class)($z);
$i = new ($a->b)();
$j = new (static::$class)();
$k = new ($classes[$k])();
$l = new ($this->factory())();

=====================================output=====================================
<?php
$a = new ($x ?? $y)($z);
$b = new ($x . "Suffix")();
$c = new (Foo::BAR)();
$d = new ("Foo")();
$e = new ((string) $x)();
$f = new ($cond ? A::class : B::class)();
$g = new ($a->factory()->class)();
$h = new (static::$classes[$k] ?? Fallback::class)($z);
$i = new $a->b();
$j = new static::$class();
$k = new ($classes[$k])();
$l = new ($this->factory())();

================================================================================
`;

exports[`class-expression.php 2`] = `
====================================options=====================================
parsers: ["php"]
printWidth: 80
| printWidth
=====================================input======================================
<?php
$a = new ($x ?? $y)($z);
$b = new ($x . "Suffix")();
$c = new (Foo::BAR)();
$d = new ("Foo")();
$e = new ((string) $x)();
$f = new ($cond ? A::class : B::class)();
$g = new ($a->factory()->class)();
$h = new (static::$classes[$k] ?? Fallback::class)($z);
$i = new ($a->b)();
$j = new (static::$class)();
$k = new ($classes[$k])();
$l = new ($this->factory())();

=====================================output=====================================
<?php
$a = new ($x ?? $y)($z);
$b = new ($x . "Suffix")();
$c = new (Foo::BAR)();
$d = new ("Foo")();
$e = new ((string) $x)();
$f = new ($cond ? A::class : B::class)();
$g = new ($a->factory()->class)();
$h = new (static::$classes[$k] ?? Fallback::class)($z);
$i = new $a->b();
$j = new static::$class();
$k = new ($classes[$k])();
$l = new ($this->factory())();

================================================================================
`;

exports[`new.php 1`] = `
====================================options=====================================
parsers: ["php"]
Expand Down
13 changes: 13 additions & 0 deletions tests/new/class-expression.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
$a = new ($x ?? $y)($z);
$b = new ($x . "Suffix")();
$c = new (Foo::BAR)();
$d = new ("Foo")();
$e = new ((string) $x)();
$f = new ($cond ? A::class : B::class)();
$g = new ($a->factory()->class)();
$h = new (static::$classes[$k] ?? Fallback::class)($z);
$i = new ($a->b)();
$j = new (static::$class)();
$k = new ($classes[$k])();
$l = new ($this->factory())();