diff --git a/fixtures/small/rescue_inline_comment_actual.rb b/fixtures/small/rescue_inline_comment_actual.rb new file mode 100644 index 00000000..6b8e73f9 --- /dev/null +++ b/fixtures/small/rescue_inline_comment_actual.rb @@ -0,0 +1,19 @@ +begin +rescue Foo # comment +end + +begin +rescue Foo, + Bar # comment +end + +begin +rescue \ + Foo # comment +end + +begin +rescue \ + Foo, + Bar # comment +end diff --git a/fixtures/small/rescue_inline_comment_expected.rb b/fixtures/small/rescue_inline_comment_expected.rb new file mode 100644 index 00000000..a92cd67a --- /dev/null +++ b/fixtures/small/rescue_inline_comment_expected.rb @@ -0,0 +1,21 @@ +begin + # comment +rescue Foo +end + +begin +rescue Foo, + # comment + Bar +end + +begin + # comment +rescue Foo +end + +begin +rescue Foo, + # comment + Bar +end diff --git a/librubyfmt/src/format_prism.rs b/librubyfmt/src/format_prism.rs index d27e4bf6..d6807c55 100644 --- a/librubyfmt/src/format_prism.rs +++ b/librubyfmt/src/format_prism.rs @@ -4728,6 +4728,8 @@ fn format_rescue_node<'src>(ps: &mut ParserState<'src>, rescue_node: prism::Resc let exceptions = rescue_node.exceptions(); let reference = rescue_node.reference(); if !exceptions.is_empty() { + // Preemptively extract inline comments so they land before rescue, not inside the breakable. + ps.at_offset(exceptions.first().unwrap().location().start_offset()); ps.with_start_of_line(false, |ps| { ps.inline_breakable_of(BreakableDelims::for_binary_op(), |ps| { let exceptions_count = exceptions.len();