Sha256: 55160dabeb3e6449d03bf4d8e86b824c343659483a5b8812ef83086ed9a98b92

Contents?: true

Size: 1.62 KB

Versions: 24

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # Checks for space between the name of a called method and a left
      # parenthesis.
      #
      # @example
      #
      #   # bad
      #
      #   puts (x + y)
      #
      # @example
      #
      #   # good
      #
      #   puts(x + y)
      class ParenthesesAsGroupedExpression < Cop
        include RangeHelp

        MSG = '`(...)` interpreted as grouped expression.'.freeze

        def on_send(node)
          return unless node.arguments.one?
          return if node.operator_method? || node.setter_method?

          return unless node.first_argument.source.start_with?('(')

          space_length = spaces_before_left_parenthesis(node)
          return unless space_length > 0

          range = space_range(node.first_argument.source_range, space_length)

          add_offense(nil, location: range)
        end

        private

        def spaces_before_left_parenthesis(node)
          receiver = node.receiver
          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(node.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

24 entries across 22 versions & 3 rubygems

Version Path
rubocop-0.63.1 lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb
rubocop-0.63.0 lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb
rubocop-0.62.0 lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb
rubocop-0.61.1 lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb
rubocop-0.61.0 lib/rubocop/cop/lint/parentheses_as_grouped_expression.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/lint/parentheses_as_grouped_expression.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/lint/parentheses_as_grouped_expression.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/lint/parentheses_as_grouped_expression.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb
rubocop-0.60.0 lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb
rubocop-0.59.2 lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb
rubocop-0.59.1 lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb
rubocop-0.59.0 lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb
rubocop-0.58.2 lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb
rubocop-0.58.1 lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb
rubocop-0.58.0 lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb
rubocop-0.57.2 lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb
rubocop-0.57.1 lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb
rubocop-0.57.0 lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb