diff --git a/CHANGELOG.md b/CHANGELOG.md index df1850f65ea..8cc3271fa96 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,8 @@ * [#8193](https://github.com/rubocop-hq/rubocop/issues/8193): Fix a false positive for `Style/RedundantRegexpCharacterClass` when using `[\b]`. ([@owst][]) * [#8205](https://github.com/rubocop-hq/rubocop/issues/8205): Fix a false positive for `Style/RedundantRegexpCharacterClass` when using a leading escaped `]`. ([@owst][]) * [#8208](https://github.com/rubocop-hq/rubocop/issues/8208): Fix `Style/RedundantParentheses` with hash literal as first argument to `yield`. ([@karlwithak][]) +* [#7777](https://github.com/rubocop-hq/rubocop/issues/7777): Fix crash for `Layout/MultilineArrayBraceLayout` when comment is present after last element. ([@shekhar-patil][]) +* [#7776](https://github.com/rubocop-hq/rubocop/issues/7776): Fix crash for `Layout/MultilineMethodCallBraceLayout` when comment is present before closing braces. ([@shekhar-patil][]) * [#8176](https://github.com/rubocop-hq/rubocop/pull/8176): Don't load `.rubocop.yml` from personal folders to check for exclusions if there's a project configuration. ([@deivid-rodriguez][]) ### Changes @@ -4665,3 +4667,4 @@ [@fatkodima]: https://github.com/fatkodima [@karlwithak]: https://github.com/karlwithak [@CamilleDrapier]: https://github.com/CamilleDrapier +[@shekhar-patil]: https://github.com/shekhar-patil diff --git a/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb b/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb index 35e8fc7b248..7a0d973cfb0 100644 --- a/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +++ b/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb @@ -42,8 +42,34 @@ def correct_next_line_brace(corrector) corrector.insert_before( last_element_range_with_trailing_comma(node).end, + content_if_comment_present(corrector, node) + ) + end + + def content_if_comment_present(corrector, node) + range = range_with_surrounding_space( + range: children(node).last.source_range, + side: :right + ).end.resize(1) + if range.source == '#' + select_content_to_be_inserted_after_last_element(corrector, node) + else node.loc.end.source + end + end + + def select_content_to_be_inserted_after_last_element(corrector, node) + range = range_between( + node.loc.end.begin_pos, + range_by_whole_lines(node.loc.expression).end.end_pos ) + + remove_trailing_content_of_comment(corrector, range) + range.source + end + + def remove_trailing_content_of_comment(corrector, range) + corrector.remove(range) end def last_element_range_with_trailing_comma(node) diff --git a/spec/rubocop/cop/layout/multiline_array_brace_layout_spec.rb b/spec/rubocop/cop/layout/multiline_array_brace_layout_spec.rb index 8715583ba84..9eb0a66aadf 100644 --- a/spec/rubocop/cop/layout/multiline_array_brace_layout_spec.rb +++ b/spec/rubocop/cop/layout/multiline_array_brace_layout_spec.rb @@ -34,4 +34,26 @@ let(:open) { '[' } let(:close) { ']' } end + + context 'when comment present before closing brace' do + it 'corrects closing brace without crashing' do + expect_offense(<<~RUBY) + { + key1: [a, # comment 1 + b # comment 2 + ], + ^ The closing array brace must be on the same line as the last array element when the opening brace is on the same line as the first array element. + key2: 'foo' + } + RUBY + + expect_correction(<<~RUBY) + { + key1: [a, # comment 1 + b], # comment 2 + key2: 'foo' + } + RUBY + end + end end diff --git a/spec/rubocop/cop/layout/multiline_method_call_brace_layout_spec.rb b/spec/rubocop/cop/layout/multiline_method_call_brace_layout_spec.rb index 961ea1be578..27fc260e2e0 100644 --- a/spec/rubocop/cop/layout/multiline_method_call_brace_layout_spec.rb +++ b/spec/rubocop/cop/layout/multiline_method_call_brace_layout_spec.rb @@ -61,4 +61,20 @@ RUBY end end + + context 'when comment present before closing brace' do + it 'corrects closing brace without crashing' do + expect_offense(<<~RUBY) + super(bar(baz, + ham # comment + )) + ^ Closing method call brace must be on the same line as the last argument when opening brace is on the same line as the first argument. + RUBY + + expect_correction(<<~RUBY) + super(bar(baz, + ham)) # comment + RUBY + end + end end