Sha256: bf773ad66c3a978b81887c230d9916814e57ed7645af09fa36226393baf80a9e

Contents?: true

Size: 1.34 KB

Versions: 43

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # Checks to make sure safe navigation isn't used with `blank?` in
      # a conditional.
      #
      # @safety
      #   While the safe navigation operator is generally a good idea, when
      #   checking `foo&.blank?` in a conditional, `foo` being `nil` will actually
      #   do the opposite of what the author intends.
      #
      #   For example:
      #
      #   [source,ruby]
      #   ----
      #   foo&.blank? #=> nil
      #   foo.blank? #=> true
      #   ----
      #
      # @example
      #   # bad
      #   do_something if foo&.blank?
      #   do_something unless foo&.blank?
      #
      #   # good
      #   do_something if foo.blank?
      #   do_something unless foo.blank?
      #
      class SafeNavigationWithBlank < Base
        extend AutoCorrector

        MSG = 'Avoid calling `blank?` with the safe navigation operator in conditionals.'

        def_node_matcher :safe_navigation_blank_in_conditional?, <<~PATTERN
          (if $(csend ... :blank?) ...)
        PATTERN

        def on_if(node)
          return unless safe_navigation_blank_in_conditional?(node)

          add_offense(node) do |corrector|
            corrector.replace(safe_navigation_blank_in_conditional?(node).location.dot, '.')
          end
        end
      end
    end
  end
end

Version data entries

43 entries across 42 versions & 7 rubygems

Version Path
rubocop-rails-2.30.1 lib/rubocop/cop/rails/safe_navigation_with_blank.rb
rubocop-rails-2.30.0 lib/rubocop/cop/rails/safe_navigation_with_blank.rb
tailscale_middleware-0.0.3 vendor/cache/ruby/3.4.0/gems/rubocop-rails-2.29.1/lib/rubocop/cop/rails/safe_navigation_with_blank.rb
rubocop-rails-2.29.1 lib/rubocop/cop/rails/safe_navigation_with_blank.rb
rubocop-rails-2.29.0 lib/rubocop/cop/rails/safe_navigation_with_blank.rb
rubocop-rails-2.28.0 lib/rubocop/cop/rails/safe_navigation_with_blank.rb
rubocop-rails-2.27.0 lib/rubocop/cop/rails/safe_navigation_with_blank.rb
rubocop-rails-2.26.2 lib/rubocop/cop/rails/safe_navigation_with_blank.rb
rubocop-rails-2.26.1 lib/rubocop/cop/rails/safe_navigation_with_blank.rb
rubocop-rails-2.26.0 lib/rubocop/cop/rails/safe_navigation_with_blank.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.25.1/lib/rubocop/cop/rails/safe_navigation_with_blank.rb
rubocop-rails-2.25.1 lib/rubocop/cop/rails/safe_navigation_with_blank.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.25.0/lib/rubocop/cop/rails/safe_navigation_with_blank.rb
rubocop-rails-2.24.1 lib/rubocop/cop/rails/safe_navigation_with_blank.rb
rubocop-rails-2.24.0 lib/rubocop/cop/rails/safe_navigation_with_blank.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.23.1/lib/rubocop/cop/rails/safe_navigation_with_blank.rb
rubocop-rails-2.23.1 lib/rubocop/cop/rails/safe_navigation_with_blank.rb
rubocop-rails-2.23.0 lib/rubocop/cop/rails/safe_navigation_with_blank.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rails-2.20.0/lib/rubocop/cop/rails/safe_navigation_with_blank.rb
rubocop-rails-2.22.2 lib/rubocop/cop/rails/safe_navigation_with_blank.rb