Sha256: b501e2e7ce388c9ef405a576c6eb9e3ceb2ed0b67d8a5ca8618814c460bdc75e

Contents?: true

Size: 1.52 KB

Versions: 11

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # Checks for space between a the name of a called method and a left
      # parenthesis.
      #
      # @example
      #
      #   puts (x + y)
      class ParenthesesAsGroupedExpression < Cop
        MSG = '`(...)` interpreted as grouped expression.'.freeze

        def on_send(node)
          _receiver, method_name, *args = *node
          return unless args.one?
          return if operator?(method_name) || node.asgn_method_call?

          first_arg = args.first
          return unless first_arg.source.start_with?('(')

          space_length = spaces_before_left_parenthesis(node)
          return unless space_length > 0

          add_offense(nil, space_range(first_arg.source_range, space_length))
        end

        private

        def spaces_before_left_parenthesis(node)
          receiver, method_name, _args = *node
          receiver_length = if receiver
                              receiver.source.length
                            else
                              0
                            end
          without_receiver = node.source[receiver_length..-1]

          # Escape question mark if any.
          method_regexp = Regexp.escape(method_name)

          match = without_receiver.match(/^\s*\.?\s*#{method_regexp}(\s+)\(/)
          match ? match.captures[0].length : 0
        end

        def space_range(expr, space_length)
          range_between(expr.begin_pos - space_length, expr.begin_pos)
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

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