Sha256: a4f88f64437582086c33bc593bc60c4423bc1d0289fbb65628de8f97a05f571e

Contents?: true

Size: 1.65 KB

Versions: 14

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module FactoryBot
      # Checks for redundant `factory` option.
      #
      # @example
      #   # bad
      #   association :user, factory: :user
      #
      #   # good
      #   association :user
      class RedundantFactoryOption < ::RuboCop::Cop::Base
        extend AutoCorrector

        include RangeHelp

        MSG = 'Remove redundant `factory` option.'

        RESTRICT_ON_SEND = %i[association].freeze

        # @!method association_with_a_factory_option(node)
        def_node_matcher :association_with_a_factory_option, <<~PATTERN
          (send nil? :association
            (sym $_association_name)
            ...
            (hash
              <$(pair
                  (sym :factory)
                  {
                    (sym $_factory_name)
                    (array (sym $_factory_name))
                  }
                )
                ...
              >
            )
          )
        PATTERN

        def on_send(node)
          association_with_a_factory_option(node) do
            |association_name, factory_option, factory_name|
            next if association_name != factory_name

            add_offense(factory_option) do |corrector|
              autocorrect(corrector, factory_option)
            end
          end
        end

        private

        def autocorrect(corrector, node)
          corrector.remove(
            range_with_surrounding_comma(
              range_with_surrounding_space(
                node.source_range,
                side: :left
              ),
              :left
            )
          )
        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/redundant_factory_option.rb
rubocop-factory_bot-2.26.1 lib/rubocop/cop/factory_bot/redundant_factory_option.rb
rubocop-factory_bot-2.26.0 lib/rubocop/cop/factory_bot/redundant_factory_option.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/redundant_factory_option.rb
rubocop-factory_bot-2.25.1 lib/rubocop/cop/factory_bot/redundant_factory_option.rb
rubocop-factory_bot-2.25.0 lib/rubocop/cop/factory_bot/redundant_factory_option.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-factory_bot-2.23.1/lib/rubocop/cop/factory_bot/redundant_factory_option.rb
rubocop-factory_bot-2.24.0 lib/rubocop/cop/factory_bot/redundant_factory_option.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/redundant_factory_option.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-factory_bot-2.23.1/lib/rubocop/cop/factory_bot/redundant_factory_option.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-factory_bot-2.23.1/lib/rubocop/cop/factory_bot/redundant_factory_option.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rubocop-factory_bot-2.23.1/lib/rubocop/cop/factory_bot/redundant_factory_option.rb
rubocop-factory_bot-2.23.1 lib/rubocop/cop/factory_bot/redundant_factory_option.rb
rubocop-factory_bot-2.23.0 lib/rubocop/cop/factory_bot/redundant_factory_option.rb