Skip to content

Commit

Permalink
Add support for multiline fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Jaime committed Jul 28, 2021
1 parent ed17552 commit f7b5df5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
5 changes: 2 additions & 3 deletions lib/rubocop/cop/style/hash_as_last_array_item.rb
@@ -1,5 +1,4 @@
# frozen_string_literal: true
require 'byebug'

module RuboCop
module Cop
Expand Down Expand Up @@ -76,8 +75,8 @@ def check_no_braces(node)
return if node.children.empty? # Empty hash cannot be "unbraced"

add_offense(node, message: 'Omit the braces around the hash.') do |corrector|
remove_last_element_trailing_comma(corrector, node.parent)
corrector.remove(node.loc.begin)
remove_last_element_trailing_comma(corrector, node)
corrector.remove(node.loc.end)
end
end
Expand All @@ -91,7 +90,7 @@ def remove_last_element_trailing_comma(corrector, node)
range: node.children.last.source_range,
side: :right
).end.resize(1)

corrector.remove(range) if range.source == ','
end
end
Expand Down
21 changes: 18 additions & 3 deletions spec/rubocop/cop/style/hash_as_last_array_item_spec.rb
Expand Up @@ -61,7 +61,7 @@
RUBY

expect_correction(<<~RUBY)
[1, 2, one: 1, two: 2 ,]
[1, 2, one: 1, two: 2, ]
RUBY
end

Expand All @@ -82,8 +82,23 @@
[
1,
2,
one: 1,
two: 2,
#{' '}
one: 1,
two: 2,
#{' '}
]
RUBY
end

it 'does not register an offense when hash is not the last element' do
expect_no_offenses(<<~RUBY)
[
1,
2,
{
one: 1
},
two: 2
]
RUBY
end
Expand Down

0 comments on commit f7b5df5

Please sign in to comment.