Class: RuboCop::Cop::Faker::DeprecatedArguments

Inherits:
RuboCop::Cop
  • Object
show all
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/faker/deprecated_arguments.rb

Overview

Checks that Faker arguments style is based on Faker 2. Use keyword arguments instead of positional arguments.

Examples:

# bad
Avatar.image(slug, size, format)

# good
Avatar.image(slug: slug, size: size, format: format)

Constant Summary collapse

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.'

Instance Method Summary collapse

Instance Method Details

#autocorrect(node) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/rubocop/cop/faker/deprecated_arguments.rb', line 49

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

#on_send(node) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rubocop/cop/faker/deprecated_arguments.rb', line 25

def on_send(node)
  return unless node.receiver

  class_name = faker_class_name(node)

  return unless (methods = argument_keywords[class_name])

  node = node.parent if unique_generator_method?(node)

  return unless (keywords = methods[node.method_name.to_s])

  node.arguments.each_with_index do |argument, index|
    next if argument.hash_type?

    message = format_message(
      keyword: keywords[index], arg: argument.source,
      class_name: class_name, index: index,
      method_name: node.method_name
    )

    add_offense_for_arguments(node, argument, message)
  end
end