Sha256: 37f6a6bbaca657d94e4bc8b35398f4081e6abba19a1d2372a857f85eff92fb07

Contents?: true

Size: 1.69 KB

Versions: 191

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for nested `File.dirname`.
      # It replaces nested `File.dirname` with the level argument introduced in Ruby 3.1.
      #
      # @example
      #
      #   # bad
      #   File.dirname(File.dirname(path))
      #
      #   # good
      #   File.dirname(path, 2)
      #
      class NestedFileDirname < Base
        include RangeHelp
        extend AutoCorrector
        extend TargetRubyVersion

        MSG = 'Use `dirname(%<path>s, %<level>s)` instead.'
        RESTRICT_ON_SEND = %i[dirname].freeze

        minimum_target_ruby_version 3.1

        # @!method file_dirname?(node)
        def_node_matcher :file_dirname?, <<~PATTERN
          (send
            (const {cbase nil?} :File) :dirname ...)
        PATTERN

        def on_send(node)
          return if file_dirname?(node.parent) || !file_dirname?(node.first_argument)

          path, level = path_with_dir_level(node, 1)
          return if level < 2

          message = format(MSG, path: path, level: level)
          range = offense_range(node)

          add_offense(range, message: message) do |corrector|
            corrector.replace(range, "dirname(#{path}, #{level})")
          end
        end

        private

        def path_with_dir_level(node, level)
          first_argument = node.first_argument

          if file_dirname?(first_argument)
            level += 1
            path_with_dir_level(first_argument, level)
          else
            [first_argument.source, level]
          end
        end

        def offense_range(node)
          range_between(node.loc.selector.begin_pos, node.source_range.end_pos)
        end
      end
    end
  end
end

Version data entries

191 entries across 184 versions & 18 rubygems

Version Path
harbr-0.2.9 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/nested_file_dirname.rb
harbr-0.2.8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/nested_file_dirname.rb
harbr-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/nested_file_dirname.rb
harbr-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/nested_file_dirname.rb
harbr-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/nested_file_dirname.rb
harbr-0.2.4 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/nested_file_dirname.rb
harbr-0.2.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/nested_file_dirname.rb
harbr-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/nested_file_dirname.rb
harbr-0.2.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/nested_file_dirname.rb
harbr-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/nested_file_dirname.rb
harbr-0.1.99 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/nested_file_dirname.rb
harbr-0.1.98 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/nested_file_dirname.rb
harbr-0.1.97 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/nested_file_dirname.rb
harbr-0.1.96 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/nested_file_dirname.rb
harbr-0.1.95 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/nested_file_dirname.rb
harbr-0.1.94 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/nested_file_dirname.rb
harbr-0.1.93 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/nested_file_dirname.rb
harbr-0.1.91 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/nested_file_dirname.rb
harbr-0.1.90 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/nested_file_dirname.rb
harbr-0.1.89 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/nested_file_dirname.rb