Skip to content

Commit 049a7d8

Browse files
authored
fix: comment position in anonymous class (#1580)
Fixes #1546
1 parent eeff25e commit 049a7d8

4 files changed

Lines changed: 23 additions & 0 deletions

File tree

src/comments.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,16 @@ function handleClassComments(enclosingNode, followingNode, comment) {
497497
return true;
498498
}
499499
}
500+
501+
if (
502+
followingNode &&
503+
followingNode.kind === "class" &&
504+
followingNode.isAnonymous &&
505+
followingNode.leadingComments &&
506+
comment.kind === "commentblock"
507+
) {
508+
return true;
509+
}
500510
return false;
501511
}
502512

src/printer.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,6 +2143,13 @@ function printNode(path, options, print) {
21432143

21442144
if (isAnonymousClassNode) {
21452145
parts.push(
2146+
node.what.leadingComments &&
2147+
node.what.leadingComments[0].kind === "commentblock"
2148+
? concat([
2149+
comments.printComments(node.what.leadingComments, options),
2150+
" ",
2151+
])
2152+
: "",
21462153
"class",
21472154
node.arguments.length > 0
21482155
? concat([" ", printArgumentsList(path, options, print)])

tests/comments/__snapshots__/jsfmt.spec.js.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,8 @@ $var = new class {
859859
// Comment
860860
};
861861
862+
$foo = /*a*/ new /* b */ class {};
863+
862864
=====================================output=====================================
863865
<?php
864866
namespace Test\\test\\test;
@@ -1001,6 +1003,8 @@ $var = new class {
10011003
// Comment
10021004
};
10031005
1006+
$foo = /*a*/ new /* b */ class {};
1007+
10041008
================================================================================
10051009
`;
10061010

tests/comments/class.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,5 @@ class MyClass
134134
$var = new class {
135135
// Comment
136136
};
137+
138+
$foo = /*a*/ new /* b */ class {};

0 commit comments

Comments
 (0)