Class: RuboCop::Cop::RSpec::FactoryBot::StaticAttributeDefinedDynamically

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

Overview

Prefer declaring static attribute values without a block.

Examples:

# bad
kind { :static }

# good
kind :static

# bad
comments_count { 0 }

# good
comments_count 0

# bad
type { User::MAGIC }

# good
type User::MAGIC

See Also:

Constant Summary collapse

MSG =
'Do not use a block to set a static 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



49
50
51
52
53
54
55
56
# File 'lib/rubocop/cop/rspec/factory_bot/static_attribute_defined_dynamically.rb', line 49

def autocorrect(node)
  lambda do |corrector|
    corrector.replace(
      node.loc.expression,
      autocorrected_source(node)
    )
  end
end

#on_block(node) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/rubocop/cop/rspec/factory_bot/static_attribute_defined_dynamically.rb', line 41

def on_block(node)
  factory_attributes(node).to_a.flatten.each do |attribute|
    values = block_value_matcher(attribute)
    next if values.to_a.none? { |v| static?(v) }
    add_offense(attribute, location: :expression)
  end
end