Skip to content

Commit

Permalink
[Fix rubocop#7972] Fix an incorrect autocrrect for Style/HashSyntax
Browse files Browse the repository at this point in the history
Fixes rubocop#7972.

Fix an incorrect autocorrect for `Style/HashSyntax` when using
a return value uses `return`.
  • Loading branch information
koic committed May 15, 2020
1 parent 0052dae commit 4b93dd1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -12,6 +12,7 @@
* [#7953](https://github.com/rubocop-hq/rubocop/issues/7953): Fix an error for `Lint/AmbiguousOperator` when a method with no arguments is used in advance. ([@koic][])
* [#7962](https://github.com/rubocop-hq/rubocop/issues/7962): Fix a false positive for `Lint/ParenthesesAsGroupedExpression` when heredoc has a space between the same string as the method name and `(`. ([@koic][])
* [#7967](https://github.com/rubocop-hq/rubocop/pull/7967): `Style/SlicingWithRange` cop now supports any expression as its first index. ([@zverok][])
* [#7972](https://github.com/rubocop-hq/rubocop/issues/7972): Fix an incorrect autocrrect for `Style/HashSyntax` when using a return value uses `return`. ([@koic][])

### Changes

Expand Down
17 changes: 12 additions & 5 deletions lib/rubocop/cop/style/hash_syntax.rb
Expand Up @@ -168,18 +168,25 @@ def check(pairs, delim, msg)
end

def autocorrect_ruby19(corrector, pair_node)
key = pair_node.key.source_range
op = pair_node.loc.operator

range = key.join(op)
range = range_with_surrounding_space(range: range, side: :right)
range = range_for_autocorrect_ruby19(pair_node)

space = argument_without_space?(pair_node.parent) ? ' ' : ''

corrector.replace(
range,
range.source.sub(/^:(.*\S)\s*=>\s*$/, space.to_s + '\1: ')
)

hash_node = pair_node.parent
corrector.wrap(hash_node, '{', '}') if hash_node.parent&.return_type? && !hash_node.braces?
end

def range_for_autocorrect_ruby19(pair_node)
key = pair_node.key.source_range
operator = pair_node.loc.operator

range = key.join(operator)
range_with_surrounding_space(range: range, side: :right)
end

def argument_without_space?(node)
Expand Down
24 changes: 24 additions & 0 deletions spec/rubocop/cop/style/hash_syntax_spec.rb
Expand Up @@ -139,6 +139,30 @@
new_source = autocorrect_source('foo:bar => 1')
expect(new_source).to eq('foo bar: 1')
end

context 'when using a return value uses `return`' do
it 'registers an offense and corrects when not enclosed in parentheses' do
expect_offense(<<~RUBY)
return :key => value
^^^^^^^ Use the new Ruby 1.9 hash syntax.
RUBY

expect_correction(<<~RUBY)
return {key: value}
RUBY
end

it 'registers an offense and corrects when enclosed in parentheses' do
expect_offense(<<~RUBY)
return {:key => value}
^^^^^^^ Use the new Ruby 1.9 hash syntax.
RUBY

expect_correction(<<~RUBY)
return {key: value}
RUBY
end
end
end

context 'with SpaceAroundOperators disabled' do
Expand Down

0 comments on commit 4b93dd1

Please sign in to comment.