diff --git a/CHANGELOG.md b/CHANGELOG.md index 1202ae631dd..3faab212069 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rubocop/cop/style/empty_literal.rb b/lib/rubocop/cop/style/empty_literal.rb index f227609760e..1424bfe58b9 100644 --- a/lib/rubocop/cop/style/empty_literal.rb +++ b/lib/rubocop/cop/style/empty_literal.rb @@ -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) @@ -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) @@ -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 '{}' diff --git a/spec/rubocop/cop/style/empty_literal_spec.rb b/spec/rubocop/cop/style/empty_literal_spec.rb index 8173ffdabb2..0a29511e4ff 100644 --- a/spec/rubocop/cop/style/empty_literal_spec.rb +++ b/spec/rubocop/cop/style/empty_literal_spec.rb @@ -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