Sha256: 9bc1def9048c7a4e099116bc768f827db0d92557199979e9b1038baf3a7bd450

Contents?: true

Size: 1.95 KB

Versions: 24

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Layout
      # 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
      #   something'hello'
      #
      #   # good
      #   something x
      #   something y, z
      #   something 'hello'
      #
      class SpaceBeforeFirstArg < Cop
        include PrecedingFollowingAlignment
        include RangeHelp

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

        def on_send(node)
          return unless regular_method_call_with_arguments?(node)
          return unless expect_params_after_method_name?(node)

          first_arg = node.first_argument.source_range
          first_arg_with_space = range_with_surrounding_space(range: first_arg,
                                                              side: :left)
          space = range_between(first_arg_with_space.begin_pos,
                                first_arg.begin_pos)

          add_offense(space, location: space) if space.length != 1
        end

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

        private

        def regular_method_call_with_arguments?(node)
          node.arguments? && !node.operator_method? && !node.setter_method?
        end

        def expect_params_after_method_name?(node)
          return false if node.parenthesized?

          first_arg = node.first_argument

          same_line?(first_arg, node) &&
            !(allow_for_alignment? &&
              aligned_with_something?(first_arg.source_range))
        end
      end
    end
  end
end

Version data entries

24 entries across 22 versions & 3 rubygems

Version Path
rubocop-0.63.1 lib/rubocop/cop/layout/space_before_first_arg.rb
rubocop-0.63.0 lib/rubocop/cop/layout/space_before_first_arg.rb
rubocop-0.62.0 lib/rubocop/cop/layout/space_before_first_arg.rb
rubocop-0.61.1 lib/rubocop/cop/layout/space_before_first_arg.rb
rubocop-0.61.0 lib/rubocop/cop/layout/space_before_first_arg.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/layout/space_before_first_arg.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/layout/space_before_first_arg.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/layout/space_before_first_arg.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/layout/space_before_first_arg.rb
rubocop-0.60.0 lib/rubocop/cop/layout/space_before_first_arg.rb
rubocop-0.59.2 lib/rubocop/cop/layout/space_before_first_arg.rb
rubocop-0.59.1 lib/rubocop/cop/layout/space_before_first_arg.rb
rubocop-0.59.0 lib/rubocop/cop/layout/space_before_first_arg.rb
rubocop-0.58.2 lib/rubocop/cop/layout/space_before_first_arg.rb
rubocop-0.58.1 lib/rubocop/cop/layout/space_before_first_arg.rb
rubocop-0.58.0 lib/rubocop/cop/layout/space_before_first_arg.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/layout/space_before_first_arg.rb
rubocop-0.57.2 lib/rubocop/cop/layout/space_before_first_arg.rb
rubocop-0.57.1 lib/rubocop/cop/layout/space_before_first_arg.rb
rubocop-0.57.0 lib/rubocop/cop/layout/space_before_first_arg.rb