Sha256: bd5c63df9ed584c1c377ddeeb087f023d9caacebcf2d57ac6e8a50a9af4a75b2

Contents?: true

Size: 1.29 KB

Versions: 6835

Compression:

Stored size: 1.29 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`.'.freeze

        def_node_matcher :delegate_options, <<-PATTERN
          (send nil? :delegate _ $hash)
        PATTERN

        def_node_matcher :allow_blank_option?, <<-PATTERN
          (pair (sym :allow_blank) true)
        PATTERN

        def on_send(node)
          offending_node = allow_blank_option(node)

          return unless offending_node

          add_offense(offending_node)
        end

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

        private

        def allow_blank_option(node)
          delegate_options(node) do |hash|
            hash.pairs.find { |opt| allow_blank_option?(opt) }
          end
        end
      end
    end
  end
end

Version data entries

6,835 entries across 6,829 versions & 26 rubygems

Version Path
ory-client-0.0.1.alpha36 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/delegate_allow_blank.rb
ory-kratos-client-0.8.2.alpha1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/delegate_allow_blank.rb
ory-client-0.0.1.alpha31 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/delegate_allow_blank.rb
primary_connect_proto-0.4.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/delegate_allow_blank.rb
primary_connect_proto-0.3.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/delegate_allow_blank.rb
primary_connect_proto-0.2.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/delegate_allow_blank.rb
primary_connect_proto-0.1.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/delegate_allow_blank.rb
ory-client-0.0.1.alpha30 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/delegate_allow_blank.rb
ory-client-0.0.1.alpha29 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/delegate_allow_blank.rb
primary_connect_proto-0.0.6 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/delegate_allow_blank.rb
ory-client-0.0.1.alpha28 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/delegate_allow_blank.rb
primary_connect_proto-0.0.5 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/delegate_allow_blank.rb
ory-client-0.0.1.alpha27 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/delegate_allow_blank.rb
ory-client-0.0.1.alpha24 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/delegate_allow_blank.rb
primary_connect_proto-0.0.4 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/delegate_allow_blank.rb
primary_connect_proto-0.0.3 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/delegate_allow_blank.rb
ory-client-0.0.1.alpha23 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/delegate_allow_blank.rb
ory-kratos-client-0.8.0.alpha2 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/delegate_allow_blank.rb
ory-client-0.0.1.alpha21 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/delegate_allow_blank.rb
ory-keto-client-0.7.0.alpha1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/delegate_allow_blank.rb