Sha256: e23f06fdd94e7b6ca4e1c301ac2bf8d169330d88cdabfc0cd091b2654cdc769c

Contents?: true

Size: 1.53 KB

Versions: 7

Compression:

Stored size: 1.53 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

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.
      #
      # Alternatively, extra spaces can be added to align the argument with
      # something on a preceding or following line, if the AllowForAlignment
      # config parameter is true.
      #
      # @example
      #   @bad
      #   something  x
      #   something   y, z
      #
      class SpaceBeforeFirstArg < Cop
        include PrecedingFollowingAlignment

        MSG = 'Put one space between the method name and ' \
              'the first argument.'.freeze

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

          _receiver, method_name, *args = *node
          return if args.empty?
          return if operator?(method_name)
          return if node.asgn_method_call?

          arg1 = args.first.source_range
          return if arg1.line > node.loc.line
          return if allow_for_alignment? && aligned_with_something?(arg1)

          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.40.0 lib/rubocop/cop/style/space_before_first_arg.rb
rubocop-0.39.0 lib/rubocop/cop/style/space_before_first_arg.rb
rubocop-0.38.0 lib/rubocop/cop/style/space_before_first_arg.rb
rubocop-0.37.2 lib/rubocop/cop/style/space_before_first_arg.rb
rubocop-0.37.1 lib/rubocop/cop/style/space_before_first_arg.rb
rubocop-0.37.0 lib/rubocop/cop/style/space_before_first_arg.rb
rubocop-0.36.0 lib/rubocop/cop/style/space_before_first_arg.rb