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

Ignore offenses inside redundant parentheses #270

Merged
merged 1 commit into from
Oct 24, 2023
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/change_ignore_offenses_inside_redundant.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#270](https://github.com/rubocop/rubocop-minitest/pull/270): Ignore offenses inside redundant parentheses. ([@sambostock][])
9 changes: 4 additions & 5 deletions lib/rubocop/cop/minitest/assert_empty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ class AssertEmpty < Base
remove_method :on_send
def on_send(node)
return unless node.method?(:assert)
return unless (arguments = peel_redundant_parentheses_from(node.arguments))
return unless arguments.first.respond_to?(:method?) && arguments.first.method?(:empty?)
return unless arguments.first.arguments.empty?
return unless node.arguments.first.respond_to?(:method?) && node.arguments.first.method?(:empty?)
return unless node.arguments.first.arguments.empty?

add_offense(node, message: offense_message(arguments)) do |corrector|
autocorrect(corrector, node, arguments)
add_offense(node, message: offense_message(node.arguments)) do |corrector|
autocorrect(corrector, node, node.arguments)
end
end
end
Expand Down
9 changes: 4 additions & 5 deletions lib/rubocop/cop/minitest/refute_empty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ class RefuteEmpty < Base
remove_method :on_send
def on_send(node)
return unless node.method?(:refute)
return unless (arguments = peel_redundant_parentheses_from(node.arguments))
return unless arguments.first.respond_to?(:method?) && arguments.first.method?(:empty?)
return unless arguments.first.arguments.empty?
return unless node.arguments.first.respond_to?(:method?) && node.arguments.first.method?(:empty?)
return unless node.arguments.first.arguments.empty?

