lib/rubocop/cop/faker/deprecated_arguments.rb in rubocop-faker-1.1.0 vs lib/rubocop/cop/faker/deprecated_arguments.rb in rubocop-faker-1.2.0
- old
+ new
@@ -1,23 +1,23 @@
# frozen_string_literal: true
module RuboCop
module Cop
module Faker
- #
# Checks that Faker arguments style is based on Faker 2.
# Use keyword arguments instead of positional arguments.
#
# @example
# # bad
# Avatar.image(slug, size, format)
#
# # good
# Avatar.image(slug: slug, size: size, format: format)
#
- class DeprecatedArguments < Cop
+ class DeprecatedArguments < Base
include RangeHelp
+ extend AutoCorrector
MSG = 'Passing `%<arg>s` with the %<index>s argument of ' \
'`%<class_name>s.%<method_name>s` is deprecated. ' \
'Use keyword argument like `%<class_name>s.%<method_name>s' \
'(%<keyword>s: %<arg>s)` instead.'
@@ -44,25 +44,14 @@
add_offense_for_arguments(node, argument, message)
end
end
- def autocorrect(node)
- methods = argument_keywords[faker_class_name(node)]
- keywords = methods[node.method_name.to_s]
-
- kwargs = build_kwargs_style(node, keywords)
-
- lambda do |corrector|
- corrector.replace(arguments_range(node), kwargs)
- end
- end
-
private
def unique_generator_method?(node)
- node.method?(:unique) && node.arguments.size.zero?
+ node.method?(:unique) && node.arguments.empty?
end
def format_message(keyword:, arg:, index:, class_name:, method_name:)
i = case index
when 0 then '1st'
@@ -80,13 +69,23 @@
)
end
def add_offense_for_arguments(node, argument, message)
add_offense(
- node,
- location: argument.source_range,
+ argument,
message: message
- )
+ ) do |corrector|
+ autocorrect(corrector, node)
+ end
+ end
+
+ def autocorrect(corrector, node)
+ methods = argument_keywords[faker_class_name(node)]
+ keywords = methods[node.method_name.to_s]
+
+ kwargs = build_kwargs_style(node, keywords)
+
+ corrector.replace(arguments_range(node), kwargs)
end
def build_kwargs_style(node, keywords)
node.arguments.map.with_index do |positional_argument, index|
if positional_argument.hash_type?