Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #6648] EmptyLiteral - wrap arguments in parentheses when correcting Hash.new is the fist argument to super #6680

Merged
merged 1 commit into from Jan 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -24,6 +24,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 &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have been using #equal? for identity checks. (Perhaps a cop opportunity. 🙂)

!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