Sha256: d59d0e1ec574c3047b2f66a5583a617df92367b852c2467573d1be6c7386668a

Contents?: true

Size: 1.62 KB

Versions: 145

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Prefer to use `Dir.empty?('path/to/dir')` when checking if a directory is empty.
      #
      # @example
      #   # bad
      #   Dir.entries('path/to/dir').size == 2
      #   Dir.children('path/to/dir').empty?
      #   Dir.children('path/to/dir').size == 0
      #   Dir.each_child('path/to/dir').none?
      #
      #   # good
      #   Dir.empty?('path/to/dir')
      #
      class DirEmpty < Base
        extend AutoCorrector
        extend TargetRubyVersion

        MSG = 'Use `%<replacement>s` instead.'
        RESTRICT_ON_SEND = %i[== != > empty? none?].freeze

        minimum_target_ruby_version 2.4

        # @!method offensive?(node)
        def_node_matcher :offensive?, <<~PATTERN
          {
            (send (send (send $(const {nil? cbase} :Dir) :entries $_) :size) {:== :!= :>} (int 2))
            (send (send (send $(const {nil? cbase} :Dir) :children $_) :size) {:== :!= :>} (int 0))
            (send (send $(const {nil? cbase} :Dir) :children $_) :empty?)
            (send (send $(const {nil? cbase} :Dir) :each_child $_) :none?)
          }
        PATTERN

        def on_send(node)
          offensive?(node) do |const_node, arg_node|
            replacement = "#{bang(node)}#{const_node.source}.empty?(#{arg_node.source})"
            add_offense(node, message: format(MSG, replacement: replacement)) do |corrector|
              corrector.replace(node, replacement)
            end
          end
        end

        private

        def bang(node)
          '!' if %i[!= >].include? node.method_name
        end
      end
    end
  end
end

Version data entries

145 entries across 144 versions & 14 rubygems

Version Path
rubocop-1.71.0 lib/rubocop/cop/style/dir_empty.rb
rubocop-1.70.0 lib/rubocop/cop/style/dir_empty.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/dir_empty.rb
rubocop-1.69.2 lib/rubocop/cop/style/dir_empty.rb
rubocop-1.69.1 lib/rubocop/cop/style/dir_empty.rb
rubocop-1.69.0 lib/rubocop/cop/style/dir_empty.rb
rubocop-1.68.0 lib/rubocop/cop/style/dir_empty.rb
rubocop-1.67.0 lib/rubocop/cop/style/dir_empty.rb
rubocop-1.66.1 lib/rubocop/cop/style/dir_empty.rb
rubocop-1.66.0 lib/rubocop/cop/style/dir_empty.rb
rubocop-1.65.1 lib/rubocop/cop/style/dir_empty.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/dir_empty.rb
rubocop-1.65.0 lib/rubocop/cop/style/dir_empty.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/dir_empty.rb
rubocop-1.64.1 lib/rubocop/cop/style/dir_empty.rb
rubocop-1.63.4 lib/rubocop/cop/style/dir_empty.rb
rubocop-1.63.3 lib/rubocop/cop/style/dir_empty.rb
rubocop-1.63.2 lib/rubocop/cop/style/dir_empty.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/dir_empty.rb
rubocop-1.63.1 lib/rubocop/cop/style/dir_empty.rb