Sha256: 943ab9a5dcf9bd20670b2081fc0344638c1ffeda6822800aa62103c3bdd55655

Contents?: true

Size: 1.27 KB

Versions: 34

Compression:

Stored size: 1.27 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[]`.
      #
      # @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

34 entries across 34 versions & 4 rubygems

Version Path
rubocop-1.25.1 lib/rubocop/cop/lint/redundant_dir_glob_sort.rb
rubocop-1.25.0 lib/rubocop/cop/lint/redundant_dir_glob_sort.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb
rubocop-1.24.1 lib/rubocop/cop/lint/redundant_dir_glob_sort.rb
rubocop-1.24.0 lib/rubocop/cop/lint/redundant_dir_glob_sort.rb
rubocop-1.23.0 lib/rubocop/cop/lint/redundant_dir_glob_sort.rb
rubocop-1.22.3 lib/rubocop/cop/lint/redundant_dir_glob_sort.rb
rubocop-1.22.2 lib/rubocop/cop/lint/redundant_dir_glob_sort.rb
rubocop-1.22.1 lib/rubocop/cop/lint/redundant_dir_glob_sort.rb
rubocop-1.22.0 lib/rubocop/cop/lint/redundant_dir_glob_sort.rb
rubocop-1.21.0 lib/rubocop/cop/lint/redundant_dir_glob_sort.rb
rubocop-1.20.0 lib/rubocop/cop/lint/redundant_dir_glob_sort.rb
rubocop-1.19.1 lib/rubocop/cop/lint/redundant_dir_glob_sort.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/rubocop-1.18.3/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb
rubocop-1.19.0 lib/rubocop/cop/lint/redundant_dir_glob_sort.rb
rubocop-1.18.4 lib/rubocop/cop/lint/redundant_dir_glob_sort.rb
rubocop-1.18.3 lib/rubocop/cop/lint/redundant_dir_glob_sort.rb
rubocop-1.18.2 lib/rubocop/cop/lint/redundant_dir_glob_sort.rb
rubocop-1.18.1 lib/rubocop/cop/lint/redundant_dir_glob_sort.rb
rubocop-1.18.0 lib/rubocop/cop/lint/redundant_dir_glob_sort.rb