Sha256: 0764089235dfa9d219531fd7bf5a3b6bb28e90803740f626c2a78ec99ad05d6e

Contents?: true

Size: 1.88 KB

Versions: 27

Compression:

Stored size: 1.88 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))
      #
      #   # bad
      #   method1(method2 arg)
      class NestedParenthesizedCalls < Base
        include RangeHelp
        include AllowedMethods
        extend AutoCorrector

        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)

            message = format(MSG, source: nested.source)
            add_offense(nested.source_range, message: message) do |corrector|
              autocorrect(corrector, nested)
            end
          end
        end
        alias on_csend on_send

        private

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

          leading_space =
            range_with_surrounding_space(range: first_arg.begin,
                                         side: :left,
                                         whitespace: true,
                                         continuations: true)

          corrector.replace(leading_space, '(')
          corrector.insert_after(last_arg, ')')
        end

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

        def allowed?(send_node)
          send_node.parent.arguments.one? &&
            allowed_method?(send_node.method_name) &&
            send_node.arguments.one?
        end
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/nested_parenthesized_calls.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/nested_parenthesized_calls.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/nested_parenthesized_calls.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/nested_parenthesized_calls.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/nested_parenthesized_calls.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-1.7.0 lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-1.6.1 lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-1.6.0 lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-1.5.2 lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-1.5.1 lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-1.5.0 lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-1.4.2 lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-1.4.1 lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-1.4.0 lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-1.3.1 lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-1.3.0 lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-1.2.0 lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-1.1.0 lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-1.0.0 lib/rubocop/cop/style/nested_parenthesized_calls.rb