Sha256: 6e186deb5dab62f6a04492795b1f8aa45548027619f2282018bcf29b33c4651d

Contents?: true

Size: 1.89 KB

Versions: 6

Compression:

Stored size: 1.89 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 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 = 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

        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

6 entries across 6 versions & 2 rubygems

Version Path
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/space_before_first_arg.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/space_before_first_arg.rb
rubocop-0.42.0 lib/rubocop/cop/style/space_before_first_arg.rb
rubocop-0.41.2 lib/rubocop/cop/style/space_before_first_arg.rb
rubocop-0.41.1 lib/rubocop/cop/style/space_before_first_arg.rb
rubocop-0.41.0 lib/rubocop/cop/style/space_before_first_arg.rb