Sha256: f565c5edb4e43d77a02949cd6a28b256e1274c505af5552238d96737f3f161be

Contents?: true

Size: 1.64 KB

Versions: 24

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop checks for ambiguous block association with method
      # when param passed without parentheses.
      #
      # @example
      #
      #   # bad
      #   some_method a { |val| puts val }
      #
      # @example
      #
      #   # good
      #   # With parentheses, there's no ambiguity.
      #   some_method(a) { |val| puts val }
      #
      #   # good
      #   # Operator methods require no disambiguation
      #   foo == bar { |b| b.baz }
      #
      #   # good
      #   # Lambda arguments require no disambiguation
      #   foo = ->(bar) { bar.baz }
      class AmbiguousBlockAssociation < Cop
        MSG = 'Parenthesize the param `%<param>s` to make sure that the ' \
              'block will be associated with the `%<method>s` method ' \
              'call.'.freeze

        def on_send(node)
          return if !node.arguments? || node.parenthesized? ||
                    node.last_argument.lambda? || allowed_method?(node)

          return unless ambiguous_block_association?(node)

          add_offense(node)
        end

        private

        def ambiguous_block_association?(send_node)
          send_node.last_argument.block_type? &&
            !send_node.last_argument.send_node.arguments?
        end

        def allowed_method?(node)
          node.assignment? || node.operator_method? || node.method?(:[])
        end

        def message(send_node)
          block_param = send_node.last_argument

          format(MSG, param: block_param.source,
                      method: block_param.send_node.source)
        end
      end
    end
  end
end

Version data entries

24 entries across 22 versions & 3 rubygems

Version Path
rubocop-0.63.1 lib/rubocop/cop/lint/ambiguous_block_association.rb
rubocop-0.63.0 lib/rubocop/cop/lint/ambiguous_block_association.rb
rubocop-0.62.0 lib/rubocop/cop/lint/ambiguous_block_association.rb
rubocop-0.61.1 lib/rubocop/cop/lint/ambiguous_block_association.rb
rubocop-0.61.0 lib/rubocop/cop/lint/ambiguous_block_association.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/lint/ambiguous_block_association.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/lint/ambiguous_block_association.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/lint/ambiguous_block_association.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/lint/ambiguous_block_association.rb
rubocop-0.60.0 lib/rubocop/cop/lint/ambiguous_block_association.rb
rubocop-0.59.2 lib/rubocop/cop/lint/ambiguous_block_association.rb
rubocop-0.59.1 lib/rubocop/cop/lint/ambiguous_block_association.rb
rubocop-0.59.0 lib/rubocop/cop/lint/ambiguous_block_association.rb
rubocop-0.58.2 lib/rubocop/cop/lint/ambiguous_block_association.rb
rubocop-0.58.1 lib/rubocop/cop/lint/ambiguous_block_association.rb
rubocop-0.58.0 lib/rubocop/cop/lint/ambiguous_block_association.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/lint/ambiguous_block_association.rb
rubocop-0.57.2 lib/rubocop/cop/lint/ambiguous_block_association.rb
rubocop-0.57.1 lib/rubocop/cop/lint/ambiguous_block_association.rb
rubocop-0.57.0 lib/rubocop/cop/lint/ambiguous_block_association.rb