diff --git a/CHANGELOG.md b/CHANGELOG.md index 18121f1b60e..3d5cf63d5ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,8 @@ * [#7842](https://github.com/rubocop-hq/rubocop/issues/7842): Fix a false positive for `Lint/RaiseException` when Exception without cbase specified under the namespace `Gem` by adding `AllowedImplicitNamespaces` option. ([@koic][]) * `Style/IfUnlessModifier` does not infinite-loop when autocorrecting long lines which use if/unless modifiers and have multiple statements separated by semicolons. ([@alexdowad][]) * [rubocop-hq/rubocop-rails#127](https://github.com/rubocop-hq/rubocop-rails/issues/127): Use `ConfigLoader.default_configuration` for the default config. ([@hanachin][]) +* [#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][]) ### Changes @@ -4479,3 +4481,4 @@ [@saurabhmaurya15]: https://github.com/saurabhmaurya15 [@DracoAter]: https://github.com/DracoAter [@diogoosorio]: https://github.com/diogoosorio +[@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..214c6c2c542 100644 --- a/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +++ b/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb @@ -42,8 +42,30 @@ 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 ) + + corrector.remove(range) + range.source 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 6de159d7e53..f9c744ba130 100644 --- a/spec/rubocop/cop/layout/multiline_array_brace_layout_spec.rb +++ b/spec/rubocop/cop/layout/multiline_array_brace_layout_spec.rb @@ -36,4 +36,26 @@ let(:open) { '[' } let(:close) { ']' } end + + context 'when comment present before closing brace' do + it 'corrects closing brace without crashing' do + expect_offense(<<~RUBY) + { + first_key: ['value a', # comment 1 + 'value 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. + second_key: 'value c' + } + RUBY + + expect_correction(<<~RUBY) + { + first_key: ['value a', # comment 1 + 'value b'], # comment 2 + second_key: 'value c' + } + 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 574c44562f9..ea02fb08a0b 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 @@ -41,6 +41,22 @@ let(:close) { ')' } end + context 'when comment present before closing brace' do + it 'corrects closing brace without crashing' do + expect_offense(<<~RUBY) + super(foo(bar, + 'hash' => { 'key' => 'value' } # 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(foo(bar, + 'hash' => { 'key' => 'value' })) # comment + RUBY + end + end + context 'when EnforcedStyle is new_line' do let(:cop_config) { { 'EnforcedStyle' => 'new_line' } }