Sha256: 0e730b901ae5900ada1e9351f3fef17a950f0691130f7866df851d9dd361f2e1

Contents?: true

Size: 1.02 KB

Versions: 21

Compression:

Stored size: 1.02 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 < Base
        extend AutoCorrector

        MSG = '`allow_blank` is not a valid option, use `allow_nil`.'
        RESTRICT_ON_SEND = %i[delegate].freeze

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

        def on_send(node)
          return unless (offending_node = allow_blank_option(node))

          add_offense(offending_node) do |corrector|
            corrector.replace(offending_node.key.source_range, 'allow_nil')
          end
        end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 2 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rails-2.14.2/lib/rubocop/cop/rails/delegate_allow_blank.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rails-2.14.2/lib/rubocop/cop/rails/delegate_allow_blank.rb
rubocop-rails-2.14.2 lib/rubocop/cop/rails/delegate_allow_blank.rb
rubocop-rails-2.14.1 lib/rubocop/cop/rails/delegate_allow_blank.rb
rubocop-rails-2.14.0 lib/rubocop/cop/rails/delegate_allow_blank.rb
rubocop-rails-2.13.2 lib/rubocop/cop/rails/delegate_allow_blank.rb
rubocop-rails-2.13.1 lib/rubocop/cop/rails/delegate_allow_blank.rb
rubocop-rails-2.13.0 lib/rubocop/cop/rails/delegate_allow_blank.rb
rubocop-rails-2.12.4 lib/rubocop/cop/rails/delegate_allow_blank.rb
rubocop-rails-2.12.3 lib/rubocop/cop/rails/delegate_allow_blank.rb
rubocop-rails-2.12.2 lib/rubocop/cop/rails/delegate_allow_blank.rb
rubocop-rails-2.12.1 lib/rubocop/cop/rails/delegate_allow_blank.rb
rubocop-rails-2.12.0 lib/rubocop/cop/rails/delegate_allow_blank.rb
rubocop-rails-2.11.3 lib/rubocop/cop/rails/delegate_allow_blank.rb
rubocop-rails-2.11.2 lib/rubocop/cop/rails/delegate_allow_blank.rb
rubocop-rails-2.11.1 lib/rubocop/cop/rails/delegate_allow_blank.rb
rubocop-rails-2.11.0 lib/rubocop/cop/rails/delegate_allow_blank.rb
rubocop-rails-2.10.1 lib/rubocop/cop/rails/delegate_allow_blank.rb
rubocop-rails-2.10.0 lib/rubocop/cop/rails/delegate_allow_blank.rb
rubocop-rails-2.9.1 lib/rubocop/cop/rails/delegate_allow_blank.rb