Sha256: b3f2d0dd43573c437f56ef00bd68ab7dd1c8cea93d4ea7e90f0890286b290820

Contents?: true

Size: 1.35 KB

Versions: 8

Compression:

Stored size: 1.35 KB

Contents

# encoding: utf-8

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.'

        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.loc.expression.end_pos)
          add_offense(nil, range)
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.26.1 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.26.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.25.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.24.1 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.24.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.23.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb