Sha256: 342681fd45cca1a891010b6a91ab2e3c0ed85e46497ad170f64b9eafc48f9c2b

Contents?: true

Size: 1.75 KB

Versions: 14

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module FactoryBot
      # Use definition in factory association instead of hard coding a strategy.
      #
      # @example
      #   # bad - only works for one strategy
      #   factory :foo do
      #     profile { create(:profile) }
      #   end
      #
      #   # good - implicit
      #   factory :foo do
      #     profile
      #   end
      #
      #   # good - explicit
      #   factory :foo do
      #     association :profile
      #   end
      #
      #   # good - inline
      #   factory :foo do
      #     profile { association :profile }
      #   end
      #
      class FactoryAssociationWithStrategy < ::RuboCop::Cop::Base
        MSG = 'Use an implicit, explicit or inline definition instead of ' \
              'hard coding a strategy for setting association within factory.'

        HARDCODED = Set.new(%i[create build build_stubbed]).freeze

        # @!method factory_declaration(node)
        def_node_matcher :factory_declaration, <<~PATTERN
          (block (send nil? {:factory :trait} ...)
            ...
          )
        PATTERN

        # @!method factory_strategy_association(node)
        def_node_matcher :factory_strategy_association, <<~PATTERN
          (block
            (send nil? _association_name)
            (args)
            < $(send nil? HARDCODED ...) ... >
          )
        PATTERN

        def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
          factory_declaration(node) do
            node.each_node do |statement|
              factory_strategy_association(statement) do |hardcoded_association|
                add_offense(hardcoded_association)
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 5 rubygems

Version Path
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-factory_bot-2.26.1/lib/rubocop/cop/factory_bot/factory_association_with_strategy.rb
rubocop-factory_bot-2.26.1 lib/rubocop/cop/factory_bot/factory_association_with_strategy.rb
rubocop-factory_bot-2.26.0 lib/rubocop/cop/factory_bot/factory_association_with_strategy.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-factory_bot-2.25.1/lib/rubocop/cop/factory_bot/factory_association_with_strategy.rb
rubocop-factory_bot-2.25.1 lib/rubocop/cop/factory_bot/factory_association_with_strategy.rb
rubocop-factory_bot-2.25.0 lib/rubocop/cop/factory_bot/factory_association_with_strategy.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-factory_bot-2.23.1/lib/rubocop/cop/factory_bot/factory_association_with_strategy.rb
rubocop-factory_bot-2.24.0 lib/rubocop/cop/factory_bot/factory_association_with_strategy.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-factory_bot-2.23.1/lib/rubocop/cop/factory_bot/factory_association_with_strategy.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-factory_bot-2.23.1/lib/rubocop/cop/factory_bot/factory_association_with_strategy.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-factory_bot-2.23.1/lib/rubocop/cop/factory_bot/factory_association_with_strategy.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rubocop-factory_bot-2.23.1/lib/rubocop/cop/factory_bot/factory_association_with_strategy.rb
rubocop-factory_bot-2.23.1 lib/rubocop/cop/factory_bot/factory_association_with_strategy.rb
rubocop-factory_bot-2.23.0 lib/rubocop/cop/factory_bot/factory_association_with_strategy.rb