Class: RuboCop::Cop::Homebrew::Blank

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
rubocops/blank.rb

Overview

Note:

This cop is unsafe autocorrection, because ' '.empty? returns false, but ' '.blank? returns true. Therefore, autocorrection is not compatible if the receiver is a non-empty blank string, tab, or newline meta characters.

Checks for code that can be written with simpler conditionals using Object#blank?.

Examples:

# Converts usages of `nil? || empty?` to `blank?`

# bad
foo.nil? || foo.empty?
foo == nil || foo.empty?

# good
foo.blank?

Constant Summary collapse

MSG_NIL_OR_EMPTY =
"Use `%<prefer>s` instead of `%<current>s`."

Instance Method Summary collapse

Instance Method Details

#on_or(node) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'rubocops/blank.rb', line 48

def on_or(node)
  nil_or_empty?(node) do |var1, var2|
    return if var1 != var2

    message = format(MSG_NIL_OR_EMPTY, prefer: replacement(var1), current: node.source)
    add_offense(node, message: message) do |corrector|
      autocorrect(corrector, node)
    end
  end
end