Sha256: 571547b539fd4c15e17aaffecc06ea25e13c539a8fc226c58b7d057123b61fad

Contents?: true

Size: 1.01 KB

Versions: 10

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # This cop looks for delegations that pass :allow_blank as an option
      # instead of :allow_nil. :allow_blank is not a valid option to pass
      # to ActiveSupport#delegate.
      #
      # @example
      #   # bad
      #   delegate :foo, to: :bar, allow_blank: true
      #
      #   # good
      #   delegate :foo, to: :bar, allow_nil: true
      class DelegateAllowBlank < Cop
        MSG = '`allow_blank` is not a valid option, use `allow_nil`.'

        def_node_matcher :allow_blank_option, <<~PATTERN
          (send nil? :delegate _ (hash <$(pair (sym :allow_blank) true) ...>))
        PATTERN

        def on_send(node)
          allow_blank_option(node) do |offending_node|
            add_offense(offending_node)
          end
        end

        def autocorrect(pair_node)
          lambda do |corrector|
            corrector.replace(pair_node.key.source_range, 'allow_nil')
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rubocop-rails-2.8.1 lib/rubocop/cop/rails/delegate_allow_blank.rb
rubocop-rails-2.8.0 lib/rubocop/cop/rails/delegate_allow_blank.rb
rubocop-rails-2.7.1 lib/rubocop/cop/rails/delegate_allow_blank.rb
rubocop-rails-2.7.0 lib/rubocop/cop/rails/delegate_allow_blank.rb
rubocop-rails-2.6.0 lib/rubocop/cop/rails/delegate_allow_blank.rb
rubocop-rails-2.5.2 lib/rubocop/cop/rails/delegate_allow_blank.rb
rubocop-rails-2.5.1 lib/rubocop/cop/rails/delegate_allow_blank.rb
rubocop-rails-2.5.0 lib/rubocop/cop/rails/delegate_allow_blank.rb
rubocop-rails-2.4.2 lib/rubocop/cop/rails/delegate_allow_blank.rb
rubocop-rails-2.4.1 lib/rubocop/cop/rails/delegate_allow_blank.rb