class Rubocop::Cop::Style::IfUnlessModifier
Checks for if and unless statements that would fit on one line if written as a modifier if/unless.
Public Instance Methods
elsif?(node)
click to toggle source
# File lib/rubocop/cop/style/favor_modifier.rb, line 87 def elsif?(node) node.loc.keyword.is?('elsif') end
error_message()
click to toggle source
# File lib/rubocop/cop/style/favor_modifier.rb, line 59 def error_message 'Favor modifier if/unless usage when you have a single-line body. ' + 'Another good alternative is the usage of control flow &&/||.' end
if_else?(node)
click to toggle source
# File lib/rubocop/cop/style/favor_modifier.rb, line 91 def if_else?(node) node.loc.respond_to?(:else) && node.loc.else end
inspect(source_buffer, source, tokens, ast, comments)
click to toggle source
# File lib/rubocop/cop/style/favor_modifier.rb, line 64 def inspect(source_buffer, source, tokens, ast, comments) return unless ast on_node(:if, ast) do |node| # discard ternary ops, if/else and modifier if/unless nodes return if ternary_op?(node) return if modifier_if?(node) return if elsif?(node) return if if_else?(node) if check(node, comments) add_offence(:convention, node.loc.expression, error_message) end end end
modifier_if?(node)
click to toggle source
# File lib/rubocop/cop/style/favor_modifier.rb, line 83 def modifier_if?(node) node.loc.end.nil? end
ternary_op?(node)
click to toggle source
# File lib/rubocop/cop/style/favor_modifier.rb, line 79 def ternary_op?(node) node.loc.respond_to?(:question) end