Checks for if and unless statements that would fit on one line if written as a modifier if/unless. The maximum line length is configurable.
# File lib/rubocop/cop/style/if_unless_modifier.rb, line 11 def error_message(keyword) "Favor modifier `#{keyword}` usage when having a single-line body." ' Another good alternative is the usage of control flow `&&`/`||`.' end
# File lib/rubocop/cop/style/if_unless_modifier.rb, line 16 def investigate(processed_source) return unless processed_source.ast on_node(:if, processed_source.ast) do |node| # discard ternary ops, if/else and modifier if/unless nodes next if ternary_op?(node) next if modifier_if?(node) next if elsif?(node) next if if_else?(node) if check(node, processed_source.comments) add_offense(node, :keyword, error_message(node.loc.keyword.source)) end end end
Generated with the Darkfish Rdoc Generator 2.