Sha256: 5530fbabb35afddd9c4789e79049da0d20befc830e675fea419a6ef84727c11c

Contents?: true

Size: 1.51 KB

Versions: 28

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # Sort globbed results by default in Ruby 3.0.
      # This cop checks for redundant `sort` method to `Dir.glob` and `Dir[]`.
      #
      # @safety
      #   This cop is unsafe, in case of having a file and a directory with
      #   identical names, since directory will be loaded before the file, which
      #   will break `exe/files.rb` that rely on `exe.rb` file.
      #
      # @example
      #
      #   # bad
      #   Dir.glob('./lib/**/*.rb').sort.each do |file|
      #   end
      #
      #   Dir['./lib/**/*.rb'].sort.each do |file|
      #   end
      #
      #   # good
      #   Dir.glob('./lib/**/*.rb').each do |file|
      #   end
      #
      #   Dir['./lib/**/*.rb'].each do |file|
      #   end
      #
      class RedundantDirGlobSort < Base
        extend AutoCorrector
        extend TargetRubyVersion

        minimum_target_ruby_version 3.0

        MSG = 'Remove redundant `sort`.'
        RESTRICT_ON_SEND = %i[sort].freeze
        GLOB_METHODS = %i[glob []].freeze

        def on_send(node)
          return unless (receiver = node.receiver)
          return unless receiver.receiver&.const_type? && receiver.receiver.short_name == :Dir
          return unless GLOB_METHODS.include?(receiver.method_name)

          selector = node.loc.selector

          add_offense(selector) do |corrector|
            corrector.remove(selector)
            corrector.remove(node.loc.dot)
          end
        end
      end
    end
  end
end

Version data entries

28 entries across 24 versions & 3 rubygems

Version Path
rubocop-1.30.0 lib/rubocop/cop/lint/redundant_dir_glob_sort.rb
rubocop-1.29.1 lib/rubocop/cop/lint/redundant_dir_glob_sort.rb
rubocop-1.29.0 lib/rubocop/cop/lint/redundant_dir_glob_sort.rb
rubocop-1.28.2 lib/rubocop/cop/lint/redundant_dir_glob_sort.rb
rubocop-1.28.1 lib/rubocop/cop/lint/redundant_dir_glob_sort.rb
rubocop-1.28.0 lib/rubocop/cop/lint/redundant_dir_glob_sort.rb
rubocop-1.27.0 lib/rubocop/cop/lint/redundant_dir_glob_sort.rb
rubocop-1.26.1 lib/rubocop/cop/lint/redundant_dir_glob_sort.rb