Sha256: 79c7466da6e0a6ca4ea97b7a9eab1a4dca4338d1305f8e1aa8a8222ee0ac7e8d

Contents?: true

Size: 1.35 KB

Versions: 13

Compression:

Stored size: 1.35 KB

Contents

# encoding: utf-8
# 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
      #
      #   Thread.list.find_all do |t|
      #     t.alive?
      #   end.map do |t|
      #     t.object_id
      #   end
      class MultilineBlockChain < Cop
        MSG = 'Avoid multi-line chains of blocks.'.freeze

        def on_block(node)
          method, _args, _body = *node
          method.each_node(:send) do |send_node|
            receiver, _method_name, *_args = *send_node
            next unless receiver && receiver.type == :block

            # The begin and end could also be braces, but we call the
            # variables do... and end...
            do_kw_loc = receiver.loc.begin
            end_kw_loc = receiver.loc.end
            next if do_kw_loc.line == end_kw_loc.line

            range =
              Parser::Source::Range.new(end_kw_loc.source_buffer,
                                        end_kw_loc.begin_pos,
                                        method.source_range.end_pos)
            add_offense(nil, 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

13 entries across 13 versions & 2 rubygems

Version Path
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/multiline_block_chain.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-0.42.0 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-0.41.2 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-0.41.1 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-0.41.0 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-0.40.0 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-0.39.0 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-0.38.0 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-0.37.2 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-0.37.1 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-0.37.0 lib/rubocop/cop/style/multiline_block_chain.rb
rubocop-0.36.0 lib/rubocop/cop/style/multiline_block_chain.rb