Sha256: ea6a3ebeed0a8bf70b627f4132d903c84d0f2175f8178b2304837429d9cede22

Contents?: true

Size: 1.18 KB

Versions: 32

Compression:

Stored size: 1.18 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 < 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

32 entries across 32 versions & 2 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/multiline_block_chain.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/multiline_block_chain.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/multiline_block_chain.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/multiline_block_chain.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/multiline_block_chain.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-1.12.1 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-1.12.0 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-1.11.0 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-1.10.0 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-1.9.1 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-1.9.0 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-1.8.1 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-1.8.0 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-1.7.0 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-1.6.1 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-1.6.0 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-1.5.2 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-1.5.1 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-1.5.0 lib/rubocop/cop/style/multiline_block_chain.rb