Class: RuboCop::Cop::RSpec::FactoryBot::AttributeDefinedStatically
- Inherits:
-
Base
- Object
- Base
- Base
- RuboCop::Cop::RSpec::FactoryBot::AttributeDefinedStatically
- Extended by:
- AutoCorrector
- Defined in:
- lib/rubocop/cop/rspec/factory_bot/attribute_defined_statically.rb
Overview
Always declare attribute values as blocks.
Constant Summary collapse
- MSG =
'Use a block to declare attribute values.'
Instance Method Summary collapse
- #factory_attributes(node) ⇒ Object
-
#on_block(node) ⇒ Object
rubocop:disable InternalAffairs/NumblockHandler.
- #value_matcher(node) ⇒ Object
Methods inherited from Base
inherited, #on_new_investigation
Methods included from RSpec::Language::NodePattern
#block_pattern, #numblock_pattern, #send_pattern
Methods included from RSpec::Language
#example?, #example_group?, #example_group_with_body?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?
Instance Method Details
#factory_attributes(node) ⇒ Object
39 40 41 |
# File 'lib/rubocop/cop/rspec/factory_bot/attribute_defined_statically.rb', line 39 def_node_matcher :factory_attributes, <<-PATTERN (block (send _ #attribute_defining_method? ...) _ { (begin $...) $(send ...) } ) PATTERN |
#on_block(node) ⇒ Object
rubocop:disable InternalAffairs/NumblockHandler
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rubocop/cop/rspec/factory_bot/attribute_defined_statically.rb', line 43 def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler attributes = factory_attributes(node) || [] attributes = [attributes] unless attributes.is_a?(Array) # rubocop:disable Style/ArrayCoercion, Lint/RedundantCopDisableDirective attributes.each do |attribute| next unless offensive_receiver?(attribute.receiver, node) next if proc?(attribute) || association?(attribute.first_argument) add_offense(attribute) do |corrector| autocorrect(corrector, attribute) end end end |
#value_matcher(node) ⇒ Object
34 35 36 |
# File 'lib/rubocop/cop/rspec/factory_bot/attribute_defined_statically.rb', line 34 def_node_matcher :value_matcher, <<-PATTERN (send _ !#reserved_method? $...) PATTERN |