Sha256: 9935d5294a7379a84f4dfce2dd30c3cca52915f68de6dc256d6ed9e946d5d276

Contents?: true

Size: 1.8 KB

Versions: 11

Compression:

Stored size: 1.8 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 < 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.begin,
                                         side: :left)

          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? ||
            allowed?(send_node)
        end

        def allowed?(send_node)
          send_node.parent.arguments.one? &&
            allowed_methods.include?(send_node.method_name.to_s) &&
            send_node.arguments.one?
        end

        def allowed_methods
          cop_config['AllowedMethods'] || []
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 3 rubygems

Version Path
rubocop-0.86.0 lib/rubocop/cop/style/nested_parenthesized_calls.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/style/nested_parenthesized_calls.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/style/nested_parenthesized_calls.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-0.85.1 lib/rubocop/cop/style/nested_parenthesized_calls.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-0.85.0 lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-0.84.0 lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-0.83.0 lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-0.82.0 lib/rubocop/cop/style/nested_parenthesized_calls.rb
rubocop-0.81.0 lib/rubocop/cop/style/nested_parenthesized_calls.rb