Skip to content

Commit 8254ac0

Browse files
authored
fix: comment after switch case should not end up after label (#461)
1 parent ee4861a commit 8254ac0

File tree

4 files changed

+27
-30
lines changed

4 files changed

+27
-30
lines changed

rust-toolchain.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[toolchain]
2-
channel = "1.61.0"
2+
channel = "1.66.0"
33
components = ["clippy"]

src/generation/generate.rs

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4925,7 +4925,7 @@ fn gen_switch_case<'a>(node: &'a SwitchCase, context: &mut Context<'a>) -> Print
49254925
items.push_str("default:");
49264926
}
49274927

4928-
items.extend(gen_first_line_trailing_comments(&node.range(), node.cons.get(0).map(|x| x.range()), context));
4928+
items.extend(gen_trailing_comments_same_line(&colon_token.range(), context));
49294929
let generated_trailing_comments = gen_trailing_comments_for_case(node, &block_stmt_body, context);
49304930
if !node.cons.is_empty() {
49314931
if let Some(block_stmt_body) = block_stmt_body {
@@ -6454,33 +6454,6 @@ fn gen_js_doc_or_multiline_block(comment: &Comment, _context: &mut Context) -> P
64546454
}
64556455
}
64566456

6457-
fn gen_first_line_trailing_comments<'a>(node: &dyn SourceRanged, first_member: Option<SourceRange>, context: &mut Context<'a>) -> PrintItems {
6458-
let mut items = PrintItems::new();
6459-
let node_start_line = node.start_line_fast(context.program);
6460-
6461-
for comment in get_comments(node, &first_member, context) {
6462-
if comment.start_line_fast(context.program) == node_start_line {
6463-
if let Some(generated_comment) = gen_comment(comment, context) {
6464-
if comment.kind == CommentKind::Line {
6465-
items.push_str(" ");
6466-
}
6467-
items.extend(generated_comment);
6468-
}
6469-
}
6470-
}
6471-
6472-
return items;
6473-
6474-
fn get_comments<'a>(node: &dyn SourceRanged, first_member: &Option<SourceRange>, context: &mut Context<'a>) -> Vec<&'a Comment> {
6475-
let mut comments = Vec::new();
6476-
if let Some(first_member) = first_member {
6477-
comments.extend(first_member.leading_comments_fast(context.program));
6478-
}
6479-
comments.extend(node.trailing_comments_fast(context.program));
6480-
comments
6481-
}
6482-
}
6483-
64846457
fn gen_trailing_comments<'a>(node: &dyn SourceRanged, context: &mut Context<'a>) -> PrintItems {
64856458
let trailing_comments = node.trailing_comments_fast(context.program);
64866459
gen_comments_as_trailing(node, trailing_comments, context)
@@ -7375,7 +7348,7 @@ fn gen_separated_values_with_result<'a>(opts: GenSeparatedValuesParams<'a>, cont
73757348
let leading_comments = separator_token.leading_comments_fast(context.program);
73767349
items.extend(gen_comment_collection(leading_comments, None, Some(&separator_token.range()), context));
73777350
items.extend(generated_separator);
7378-
items.extend(gen_first_line_trailing_comments(&separator_token.range(), None, context));
7351+
items.extend(gen_trailing_comments_same_line(&separator_token.range(), context));
73797352
items
73807353
}
73817354
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![allow(clippy::bool_assert_comparison)]
2+
#![allow(clippy::bool_to_int_with_if)]
23
#![allow(clippy::comparison_chain)]
34
#![allow(clippy::if_same_then_else)]
45
#![allow(clippy::vec_init_then_push)]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
~~ deno: true ~~
2+
== should keep comment on same line ==
3+
const unit = 's';
4+
let seconds = 10;
5+
6+
switch (unit) {
7+
case 'd': seconds *= 24; // fallthrough
8+
case 'h': seconds *= 60; // fallthrough
9+
case 'm': seconds *= 60; // fallthrough
10+
}
11+
12+
[expect]
13+
const unit = "s";
14+
let seconds = 10;
15+
16+
switch (unit) {
17+
case "d":
18+
seconds *= 24; // fallthrough
19+
case "h":
20+
seconds *= 60; // fallthrough
21+
case "m":
22+
seconds *= 60; // fallthrough
23+
}

0 commit comments

Comments
 (0)