Sha256: 570c9a5c99fbbbdb86cb95db42b93c3a281a3466000a7bcf431e65d0a85f3418

Contents?: true

Size: 1.45 KB

Versions: 4

Compression:

Stored size: 1.45 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # This cop checks the . position in multi-line method calls.
      class DotPosition < Cop
        include ConfigurableEnforcedStyle

        def on_send(node)
          return unless node.loc.dot

          if proper_dot_position?(node)
            correct_style_detected
          else
            add_offense(node, :dot) { opposite_style_detected }
          end
        end

        private

        def message(node)
          'Place the . on the ' +
            case style
            when :leading
              'next line, together with the method name.'
            when :trailing
              'previous line, together with the method call receiver.'
            end
        end

        def proper_dot_position?(node)
          receiver, _method_name, *_args = *node

          receiver_line = receiver.loc.expression.end.line

          if node.loc.selector
            selector_line = node.loc.selector.line
          else
            # l.(1) has no selector, so we use the opening parenthesis instead
            selector_line = node.loc.begin.line
          end

          # receiver and selector are on the same line
          return true if selector_line == receiver_line

          dot_line = node.loc.dot.line

          case style
          when :leading then dot_line == selector_line
          when :trailing then dot_line != selector_line
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-0.20.1 lib/rubocop/cop/style/dot_position.rb
rubocop-0.20.0 lib/rubocop/cop/style/dot_position.rb
rubocop-0.19.1 lib/rubocop/cop/style/dot_position.rb
rubocop-0.19.0 lib/rubocop/cop/style/dot_position.rb