Sha256: 1dea1fea7454ab2d2bdb37c07436c2a4efa699577ef529832abf4052bf5c0fbf

Contents?: true

Size: 1.64 KB

Versions: 5

Compression:

Stored size: 1.64 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module Lint
      # This cop checks whether the end keywords of method definitions are
      # aligned properly.
      #
      # Two modes are supported through the AlignWith configuration
      # parameter. If it's set to `start_of_line` (which is the default), the
      # `end` shall be aligned with the start of the line where the `def`
      # keyword is. If it's set to `def`, the `end` shall be aligned with the
      # `def` keyword.
      #
      # @example
      #
      #   private def foo
      #   end
      class DefEndAlignment < Cop
        include OnMethod
        include EndKeywordAlignment

        MSG = '`end` at %d, %d is not aligned with `%s` at %d, %d'

        def on_method(node, _method_name, _args, _body)
          check_offset_of_node(node)
        end

        def on_send(node)
          receiver, method_name, *args = *node
          return unless visibility_and_def_on_same_line?(receiver, method_name,
                                                         args)

          method_def = args.first
          if style == :start_of_line
            expr = node.loc.expression
            range = Parser::Source::Range.new(expr.source_buffer,
                                              expr.begin_pos,
                                              method_def.loc.keyword.end_pos)
            check_offset(method_def, range.source,
                         method_def.loc.keyword.begin_pos - expr.begin_pos)
          else
            check_offset(method_def, 'def', 0)
          end

          ignore_node(method_def) # Don't check the same `end` again.
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/lint/def_end_alignment.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/lint/def_end_alignment.rb
rubocop-0.26.1 lib/rubocop/cop/lint/def_end_alignment.rb
rubocop-0.26.0 lib/rubocop/cop/lint/def_end_alignment.rb
rubocop-0.25.0 lib/rubocop/cop/lint/def_end_alignment.rb