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
20 changes: 19 additions & 1 deletion src/printer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2102,11 +2102,29 @@ function printNode(path, options, print) {
);
} else {
const isExpression = ["call", "offsetlookup"].includes(node.what.kind);

// Skip parens for staticlookup in member chain to preserve original syntax
const isInMemberChain =
path.parent &&
(isLookupNode(path.parent) || path.parent.kind === "call");
const hasOriginalParens =
options.originalText.charAt(
getNextNonSpaceNonCommentCharacterIndex(
options.originalText,
locEnd(node.what)
)
) === "(";
const shouldSkipParens =
isInMemberChain &&
node.arguments.length === 0 &&
!hasOriginalParens &&
node.what.kind === "staticlookup";

const printed = [
isExpression ? "(" : "",
print("what"),
isExpression ? ")" : "",
printArgumentsList(path, options, print),
shouldSkipParens ? "" : printArgumentsList(path, options, print),
];

parts.push(hasLeadingComment(node.what) ? indent(printed) : printed);
Expand Down
16 changes: 16 additions & 0 deletions tests/parens/__snapshots__/jsfmt.spec.mjs.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5021,6 +5021,10 @@ $var = ((new $a)->b());

$var = (new class {})->foo;

// Issue #2441: static property access should not add parens
$identifier = new Yii::$app->class([]);
$identifier = new Yii::$app()->class([]);

=====================================output=====================================
<?php
new Translator(
Expand Down Expand Up @@ -5075,10 +5079,14 @@ $var = new $a->b();
$var = (new $a())->b();
$var = (new $a())->b();
(new class {})->foo;

(new class {})->foo();
(new class {})();
(new class {})["foo"];
$var = (new class {})->foo;
// Issue #2441: static property access should not add parens
$identifier = (new Yii::$app)->class([]);
$identifier = (new Yii::$app)->class([]);


================================================================================
Expand Down Expand Up @@ -5163,6 +5171,10 @@ $var = ((new $a)->b());

$var = (new class {})->foo;

// Issue #2441: static property access should not add parens
$identifier = new Yii::$app->class([]);
$identifier = new Yii::$app()->class([]);

=====================================output=====================================
<?php
new Translator(
Expand Down Expand Up @@ -5217,10 +5229,14 @@ $var = new $a->b();
$var = new $a()->b();
$var = new $a()->b();
new class {}->foo;

new class {}->foo();
new class {}();
new class {}["foo"];
$var = new class {}->foo;
// Issue #2441: static property access should not add parens
$identifier = new Yii::$app->class([]);
$identifier = new Yii::$app->class([]);


================================================================================
Expand Down
4 changes: 4 additions & 0 deletions tests/parens/new.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,7 @@ public function log($msg)
(new class {})['foo'];

$var = (new class {})->foo;

// Issue #2441: static property access should not add parens
$identifier = new Yii::$app->class([]);
$identifier = new Yii::$app()->class([]);