Skip to content

Commit fb89090

Browse files
committed
Improve
1 parent d802058 commit fb89090

3 files changed

Lines changed: 39 additions & 26 deletions

File tree

src/language-js/comments/handle-comments.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,17 @@ function handleIfStatementComments({
198198
return true;
199199
}
200200

201+
// if comment is positioned between the condition and its body
202+
if (
203+
followingNode.type === "BlockStatement" &&
204+
followingNode === enclosingNode.consequent &&
205+
locStart(comment) >= locEnd(precedingNode) &&
206+
locEnd(comment) <= locStart(followingNode)
207+
) {
208+
addLeadingComment(followingNode, comment);
209+
return true;
210+
}
211+
201212
// Comments before `else`:
202213
// - treat as trailing comments of the consequent, if it's a BlockStatement
203214
// - treat as a dangling comment otherwise
@@ -209,6 +220,17 @@ function handleIfStatementComments({
209220
text,
210221
locEnd(enclosingNode.consequent),
211222
);
223+
224+
// if comment is positioned between the `else` token and its body
225+
if (
226+
followingNode.type === "BlockStatement" &&
227+
locStart(comment) >= maybeElseTokenIndex &&
228+
locEnd(comment) <= locStart(followingNode)
229+
) {
230+
addLeadingComment(followingNode, comment);
231+
return true;
232+
}
233+
212234
// With the above conditions alone, this code would also match. This is a false positive.
213235
// So, ignore cases where the token "else" appears immediately after the consequent:
214236
//
@@ -241,16 +263,6 @@ function handleIfStatementComments({
241263
}
242264
}
243265

244-
// if comment is positioned between the condition or else and its body
245-
if (
246-
followingNode.type === "BlockStatement" &&
247-
locStart(comment) >= locEnd(precedingNode) &&
248-
locEnd(comment) <= locStart(followingNode)
249-
) {
250-
addLeadingComment(followingNode, comment);
251-
return true;
252-
}
253-
254266
if (followingNode.type === "BlockStatement") {
255267
addBlockStatementFirstComment(followingNode, comment);
256268
return true;

tests/format/js/comments/__snapshots__/format.test.js.snap

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2672,8 +2672,8 @@ if (6) // comment
26722672
} else if (7)
26732673
// comment
26742674
true
2675-
// comment
2676-
else {
2675+
else // comment
2676+
{
26772677
true
26782678
}
26792679
@@ -2685,9 +2685,9 @@ if (8) // comment
26852685
// comment
26862686
// comment
26872687
true
2688+
else // comment
26882689
// comment
2689-
// comment
2690-
else {
2690+
{
26912691
true
26922692
}
26932693
@@ -2701,7 +2701,8 @@ else if (12)
27012701
else if (13)
27022702
/* comment */ /* comment */ // comment
27032703
true
2704-
/* comment */ else {
2704+
else /* comment */
2705+
{
27052706
true
27062707
}
27072708
@@ -2818,8 +2819,8 @@ if (6) // comment
28182819
} else if (7)
28192820
// comment
28202821
true;
2821-
// comment
2822-
else {
2822+
else // comment
2823+
{
28232824
true;
28242825
}
28252826
@@ -2831,9 +2832,9 @@ if (8) // comment
28312832
// comment
28322833
// comment
28332834
true;
2835+
else // comment
28342836
// comment
2835-
// comment
2836-
else {
2837+
{
28372838
true;
28382839
}
28392840
@@ -2847,7 +2848,8 @@ else if (12)
28472848
else if (13)
28482849
/* comment */ /* comment */ // comment
28492850
true;
2850-
/* comment */ else {
2851+
else /* comment */
2852+
{
28512853
true;
28522854
}
28532855

tests/format/js/if/__snapshots__/format.test.js.snap

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,8 @@ if (2) // foo may not exist
9494
doThing(foo);
9595
}
9696
if (2) {
97-
}
98-
// foo may not exist
99-
else {
97+
} else // foo may not exist
98+
{
10099
doThing(foo);
101100
}
102101
@@ -105,16 +104,16 @@ if (3) // foo may not exist
105104
doThing(foo);
106105
}
107106
if (3) {
108-
} // foo may not exist
109-
else {
107+
} else // foo may not exist
108+
{
110109
doThing(foo);
111110
}
112111
113112
if (4) /* foo may not exist */ {
114113
doThing(foo);
115114
}
116115
if (4) {
117-
} /* foo may not exist */ else {
116+
} else /* foo may not exist */ {
118117
doThing(foo);
119118
}
120119

0 commit comments

Comments
 (0)