Skip to content

Commit

Permalink
Merge pull request #6680 from rrosenblum/6648_empty_literal_hash_super
Browse files Browse the repository at this point in the history
[Fix #6648] EmptyLiteral - wrap arguments in parentheses when correcting Hash.new is the fist argument to super
  • Loading branch information
Drenmi committed Jan 22, 2019
2 parents bb62640 + 72a17c2 commit 35251b4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -35,6 +35,7 @@
* [#6633](https://github.com/rubocop-hq/rubocop/issues/6633): Fix `Lint/SafeNavigation` complaining about use of `to_d`. ([@tejasbubane][])
* [#6575](https://github.com/rubocop-hq/rubocop/issues/6575): Fix `Naming/PredicateName` suggesting invalid rename. ([@tejasbubane][])
* [#6673](https://github.com/rubocop-hq/rubocop/issues/6673): Fix `Style/DocumentationMethod` cop to recognize documentation comments for `def` inline with `module_function`. ([@tejasbubane][])
* [#6648](https://github.com/rubocop-hq/rubocop/issues/6648): Fix auto-correction of `Style/EmptyLiteral` when `Hash.new` is passed as the first argument to `super`. ([@rrosenblum][])

### Changes

Expand Down
13 changes: 8 additions & 5 deletions lib/rubocop/cop/style/empty_literal.rb
Expand Up @@ -67,10 +67,13 @@ def string_literals_config
end

def first_argument_unparenthesized?(node)
return false unless node.parent && node.parent.send_type?
parent = node.parent
unless parent && %i[send super zsuper].include?(parent.type)
return false
end

_receiver, _method_name, *args = *node.parent
node.object_id == args.first.object_id && !parentheses?(node.parent)
node.object_id == parent.arguments.first.object_id &&
!parentheses?(node.parent)
end

def replacement_range(node)
Expand All @@ -79,7 +82,7 @@ def replacement_range(node)
# `some_method {}` is not same as `some_method Hash.new`
# because the braces are interpreted as a block. We will have
# to rewrite the arguments to wrap them in parenthesis.
_receiver, _method_name, *args = *node.parent
args = node.parent.arguments

range_between(args[0].loc.expression.begin_pos - 1,
args[-1].loc.expression.end_pos)
Expand Down Expand Up @@ -107,7 +110,7 @@ def correction(node)
# `some_method {}` is not same as `some_method Hash.new`
# because the braces are interpreted as a block. We will have
# to rewrite the arguments to wrap them in parenthesis.
_receiver, _method_name, *args = *node.parent
args = node.parent.arguments
"(#{args[1..-1].map(&:source).unshift('{}').join(', ')})"
else
'{}'
Expand Down
30 changes: 30 additions & 0 deletions spec/rubocop/cop/style/empty_literal_spec.rb
Expand Up @@ -102,6 +102,36 @@
new_source = autocorrect_source(source)
expect(new_source).to eq('yadayada.map { a }.reduce({}, :merge)')
end

it 'auto-correct changes Hash.new to {} and wraps it in parentheses ' \
'when it is the only argument to super' do
new_source = autocorrect_source(<<-RUBY.strip_indent)
def foo
super Hash.new
end
RUBY

expect(new_source).to eq(<<-RUBY.strip_indent)
def foo
super({})
end
RUBY
end

it 'auto-correct changes Hash.new to {} and wraps all arguments in ' \
'parentheses when it is the first argument to super' do
new_source = autocorrect_source(<<-RUBY.strip_indent)
def foo
super Hash.new, something
end
RUBY

expect(new_source).to eq(<<-RUBY.strip_indent)
def foo
super({}, something)
end
RUBY
end
end

describe 'Empty String' do
Expand Down

0 comments on commit 35251b4

Please sign in to comment.