Sha256: ef00ac5fa688eb574fa2b7e30eb1cdb6074dae1c3d2873952f5de50c51478df7

Contents?: true

Size: 1.86 KB

Versions: 6809

Compression:

Stored size: 1.86 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`.'.freeze

        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

6,809 entries across 6,805 versions & 24 rubygems

Version Path
cybrid_api_organization_ruby-0.123.162 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
cybrid_api_id_ruby-0.123.162 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
cybrid_api_id_ruby-0.123.161 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
cybrid_api_organization_ruby-0.123.161 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
cybrid_api_organization_ruby-0.123.160 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
cybrid_api_id_ruby-0.123.160 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
cybrid_api_organization_ruby-0.123.159 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
cybrid_api_id_ruby-0.123.159 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
cybrid_api_id_ruby-0.123.158 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
cybrid_api_organization_ruby-0.123.158 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
cybrid_api_id_ruby-0.123.157 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
cybrid_api_organization_ruby-0.123.157 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
cybrid_api_organization_ruby-0.123.156 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
cybrid_api_id_ruby-0.123.156 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
messente_api-2.3.0 vendor/bundle/ruby/3.4.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
cybrid_api_bank_ruby-0.123.153 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
cybrid_api_id_ruby-0.123.153 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
cybrid_api_organization_ruby-0.123.153 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
cybrid_api_bank_ruby-0.123.152 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb
cybrid_api_id_ruby-0.123.152 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/nested_parenthesized_calls.rb