diff --git a/src/printer.mjs b/src/printer.mjs index c3cf11cd0..0ecad0e63 100644 --- a/src/printer.mjs +++ b/src/printer.mjs @@ -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); diff --git a/tests/parens/__snapshots__/jsfmt.spec.mjs.snap b/tests/parens/__snapshots__/jsfmt.spec.mjs.snap index ccb74dd00..ace43eea2 100644 --- a/tests/parens/__snapshots__/jsfmt.spec.mjs.snap +++ b/tests/parens/__snapshots__/jsfmt.spec.mjs.snap @@ -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===================================== 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([]); ================================================================================ @@ -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===================================== 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([]); ================================================================================ diff --git a/tests/parens/new.php b/tests/parens/new.php index 290cfe91c..a477bab9d 100644 --- a/tests/parens/new.php +++ b/tests/parens/new.php @@ -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([]);