From fa3dcaf3786232906cf58ad2849b45bb271c87cb Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 15 May 2020 13:00:56 +0900 Subject: [PATCH] [Fix #7972] Fix an incorrect autocorrect for `Style/HashSyntax` (#7973) Fixes #7972. Fix an incorrect autocorrect for `Style/HashSyntax` when using a return value uses `return`. --- CHANGELOG.md | 1 + lib/rubocop/cop/style/hash_syntax.rb | 17 ++++++++++----- spec/rubocop/cop/style/hash_syntax_spec.rb | 24 ++++++++++++++++++++++ 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 392a1a9c1cb..01a4e287fde 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rubocop/cop/style/hash_syntax.rb b/lib/rubocop/cop/style/hash_syntax.rb index b994cb0d6db..1be7d09524a 100644 --- a/lib/rubocop/cop/style/hash_syntax.rb +++ b/lib/rubocop/cop/style/hash_syntax.rb @@ -168,11 +168,7 @@ 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) ? ' ' : '' @@ -180,6 +176,17 @@ def autocorrect_ruby19(corrector, pair_node) 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) diff --git a/spec/rubocop/cop/style/hash_syntax_spec.rb b/spec/rubocop/cop/style/hash_syntax_spec.rb index cf80e4b25cd..56d781da2fa 100644 --- a/spec/rubocop/cop/style/hash_syntax_spec.rb +++ b/spec/rubocop/cop/style/hash_syntax_spec.rb @@ -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