Sha256: c961d5be051d98cb8aa342e6c34933303d6bfb1826b35ffc66a5f83824c3e0b2

Contents?: true

Size: 1.2 KB

Versions: 9

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for chaining of a block after another block that spans
      # multiple lines.
      #
      # @example
      #
      #   # bad
      #   Thread.list.select do |t|
      #     t.alive?
      #   end.map do |t|
      #     t.object_id
      #   end
      #
      #   # good
      #   alive_threads = Thread.list.select do |t|
      #     t.alive?
      #   end
      #   alive_threads.map do |t|
      #     t.object_id
      #   end
      class MultilineBlockChain < Cop
        include RangeHelp

        MSG = 'Avoid multi-line chains of blocks.'

        def on_block(node)
          node.send_node.each_node(:send) do |send_node|
            receiver = send_node.receiver

            next unless receiver&.block_type? && receiver&.multiline?

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

            add_offense(nil, location: range)

            # Done. If there are more blocks in the chain, they will be
            # found by subsequent calls to on_block.
            break
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 3 rubygems

Version Path
rubocop-0.91.0 lib/rubocop/cop/style/multiline_block_chain.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.90.0/lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-0.90.0 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-0.89.1 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-0.89.0 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-0.88.0 lib/rubocop/cop/style/multiline_block_chain.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-0.87.1 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-0.87.0 lib/rubocop/cop/style/multiline_block_chain.rb