Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix-7777-7776]: Generate valid code if comment exists before closing brace #8237

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down Expand Up @@ -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
26 changes: 26 additions & 0 deletions lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb
Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions spec/rubocop/cop/layout/multiline_array_brace_layout_spec.rb
Expand Up @@ -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
16 changes: 16 additions & 0 deletions spec/rubocop/cop/layout/multiline_method_call_brace_layout_spec.rb
Expand Up @@ -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