Sha256: 09e64ad50ce48f9d548eaf5b74c929a033344120da0eb9245ccc09a3b0f4d334

Contents?: true

Size: 1.4 KB

Versions: 13

Compression:

Stored size: 1.4 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for methods called on a do...end block. The point of
      # this check is that it's easy to miss the call tacked on to the block
      # when reading code.
      #
      # @example
      #
      #   a do
      #     b
      #   end.c
      class MethodCalledOnDoEndBlock < Cop
        MSG = 'Avoid chaining a method call on a do...end block.'.freeze

        def on_block(node)
          method, _args, _body = *node
          # If the method that is chained on the do...end block is itself a
          # method with a block, we allow it. It's pretty safe to assume that
          # these calls are not missed by anyone reading code. We also want to
          # avoid double reporting of offenses checked by the
          # MultilineBlockChain cop.
          ignore_node(method)
        end

        def on_send(node)
          return if ignored_node?(node)
          receiver, _method_name, *_args = *node
          return unless receiver && receiver.type == :block &&
                        receiver.loc.end.is?('end')

          range = Parser::Source::Range.new(receiver.loc.end.source_buffer,
                                            receiver.loc.end.begin_pos,
                                            node.source_range.end_pos)
          add_offense(nil, range)
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/method_called_on_do_end_block.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.42.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.41.2 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.41.1 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.41.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.40.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.39.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.38.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.37.2 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.37.1 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.37.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.36.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb