lib/rubocop/cop/mixin/minitest_cop_rule.rb in rubocop-minitest-0.11.1 vs lib/rubocop/cop/mixin/minitest_cop_rule.rb in rubocop-minitest-0.12.0
- old
+ new
@@ -24,36 +24,35 @@
def define_rule(assertion_method, target_method:, preferred_method: nil, inverse: false)
preferred_method = "#{assertion_method}_#{target_method.to_s.delete('?')}" if preferred_method.nil?
class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
include ArgumentRangeHelper
+ extend AutoCorrector
MSG = 'Prefer using `#{preferred_method}(%<new_arguments>s)` over ' \
'`#{assertion_method}(%<original_arguments>s)`.'
RESTRICT_ON_SEND = %i[#{assertion_method}].freeze
def on_send(node)
return unless node.method?(:#{assertion_method})
return unless (arguments = peel_redundant_parentheses_from(node.arguments))
return unless arguments.first.respond_to?(:method?) && arguments.first.method?(:#{target_method})
- add_offense(node, message: offense_message(arguments))
+ add_offense(node, message: offense_message(arguments)) do |corrector|
+ autocorrect(corrector, node, arguments)
+ end
end
- def autocorrect(node)
- lambda do |corrector|
- corrector.replace(node.loc.selector, '#{preferred_method}')
+ def autocorrect(corrector, node, arguments)
+ corrector.replace(node.loc.selector, '#{preferred_method}')
- arguments = peel_redundant_parentheses_from(node.arguments)
+ new_arguments = new_arguments(arguments).join(', ')
- new_arguments = new_arguments(arguments).join(', ')
-
- if enclosed_in_redundant_parentheses?(node)
- new_arguments = '(' + new_arguments + ')'
- end
-
- corrector.replace(first_argument_range(node), new_arguments)
+ if enclosed_in_redundant_parentheses?(node)
+ new_arguments = '(' + new_arguments + ')'
end
+
+ corrector.replace(first_argument_range(node), new_arguments)
end
private
def peel_redundant_parentheses_from(arguments)