Sha256: 24f635a99dcfd9b1af75695de0bac7ab21c8849342ef447f0cb09a3f6b34fcb8

Contents?: true

Size: 1.75 KB

Versions: 13

Compression:

Stored size: 1.75 KB

Contents

# 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 unless regular_method_call_with_params?(node)
          return unless expect_params_after_method_name?(node)

          _receiver, _method_name, *args = *node
          arg1 = args.first.source_range
          arg1_with_space = range_with_surrounding_space(arg1, :left)
          space = range_between(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

        private

        def regular_method_call_with_params?(node)
          _receiver, method_name, *args = *node

          !(args.empty? || operator?(method_name) || node.asgn_method_call?)
        end

        def expect_params_after_method_name?(node)
          return false if parentheses?(node)

          _receiver, _method_name, *args = *node
          arg1 = args.first.source_range

          arg1.line == node.loc.line &&
            !(allow_for_alignment? && aligned_with_something?(arg1))
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/space_before_first_arg.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/space_before_first_arg.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/space_before_first_arg.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/space_before_first_arg.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/space_before_first_arg.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/space_before_first_arg.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/space_before_first_arg.rb
rubocop-0.47.1 lib/rubocop/cop/style/space_before_first_arg.rb
rubocop-0.47.0 lib/rubocop/cop/style/space_before_first_arg.rb
rubocop-0.46.0 lib/rubocop/cop/style/space_before_first_arg.rb
rubocop-0.45.0 lib/rubocop/cop/style/space_before_first_arg.rb
rubocop-0.44.1 lib/rubocop/cop/style/space_before_first_arg.rb
rubocop-0.44.0 lib/rubocop/cop/style/space_before_first_arg.rb