Sha256: 5c138de6145eff9e8e43a45f2fac41cf5d5444dce69fd7558c2b2a8770c86c14

Contents?: true

Size: 1.83 KB

Versions: 8

Compression:

Stored size: 1.83 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module Style
      # This cop checks how the *when*s of a *case* expression
      # are indented in relation to its *case* or *end* keyword.
      #
      # It will register a separate offense for each misaligned *when*.
      class CaseIndentation < Cop
        include ConfigurableEnforcedStyle

        def on_case(case_node)
          _condition, *whens, _else = *case_node

          base = style
          indent = cop_config['IndentOneStep']
          base_column = base_column(case_node, base)

          whens.each do |when_node|
            check_when(when_node, case_node, base, indent, base_column)
          end
        end

        private

        def check_when(when_node, case_node, base, indent, base_column)
          pos = when_node.loc.keyword
          expected_column = base_column +
            (indent ? IndentationWidth::CORRECT_INDENTATION : 0)
          if pos.column == expected_column
            correct_style_detected
          else
            msg = 'Indent `when` ' + if indent
                                       "one step more than `#{base}`."
                                     else
                                       "as deep as `#{base}`."
                                     end
            add_offense(when_node, pos, msg) do
              if pos.column == base_column(case_node, alternative_style)
                opposite_style_detected
              else
                unrecognized_style_detected
              end
            end
          end
        end

        def parameter_name
          'IndentWhenRelativeTo'
        end

        def base_column(case_node, base)
          case base
          when :case then case_node.location.keyword.column
          when :end  then case_node.location.end.column
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/case_indentation.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/case_indentation.rb
rubocop-0.26.1 lib/rubocop/cop/style/case_indentation.rb
rubocop-0.26.0 lib/rubocop/cop/style/case_indentation.rb
rubocop-0.25.0 lib/rubocop/cop/style/case_indentation.rb
rubocop-0.24.1 lib/rubocop/cop/style/case_indentation.rb
rubocop-0.24.0 lib/rubocop/cop/style/case_indentation.rb
rubocop-0.23.0 lib/rubocop/cop/style/case_indentation.rb