Skip to content

Commit c4e12df

Browse files
committed
fix(formatter): correct checking comments around the return argument (#14585)
1 parent 454303b commit c4e12df

File tree

3 files changed

+49
-17
lines changed

3 files changed

+49
-17
lines changed

crates/oxc_formatter/src/write/return_or_throw_statement.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -123,39 +123,39 @@ fn has_argument_leading_comments(argument: &AstNode<Expression>, f: &Formatter<'
123123
let start = left_side.span().start;
124124
let comments = f.comments().comments_before(start);
125125

126-
let is_line_comment_or_multi_line_comment = |comments: &[Comment]| {
126+
if f.comments().comments_before_iter(start).any(|comment| {
127+
source_text.contains_newline(comment.span)
128+
|| source_text.is_end_of_line_comment(comment)
129+
}) {
130+
return true;
131+
}
132+
133+
let is_own_line_comment_or_multi_line_comment = |comments: &[Comment]| {
127134
comments.iter().any(|comment| {
128-
comment.is_line()
135+
source_text.is_own_line_comment(comment)
129136
|| source_text.contains_newline(comment.span)
130-
|| source_text.is_end_of_line_comment(comment)
131137
})
132138
};
133139

134-
if is_line_comment_or_multi_line_comment(comments) {
135-
return true;
136-
}
137-
138140
// This check is based on
139141
// <https://github.com/prettier/prettier/blob/7584432401a47a26943dd7a9ca9a8e032ead7285/src/language-js/comments/handle-comments.js#L335-L349>
140142
if let ExpressionLeftSide::Expression(left_side) = left_side {
141143
let has_leading_own_line_comment = match left_side.as_ref() {
142144
Expression::ChainExpression(chain) => {
143145
if let ChainElement::StaticMemberExpression(member) = &chain.expression {
144-
is_line_comment_or_multi_line_comment(
145-
f.comments().comments_in_range(
146-
member.object.span().end,
147-
member.property.span.end,
148-
),
149-
)
146+
let comments = f
147+
.comments()
148+
.comments_in_range(member.object.span().end, member.property.span.end);
149+
is_own_line_comment_or_multi_line_comment(comments)
150150
} else {
151151
false
152152
}
153153
}
154154
Expression::StaticMemberExpression(member) => {
155-
is_line_comment_or_multi_line_comment(
156-
f.comments()
157-
.comments_in_range(member.object.span().end, member.property.span.end),
158-
)
155+
let comments = f
156+
.comments()
157+
.comments_in_range(member.object.span().end, member.property.span.end);
158+
is_own_line_comment_or_multi_line_comment(comments)
159159
}
160160
_ => false,
161161
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function escapePathForGlob(path) {
2+
return fastGlob
3+
.escapePath(
4+
path.replaceAll("\\", "\0"), // Workaround for fast-glob#262 (part 1)
5+
)
6+
.replaceAll(String.raw`\!`, "@(!)") // Workaround for fast-glob#261
7+
.replaceAll("\0", String.raw`@(\\)`); // Workaround for fast-glob#262 (part 2)
8+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
function escapePathForGlob(path) {
6+
return fastGlob
7+
.escapePath(
8+
path.replaceAll("\\", "\0"), // Workaround for fast-glob#262 (part 1)
9+
)
10+
.replaceAll(String.raw`\!`, "@(!)") // Workaround for fast-glob#261
11+
.replaceAll("\0", String.raw`@(\\)`); // Workaround for fast-glob#262 (part 2)
12+
}
13+
14+
==================== Output ====================
15+
function escapePathForGlob(path) {
16+
return fastGlob
17+
.escapePath(
18+
path.replaceAll("\\", "\0"), // Workaround for fast-glob#262 (part 1)
19+
)
20+
.replaceAll(String.raw`\!`, "@(!)") // Workaround for fast-glob#261
21+
.replaceAll("\0", String.raw`@(\\)`); // Workaround for fast-glob#262 (part 2)
22+
}
23+
24+
===================== End =====================

0 commit comments

Comments
 (0)