Sha256: 124d6fdacb05f1944e63d9fe2ef17e9a0be9a04d7c508f8f0f939b1a3a572f19

Contents?: true

Size: 1.85 KB

Versions: 36

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
        include RangeHelp

        MSG = 'Add parentheses to nested method call `%<source>s`.'

        def on_send(node)
          return unless node.parenthesized?

          node.each_child_node(:send, :csend) do |nested|
            next if allowed_omission?(nested)

            add_offense(nested,
                        location: nested.source_range,
                        message: format(MSG, source: nested.source))
          end
        end
        alias on_csend on_send

        def autocorrect(nested)
          first_arg = nested.first_argument.source_range
          last_arg = nested.last_argument.source_range

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

          lambda do |corrector|
            corrector.replace(leading_space, '(')
            corrector.insert_after(last_arg, ')')
          end
        end

        private

        def allowed_omission?(send_node)
          !send_node.arguments? || send_node.parenthesized? ||
            send_node.setter_method? || send_node.operator_method? ||
            whitelisted?(send_node)
        end

        def whitelisted?(send_node)
          send_node.parent.arguments.one? &&
            whitelisted_methods.include?(send_node.method_name.to_s) &&
            send_node.arguments.one?
        end

        def whitelisted_methods
          cop_config['Whitelist'] || []
        end
      end
    end
  end
end

Version data entries

36 entries across 19 versions & 2 rubygems

Version Path
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.1/lib/rubocop/cop/style/nested_parenthesized_calls.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.76.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.76.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.75.1/lib/rubocop/cop/style/nested_parenthesized_calls.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-0.76.0 lib/rubocop/cop/style/nested_parenthesized_calls.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/rubocop-0.75.1/lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-0.75.1 lib/rubocop/cop/style/nested_parenthesized_calls.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-0.75.0 lib/rubocop/cop/style/nested_parenthesized_calls.rb