Skip to content

Commit 0343d2f

Browse files
committed
fix: avoid adding parens to static property in new expression
1 parent 0c883a4 commit 0343d2f

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/printer.mjs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2102,11 +2102,28 @@ function printNode(path, options, print) {
21022102
);
21032103
} else {
21042104
const isExpression = ["call", "offsetlookup"].includes(node.what.kind);
2105+
2106+
const isInMemberChain =
2107+
path.parent &&
2108+
(isLookupNode(path.parent) || path.parent.kind === "call");
2109+
const whatEndOffset = locEnd(node.what);
2110+
const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
2111+
options.originalText,
2112+
whatEndOffset
2113+
);
2114+
const hasOriginalParens =
2115+
options.originalText.charAt(nextCharIndex) === "(";
2116+
const shouldSkipParens =
2117+
isInMemberChain &&
2118+
node.arguments.length === 0 &&
2119+
!hasOriginalParens &&
2120+
node.what.kind === "staticlookup";
2121+
21052122
const printed = [
21062123
isExpression ? "(" : "",
21072124
print("what"),
21082125
isExpression ? ")" : "",
2109-
printArgumentsList(path, options, print),
2126+
shouldSkipParens ? "" : printArgumentsList(path, options, print),
21102127
];
21112128

21122129
parts.push(hasLeadingComment(node.what) ? indent(printed) : printed);

tests/parens/__snapshots__/jsfmt.spec.mjs.snap

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5021,6 +5021,10 @@ $var = ((new $a)->b());
50215021
50225022
$var = (new class {})->foo;
50235023
5024+
// Issue #2441: static property access should not add parens
5025+
$identifier = new Yii::$app->class([]);
5026+
$identifier = new Yii::$app()->class([]);
5027+
50245028
=====================================output=====================================
50255029
<?php
50265030
new Translator(
@@ -5075,10 +5079,14 @@ $var = new $a->b();
50755079
$var = (new $a())->b();
50765080
$var = (new $a())->b();
50775081
(new class {})->foo;
5082+
50785083
(new class {})->foo();
50795084
(new class {})();
50805085
(new class {})["foo"];
50815086
$var = (new class {})->foo;
5087+
// Issue #2441: static property access should not add parens
5088+
$identifier = (new Yii::$app)->class([]);
5089+
$identifier = (new Yii::$app)->class([]);
50825090
50835091
50845092
================================================================================
@@ -5163,6 +5171,10 @@ $var = ((new $a)->b());
51635171
51645172
$var = (new class {})->foo;
51655173
5174+
// Issue #2441: static property access should not add parens
5175+
$identifier = new Yii::$app->class([]);
5176+
$identifier = new Yii::$app()->class([]);
5177+
51665178
=====================================output=====================================
51675179
<?php
51685180
new Translator(
@@ -5217,10 +5229,14 @@ $var = new $a->b();
52175229
$var = new $a()->b();
52185230
$var = new $a()->b();
52195231
new class {}->foo;
5232+
52205233
new class {}->foo();
52215234
new class {}();
52225235
new class {}["foo"];
52235236
$var = new class {}->foo;
5237+
// Issue #2441: static property access should not add parens
5238+
$identifier = new Yii::$app->class([]);
5239+
$identifier = new Yii::$app->class([]);
52245240
52255241
52265242
================================================================================

tests/parens/new.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,7 @@ public function log($msg)
7070
(new class {})['foo'];
7171

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

0 commit comments

Comments
 (0)