Sha256: 54a4e5a8f0e6f2c53eb06e7821613bddd0791b1bae4971a523ed0275af44576a

Contents?: true

Size: 1.85 KB

Versions: 13

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for unparenthesized method calls in the argument list
      # of a parenthesized method call.
      #
      # @example
      #   @good
      #   method1(method2(arg), method3(arg))
      #
      #   @bad
      #   method1(method2 arg, method3, arg)
      class NestedParenthesizedCalls < Cop
        MSG = 'Add parentheses to nested method call `%s`.'.freeze
        RSPEC_MATCHERS = [:be, :eq, :eql, :equal, :be_kind_of, :be_instance_of,
                          :respond_to, :be_between, :match, :be_within,
                          :start_with, :end_with, :include, :raise_error].freeze

        def on_send(node)
          return unless parenthesized_call?(node)

          node.each_child_node(:send) do |nested|
            next if nested.method_args.empty? ||
                    parenthesized_call?(nested) ||
                    operator?(nested.method_name) ||
                    rspec_matcher?(node, nested) ||
                    nested.asgn_method_call?
            add_offense(nested, nested.source_range, format(MSG, nested.source))
          end
        end

        private

        def rspec_matcher?(parent, send)
          parent.method_args.one? && # .to, .not_to, etc
            RSPEC_MATCHERS.include?(send.method_name) &&
            send.method_args.one?
        end

        def autocorrect(nested)
          _scope, _method_name, *args = *nested

          first_arg = args.first.source_range
          last_arg = args.last.source_range

          first_arg_with_space = range_with_surrounding_space(first_arg, :left)
          leading_space = first_arg_with_space.begin.resize(1)

          lambda do |corrector|
            corrector.replace(leading_space, '(')
            corrector.insert_after(last_arg, ')')
          end
        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/nested_parenthesized_calls.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-0.47.1 lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-0.47.0 lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-0.46.0 lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-0.45.0 lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-0.44.1 lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-0.44.0 lib/rubocop/cop/style/nested_parenthesized_calls.rb