Sha256: caf3eb032c6da6b8756c0d000a80d2c525d79403f21177957f15ccb72cf0d678

Contents?: true

Size: 1.59 KB

Versions: 30

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # Emulates the following Ruby warning in Ruby 3.3.
      #
      # [source,ruby]
      # ----
      # $ ruby -e '0.times { it }'
      # -e:1: warning: `it` calls without arguments will refer to the first block param in Ruby 3.4;
      # use it() or self.it
      # ----
      #
      # `it` calls without arguments will refer to the first block param in Ruby 3.4.
      # So use `it()` or `self.it` to ensure compatibility.
      #
      # @example
      #
      #   # bad
      #   do_something { it }
      #
      #   # good
      #   do_something { it() }
      #   do_something { self.it }
      #
      class ItWithoutArgumentsInBlock < Base
        include NodePattern::Macros

        MSG = '`it` calls without arguments will refer to the first block param in Ruby 3.4; ' \
              'use `it()` or `self.it`.'

        def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
          return unless (body = node.body)
          return unless node.arguments.empty_and_without_delimiters?

          if body.send_type? && deprecated_it_method?(body)
            add_offense(body)
          else
            body.each_descendant(:send).each do |send_node|
              next unless deprecated_it_method?(send_node)

              add_offense(send_node)
            end
          end
        end

        def deprecated_it_method?(node)
          return false unless node.method?(:it)

          !node.receiver && node.arguments.empty? && !node.parenthesized? && !node.block_literal?
        end
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 7 rubygems

Version Path
rubocop-1.66.1 lib/rubocop/cop/lint/it_without_arguments_in_block.rb
rubocop-1.66.0 lib/rubocop/cop/lint/it_without_arguments_in_block.rb
rubocop-1.65.1 lib/rubocop/cop/lint/it_without_arguments_in_block.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/it_without_arguments_in_block.rb
rubocop-1.65.0 lib/rubocop/cop/lint/it_without_arguments_in_block.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/it_without_arguments_in_block.rb
rubocop-1.64.1 lib/rubocop/cop/lint/it_without_arguments_in_block.rb
rubocop-1.63.4 lib/rubocop/cop/lint/it_without_arguments_in_block.rb
rubocop-1.63.3 lib/rubocop/cop/lint/it_without_arguments_in_block.rb
rubocop-1.63.2 lib/rubocop/cop/lint/it_without_arguments_in_block.rb
rubocop-1.63.1 lib/rubocop/cop/lint/it_without_arguments_in_block.rb
rubocop-1.63.0 lib/rubocop/cop/lint/it_without_arguments_in_block.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/lint/it_without_arguments_in_block.rb
rubocop-1.62.1 lib/rubocop/cop/lint/it_without_arguments_in_block.rb
rubocop-1.62.0 lib/rubocop/cop/lint/it_without_arguments_in_block.rb
rubocop-1.61.0 lib/rubocop/cop/lint/it_without_arguments_in_block.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/lint/it_without_arguments_in_block.rb
rubocop-1.60.2 lib/rubocop/cop/lint/it_without_arguments_in_block.rb
study_line-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/lint/it_without_arguments_in_block.rb
study_line-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/lint/it_without_arguments_in_block.rb