Skip to content

Commit

Permalink
Fix-7777-7776-Generate-valid-code-if-comment-exists-before-closing-brace
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhar-patil committed Apr 24, 2020
1 parent db23f5e commit de65e13
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down Expand Up @@ -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
22 changes: 22 additions & 0 deletions lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb
Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions spec/rubocop/cop/layout/multiline_array_brace_layout_spec.rb
Expand Up @@ -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
16 changes: 16 additions & 0 deletions spec/rubocop/cop/layout/multiline_method_call_brace_layout_spec.rb
Expand Up @@ -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' } }

Expand Down

0 comments on commit de65e13

Please sign in to comment.