Skip to content

Commit

Permalink
Fix Style/HashSyntax: Omit the hash value cases
Browse files Browse the repository at this point in the history
There are certain cases that suggestion of Style/HashSyntax: Omit the hash value and then the autocorrect will produce invalid syntax

This change acts conservative and does not change/suggest a code change for those cases.
Related with #10289

Example:

RSpec.shared_examples 'Examples' do |option_a:|
  include_examples 'Example A', option_a: option_a if option_a # Omitting the option_a value would produce invalid syntax

  include_examples 'Example B', option_a: option_a # Omitting the option_a value would produce invalid syntax

  describe '.describe' do
    it { true }
  end

  include_examples 'Example C', option_a: option_a # Valid omission
end
  • Loading branch information
berkos committed Jan 15, 2022
1 parent 3591a52 commit 017c0b6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#10357](https://github.com/rubocop/rubocop/pull/10357): Fix a false positive for `Style/HashSyntax` when omitting the value. ([@berkos][])
18 changes: 15 additions & 3 deletions lib/rubocop/cop/mixin/hash_shorthand_syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,22 @@ def require_hash_value?(hash_key_source, node)

def without_parentheses_call_expr_follows?(node)
return false unless (ancestor = node.parent.parent)
return false unless (right_sibling = ancestor.right_sibling)

ancestor.respond_to?(:parenthesized?) && !ancestor.parenthesized? &&
right_sibling.respond_to?(:parenthesized?) && !right_sibling.parenthesized?
right_sibling = ancestor.right_sibling

return true if right_sibling.nil? && without_parentheses?(ancestor)
return false unless right_sibling
return true if node_with_block_and_arguments?(right_sibling)

without_parentheses?(ancestor) && without_parentheses?(right_sibling)
end

def without_parentheses?(node)
node.respond_to?(:parenthesized?) && !node.parenthesized?
end

def node_with_block_and_arguments?(node)
node.respond_to?(:block_type?) && node.block_type? && node.children&.first&.arguments?
end
end
end
Expand Down
36 changes: 34 additions & 2 deletions spec/rubocop/cop/style/hash_syntax_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@
RUBY
end

it 'does not register an offense when hash valuees are omitted' do
it 'does not register an offense when hash values are omitted' do
expect_no_offenses(<<~RUBY)
{foo:, bar:}
RUBY
Expand Down Expand Up @@ -891,7 +891,39 @@
RUBY
end

it 'registers an offense when with parentheses call expr follows' do
it 'does not register an offense when one line condition follows' do
expect_no_offenses(<<~RUBY)
foo value: value if bar
RUBY
end

it 'does not register an offense when call expr with argument and a block follows' do
expect_no_offenses(<<~RUBY)
foo value: value
foo arg do
value
end
RUBY
end

it 'registers and corrects an offense when call expr without arguments and with a block follows' do
expect_offense(<<~RUBY)
foo value: value
^^^^^ Omit the hash value.
bar do
value
end
RUBY

expect_correction(<<~RUBY)
foo value:
bar do
value
end
RUBY
end

it 'registers and corrects an offense when with parentheses call expr follows' do
expect_offense(<<~RUBY)
foo value: value
^^^^^ Omit the hash value.
Expand Down

0 comments on commit 017c0b6

Please sign in to comment.