Class: RuboCop::Cop::RSpec::FactoryBot::DynamicAttributeDefinedStatically

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

Overview

Prefer declaring dynamic attribute values in a block.

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 }

See Also:

Constant Summary collapse

MSG =
'Use a block to set a dynamic value to an attribute.'.freeze

Constants inherited from Cop

Cop::DEFAULT_CONFIGURATION, Cop::DEFAULT_PATTERN_RE

Constants included from RSpec::Language

RSpec::Language::ALL

Instance Method Summary collapse

Methods inherited from Cop

inherited, #relevant_file?

Instance Method Details

#autocorrect(node) ⇒ Object



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

def autocorrect(node)
  if !method_uses_parens?(node.location)
    autocorrect_without_parens(node)
  elsif value_hash_without_braces?(node.descendants.first)
    autocorrect_hash_without_braces(node)
  else
    autocorrect_replacing_parens(node)
  end
end

#on_block(node) ⇒ Object



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

def on_block(node)
  factory_attributes(node).to_a.flatten.each do |attribute|
    next if callback_with_symbol_proc?(attribute) ||
        static?(attribute)
    add_offense(attribute, location: :expression)
  end
end