Sha256: 7e67d120ceee3300e26891b5506719d091768c40779924c8e35a29923a800bb1
Contents?: true
Size: 1.37 KB
Versions: 8
Compression:
Stored size: 1.37 KB
Contents
# frozen_string_literal: true module RuboCop module Cop module Rails # This cop 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
8 entries across 8 versions & 2 rubygems