Sha256: e33d811a8c5f5a24197e6e45ca70ae876d8cc3fe845e937e24649400a0582353

Contents?: true

Size: 1.44 KB

Versions: 176

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # 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
      #   # bad
      #   a do
      #     b
      #   end.c
      #
      #   # good
      #   a { b }.c
      #
      #   # good
      #   foo = a do
      #     b
      #   end
      #   foo.c
      class MethodCalledOnDoEndBlock < Base
        include RangeHelp

        MSG = 'Avoid chaining a method call on a do...end block.'

        def on_block(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(node.send_node)
        end

        alias on_numblock on_block

        def on_send(node)
          return if ignored_node?(node)

          receiver = node.receiver

          return unless (receiver&.block_type? || receiver&.numblock_type?) &&
                        receiver.loc.end.is?('end')

          range = range_between(receiver.loc.end.begin_pos, node.source_range.end_pos)

          add_offense(range)
        end
        alias on_csend on_send
      end
    end
  end
end

Version data entries

176 entries across 171 versions & 17 rubygems

Version Path
harbr-0.1.84 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/method_called_on_do_end_block.rb
harbr-0.1.83 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/method_called_on_do_end_block.rb
harbr-0.1.82 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/method_called_on_do_end_block.rb
harbr-0.1.81 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/method_called_on_do_end_block.rb
harbr-0.1.80 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/method_called_on_do_end_block.rb
harbr-0.1.79 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/method_called_on_do_end_block.rb
harbr-0.1.78 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/method_called_on_do_end_block.rb
harbr-0.1.77 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/method_called_on_do_end_block.rb
harbr-0.1.76 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/method_called_on_do_end_block.rb
harbr-0.1.75 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/method_called_on_do_end_block.rb
harbr-0.1.74 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/method_called_on_do_end_block.rb
harbr-0.1.73 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/method_called_on_do_end_block.rb
harbr-0.1.72 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/method_called_on_do_end_block.rb
harbr-0.1.71 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/method_called_on_do_end_block.rb
harbr-0.1.70 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/method_called_on_do_end_block.rb
harbr-0.1.69 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/method_called_on_do_end_block.rb
harbr-0.1.68 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/method_called_on_do_end_block.rb
harbr-0.1.67 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/method_called_on_do_end_block.rb
harbr-0.1.66 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/method_called_on_do_end_block.rb
harbr-0.1.65 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/method_called_on_do_end_block.rb