add_offense(node, message: offense_message(arguments)) do |corrector|
autocorrect(corrector, node, arguments)
add_offense(node, message: offense_message(node.arguments)) do |corrector|
autocorrect(corrector, node, node.arguments)
end
end
end
Expand Down
25 changes: 5 additions & 20 deletions lib/rubocop/cop/mixin/minitest_cop_rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,12 @@ def define_rule(assertion_method, target_method:, preferred_method: nil, inverse
def on_send(node)
return unless node.method?(:#{assertion_method})
return unless (arguments = peel_redundant_parentheses_from(node.arguments))
return unless arguments.first&.call_type?
return if arguments.first.arguments.empty? ||
#{target_methods}.none? { |target_method| arguments.first.method?(target_method) }
return unless node.arguments.first&.call_type?
return if node.arguments.first.arguments.empty? ||
#{target_methods}.none? { |target_method| node.arguments.first.method?(target_method) }
add_offense(node, message: offense_message(arguments)) do |corrector|
autocorrect(corrector, node, arguments)
add_offense(node, message: offense_message(node.arguments)) do |corrector|
autocorrect(corrector, node, node.arguments)
end
end
Expand All @@ -60,21 +59,11 @@ def autocorrect(corrector, node, arguments)
new_arguments = new_arguments(arguments).join(', ')
if enclosed_in_redundant_parentheses?(node)
new_arguments = '(' + new_arguments + ')'
end
corrector.replace(node.first_argument, new_arguments)
end
private
def peel_redundant_parentheses_from(arguments)
return arguments unless arguments.first&.begin_type?
peel_redundant_parentheses_from(arguments.first.children)
end
def offense_message(arguments)
message_argument = arguments.last if arguments.first != arguments.last
Expand Down Expand Up @@ -103,10 +92,6 @@ def new_arguments(arguments)
new_arguments
end
def enclosed_in_redundant_parentheses?(node)
node.arguments.first.begin_type?
end
def correct_receiver(receiver)
receiver ? receiver.source : 'self'
end
Expand Down
22 changes: 6 additions & 16 deletions lib/rubocop/cop/mixin/predicate_assertion_handleable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,13 @@ module PredicateAssertionHandleable
MSG = 'Prefer using `%<assertion_type>s_predicate(%<new_arguments>s)`.'

def on_send(node)
return unless (arguments = peel_redundant_parentheses_from(node.arguments))
return unless node.first_argument
return if node.first_argument.block_type? || node.first_argument.numblock_type?
return unless predicate_method?(node.first_argument)
return unless node.first_argument.arguments.count.zero?

first_argument = arguments.first

return unless first_argument
return if first_argument.block_type? || first_argument.numblock_type?
return unless predicate_method?(first_argument)
return unless first_argument.arguments.count.zero?

add_offense(node, message: offense_message(arguments)) do |corrector|
autocorrect(corrector, node, arguments)
add_offense(node, message: offense_message(node.arguments)) do |corrector|
autocorrect(corrector, node, node.arguments)
end
end

Expand All @@ -33,12 +29,6 @@ def autocorrect(corrector, node, arguments)

private

def peel_redundant_parentheses_from(arguments)
return arguments unless arguments.first&.begin_type?

peel_redundant_parentheses_from(arguments.first.children)
end

def predicate_method?(first_argument)
first_argument.respond_to?(:predicate_method?) && first_argument.predicate_method?
end
Expand Down
14 changes: 3 additions & 11 deletions test/rubocop/cop/minitest/assert_empty_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,12 @@ def test_do_something
RUBY
end

def test_registers_offense_when_using_assert_with_empty_in_redundant_parentheses
assert_offense(<<~RUBY)
# Redundant parentheses should be removed by `Style/RedundantParentheses` cop.
def test_does_not_register_offense_when_using_assert_with_empty_in_redundant_parentheses
assert_no_offenses(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
assert((somestuff.empty?))
^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_empty(somestuff)`.
end
end
RUBY

assert_correction(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
assert_empty((somestuff))
end
end
RUBY
Expand Down
14 changes: 3 additions & 11 deletions test/rubocop/cop/minitest/assert_includes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,12 @@ def test_do_something
RUBY
end

def test_registers_offense_when_using_assert_with_include_in_redundant_parentheses
assert_offense(<<~RUBY)
# Redundant parentheses should be removed by `Style/RedundantParentheses` cop.
def test_does_not_register_offense_when_using_assert_with_include_in_redundant_parentheses
assert_no_offenses(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
assert((collection.include?(object)))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_includes(collection, object)`.
end
end
RUBY

assert_correction(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
assert_includes((collection, object))
end
end
RUBY
Expand Down
14 changes: 3 additions & 11 deletions test/rubocop/cop/minitest/assert_predicate_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,12 @@ def test_do_something
RUBY
end

def test_registers_offense_when_using_assert_with_predicate_method_in_redundant_parentheses
assert_offense(<<~RUBY)
# Redundant parentheses should be removed by `Style/RedundantParentheses` cop.
def test_does_not_register_offense_when_using_assert_with_predicate_method_in_redundant_parentheses
assert_no_offenses(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
assert((obj.one?))
^^^^^^^^^^^^^^^^^^ Prefer using `assert_predicate(obj, :one?)`.
end
end
RUBY

assert_correction(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
assert_predicate(obj, :one?)
end
end
RUBY
Expand Down
12 changes: 2 additions & 10 deletions test/rubocop/cop/minitest/assert_respond_to_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,12 @@ def test_do_something
RUBY
end

# Redundant parentheses should be removed by `Style/RedundantParentheses` cop.
def test_registers_offense_when_using_assert_with_respond_to_in_redundant_parentheses
assert_offense(<<~RUBY)
assert_no_offenses(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
assert((object.respond_to?(:do_something)))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_respond_to(object, :do_something)`.
end
end
RUBY

assert_correction(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
assert_respond_to((object, :do_something))
end
end
RUBY
Expand Down
12 changes: 2 additions & 10 deletions test/rubocop/cop/minitest/refute_empty_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,12 @@ def test_do_something
RUBY
end

# Redundant parentheses should be removed by `Style/RedundantParentheses` cop.
def test_registers_offense_when_using_refute_with_empty_in_redundant_parentheses
assert_offense(<<~RUBY)
assert_no_offenses(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
refute((somestuff.empty?))
^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_empty(somestuff)`.
end
end
RUBY

assert_correction(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
refute_empty((somestuff))
end
end
RUBY
Expand Down
14 changes: 3 additions & 11 deletions test/rubocop/cop/minitest/refute_includes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,12 @@ def test_do_something
RUBY
end

def test_registers_offense_when_using_refute_with_include_in_redundant_parentheses
assert_offense(<<~RUBY)
# Redundant parentheses should be removed by `Style/RedundantParentheses` cop.
def test_does_not_register_offense_when_using_refute_with_include_in_redundant_parentheses
assert_no_offenses(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
refute((collection.include?(object)))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_includes(collection, object)`.
end
end
RUBY

assert_correction(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
refute_includes((collection, object))
end
end
RUBY
Expand Down
2 changes: 1 addition & 1 deletion test/rubocop/cop/minitest/refute_match_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def test_do_something
end

# Redundant parentheses should be removed in another cop.
define_method("test_registers_offense_when_using_refute_with_#{matcher}_in_redundant_parentheses") do
define_method("test_does_not_register_offense_when_using_refute_with_#{matcher}_in_redundant_parentheses") do
assert_no_offenses(<<~RUBY, matcher: matcher)
class FooTest < Minitest::Test
def test_do_something
Expand Down
14 changes: 3 additions & 11 deletions test/rubocop/cop/minitest/refute_predicate_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,12 @@ def test_do_something
RUBY
end

def test_registers_offense_when_using_refute_with_predicate_method_in_redundant_parentheses
assert_offense(<<~RUBY)
# Redundant parentheses should be removed by `Style/RedundantParentheses` cop.
def test_does_not_register_offense_when_using_refute_with_predicate_method_in_redundant_parentheses
assert_no_offenses(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
refute((obj.one?))
^^^^^^^^^^^^^^^^^^ Prefer using `refute_predicate(obj, :one?)`.
end
end
RUBY

assert_correction(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
refute_predicate(obj, :one?)
end
end
RUBY
Expand Down
14 changes: 3 additions & 11 deletions test/rubocop/cop/minitest/refute_respond_to_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,12 @@ def test_do_something
RUBY
end

def test_registers_offense_when_using_refute_with_respond_to_in_redundant_parentheses
assert_offense(<<~RUBY)
# Redundant parentheses should be removed by `Style/RedundantParentheses` cop.
def test_does_not_register_offense_when_using_refute_with_respond_to_in_redundant_parentheses
assert_no_offenses(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
refute((object.respond_to?(:do_something)))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_respond_to(object, :do_something)`.
end
end
RUBY

assert_correction(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
refute_respond_to((object, :do_something))
end
end
RUBY
Expand Down