Sha256: 5c6bb9fb1a52304a8618f5d67a03597c92bbd0343fc0558ecc6c67a694a776be

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # Checks for uses a file requiring itself with `require_relative`.
      #
      # @example
      #
      #   # bad
      #
      #   # foo.rb
      #   require_relative 'foo'
      #   require_relative 'bar'
      #
      #   # good
      #
      #   # foo.rb
      #   require_relative 'bar'
      #
      class RequireRelativeSelfPath < Base
        include RangeHelp
        extend AutoCorrector

        MSG = 'Remove the `require_relative` that requires itself.'
        RESTRICT_ON_SEND = %i[require_relative].freeze

        def on_send(node)
          return unless (required_feature = node.first_argument)
          return unless same_file?(processed_source.file_path, required_feature.value)

          add_offense(node) do |corrector|
            corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
          end
        end

        private

        def same_file?(file_path, required_feature)
          file_path == required_feature || remove_ext(file_path) == required_feature
        end

        def remove_ext(file_path)
          File.basename(file_path, File.extname(file_path))
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-1.22.0 lib/rubocop/cop/lint/require_relative_self_path.rb