Class: RuboCop::Cop::RSpec::FactoryBot::AttributeDefinedStatically

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/rspec/factory_bot/attribute_defined_statically.rb

Overview

Always declare attribute values as blocks.

Examples:

# bad
kind [:active, :rejected].sample

# good
kind { [:active, :rejected].sample }

# bad
closed_at 1.day.from_now

# good
closed_at { 1.day.from_now }

# bad
count 1

# good
count { 1 }

Constant Summary collapse

MSG =
'Use a block to declare attribute values.'

Constants inherited from Cop

Cop::DEFAULT_CONFIGURATION, Cop::DEFAULT_PATTERN_RE

Constants included from RSpec::Language

RSpec::Language::ALL, RSpec::Language::RSPEC

Instance Method Summary collapse

Methods inherited from Cop

inherited, #relevant_file?

Instance Method Details

#autocorrect(node) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/rubocop/cop/rspec/factory_bot/attribute_defined_statically.rb', line 47

def autocorrect(node)
  if node.parenthesized?
    autocorrect_replacing_parens(node)
  else
    autocorrect_without_parens(node)
  end
end

#on_block(node) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/rubocop/cop/rspec/factory_bot/attribute_defined_statically.rb', line 38

def on_block(node)
  factory_attributes(node).to_a.flatten.each do |attribute|
    next unless offensive_receiver?(attribute.receiver, node)
    next if proc?(attribute) || association?(attribute)

    add_offense(attribute, location: :expression)
  end
end