Class: RuboCop::Cop::FactoryBot::RedundantFactoryOption

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/factory_bot/redundant_factory_option.rb

Overview

Checks for redundant ‘factory` option.

Examples:

# bad
association :user, factory: :user

# good
association :user

Constant Summary collapse

MSG =
'Remove redundant `factory` option.'
RESTRICT_ON_SEND =
%i[association].freeze

Instance Method Summary collapse

Instance Method Details

#association_with_a_factory_option(node) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rubocop/cop/factory_bot/redundant_factory_option.rb', line 24

def_node_matcher :association_with_a_factory_option, <<~PATTERN
  (send nil? :association
    (sym $_association_name)
    ...
    (hash
      <$(pair
          (sym :factory)
          {
            (sym $_factory_name)
            (array (sym $_factory_name))
          }
        )
        ...
      >
    )
  )
PATTERN

#on_send(node) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/rubocop/cop/factory_bot/redundant_factory_option.rb', line 42

def on_send(node)
  association_with_a_factory_option(node) do
    |association_name, factory_option, factory_name|
    next if association_name != factory_name

    add_offense(factory_option) do |corrector|
      autocorrect(corrector, factory_option)
    end
  end
end