Sha256: 47f0b76191d60b5b1caee174df8025fb19c275d98296046c6ba9694b941d2dc2

Contents?: true

Size: 1.14 KB

Versions: 11

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # 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 < Base
        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(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

11 entries across 11 versions & 2 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/style/multiline_block_chain.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-1.34.1 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-1.34.0 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-1.33.0 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-1.32.0 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-1.31.2 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-1.31.1 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-1.31.0 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-1.30.1 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-1.30.0 lib/rubocop/cop/style/multiline_block_chain.rb