Sha256: 6cd687a2bcc100018734b2f35030984fad40f9f1854b466a6b6e78a30ec7ac7d

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for paths given to `require_relative` that start with
      # the current directory (`./`), which can be omitted.
      #
      # @example
      #
      #   # bad
      #   require_relative './path/to/feature'
      #
      #   # good
      #   require_relative 'path/to/feature'
      #
      class RedundantCurrentDirectoryInPath < Base
        include RangeHelp
        extend AutoCorrector

        MSG = 'Remove the redundant current directory path.'
        RESTRICT_ON_SEND = %i[require_relative].freeze
        CURRENT_DIRECTORY_PATH = './'

        def on_send(node)
          return unless (first_argument = node.first_argument)
          return unless first_argument.str_content&.start_with?(CURRENT_DIRECTORY_PATH)
          return unless (index = first_argument.source.index(CURRENT_DIRECTORY_PATH))

          begin_pos = first_argument.source_range.begin.begin_pos + index
          range = range_between(begin_pos, begin_pos + 2)

          add_offense(range) do |corrector|
            corrector.remove(range)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-1.70.0 lib/rubocop/cop/style/redundant_current_directory_in_path.rb