Sha256: e8cb70b659e3af04684645519b6a3e0b71a9179fb57eb774b1e516bbd921654c

Contents?: true

Size: 1.18 KB

Versions: 7

Compression:

Stored size: 1.18 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module Style
      # Checks that exactly one space is used between a method name and the
      # first argument for method calls without parentheses.
      #
      # @example
      #
      #   something  x
      #   something   y, z
      #
      class SingleSpaceBeforeFirstArg < Cop
        MSG = 'Put one space between the method name and the first argument.'

        def on_send(node)
          return if parentheses?(node)

          _receiver, method_name, *args = *node
          return if args.empty?
          return if operator?(method_name)
          return if method_name.to_s.end_with?('=')

          arg1 = args.first.loc.expression
          return if arg1.line > node.loc.line

          arg1_with_space = range_with_surrounding_space(arg1, :left)
          space = Parser::Source::Range.new(arg1.source_buffer,
                                            arg1_with_space.begin_pos,
                                            arg1.begin_pos)
          add_offense(space, space) if space.length > 1
        end

        def autocorrect(range)
          ->(corrector) { corrector.replace(range, ' ') }
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rubocop-0.34.2 lib/rubocop/cop/style/single_space_before_first_arg.rb
rubocop-0.34.1 lib/rubocop/cop/style/single_space_before_first_arg.rb
rubocop-0.34.0 lib/rubocop/cop/style/single_space_before_first_arg.rb
rubocop-0.33.0 lib/rubocop/cop/style/single_space_before_first_arg.rb
rubocop-0.32.1 lib/rubocop/cop/style/single_space_before_first_arg.rb
rubocop-0.32.0 lib/rubocop/cop/style/single_space_before_first_arg.rb
rubocop-0.31.0 lib/rubocop/cop/style/single_space_before_first_arg.rb