Skip to content

Commit 93a8532

Browse files
committed
Only break for a comment that can be indented
1 parent fe1ebc9 commit 93a8532

2 files changed

Lines changed: 20 additions & 14 deletions

File tree

‎src/language-js/print/binary-cast-expression.js‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { group, hardline, indent, softline } from "../../document/index.js";
2-
import { hasLeadingOwnLineComment } from "../utilities/has-leading-own-line-comment.js";
2+
import hasNewline from "../../utilities/has-newline.js";
3+
import { locEnd } from "../location/index.js";
4+
import { isLineComment } from "../utilities/comment-types.js";
5+
import { CommentCheckFlags, hasComment } from "../utilities/comments.js";
6+
import { isIndentableBlockComment } from "../utilities/indentable-block-comment.js";
37
import {
48
isCallOrNewExpression,
59
isMemberExpression,
@@ -16,7 +20,13 @@ function printBinaryCastExpression(path, options, print) {
1620

1721
const keepTypeOnOwnLine =
1822
!isUnionType(node.typeAnnotation) &&
19-
hasLeadingOwnLineComment(options.originalText, node.typeAnnotation);
23+
hasComment(
24+
node.typeAnnotation,
25+
CommentCheckFlags.Leading,
26+
(comment) =>
27+
hasNewline(options.originalText, locEnd(comment)) &&
28+
(isLineComment(comment) || isIndentableBlockComment(comment)),
29+
);
2030

2131
const parts = [
2232
print("expression"),

‎tests/format/typescript/as/comments/__snapshots__/format.test.js.snap‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,31 +128,27 @@ comment
128128
1 as const /*
129129
comment
130130
*/;
131-
1 as
132-
/*
131+
1 as /*
133132
comment
134133
*/
135-
Foo;
136-
1 satisfies
137-
/*
134+
Foo;
135+
1 satisfies /*
138136
comment
139137
*/
140-
Foo;
138+
Foo;
141139
142140
1 as const;
143141
/*
144142
comment
145143
*/
146-
1 as
147-
/*
144+
1 as /*
148145
comment
149146
*/
150-
Foo;
151-
1 satisfies
152-
/*
147+
Foo;
148+
1 satisfies /*
153149
comment
154150
*/
155-
Foo;
151+
Foo;
156152
157153
1 as const; // comment
158154
1 as Foo; // comment

0 commit comments

Comments
 (0)