Sha256: dbeaf8c89b44639ea3ac3379bd752b113b7e9c6b3548a3c7fba29f32e28e77d3

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # 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.
      class IfUnlessModifier < Cop
        include StatementModifier

        def error_message(keyword)
          "Favor modifier #{keyword} usage when you have a single-line body." \
          ' Another good alternative is the usage of control flow &&/||.'
        end

        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
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-0.19.1 lib/rubocop/cop/style/if_unless_modifier.rb
rubocop-0.19.0 lib/rubocop/cop/style/if_unless_modifier.rb