Sha256: 5cba1008eb182c9619ec86c6aa992e19207e693e8f03abd77f9283e8ae053187

Contents?: true

Size: 1.29 KB

Versions: 211

Compression:

Stored size: 1.29 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 required_feature.respond_to?(:value)
          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

211 entries across 202 versions & 20 rubygems

Version Path
rubocop-1.70.0 lib/rubocop/cop/lint/require_relative_self_path.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/require_relative_self_path.rb
rubocop-1.69.2 lib/rubocop/cop/lint/require_relative_self_path.rb
rubocop-1.69.1 lib/rubocop/cop/lint/require_relative_self_path.rb
rubocop-1.69.0 lib/rubocop/cop/lint/require_relative_self_path.rb
rubocop-1.68.0 lib/rubocop/cop/lint/require_relative_self_path.rb
rubocop-1.67.0 lib/rubocop/cop/lint/require_relative_self_path.rb
rubocop-1.66.1 lib/rubocop/cop/lint/require_relative_self_path.rb
rubocop-1.66.0 lib/rubocop/cop/lint/require_relative_self_path.rb
rubocop-1.65.1 lib/rubocop/cop/lint/require_relative_self_path.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/require_relative_self_path.rb
rubocop-1.65.0 lib/rubocop/cop/lint/require_relative_self_path.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/require_relative_self_path.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/require_relative_self_path.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/require_relative_self_path.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/require_relative_self_path.rb
rubocop-1.64.1 lib/rubocop/cop/lint/require_relative_self_path.rb
rubocop-1.63.4 lib/rubocop/cop/lint/require_relative_self_path.rb
rubocop-1.63.3 lib/rubocop/cop/lint/require_relative_self_path.rb
rubocop-1.63.2 lib/rubocop/cop/lint/require_relative_self_path.rb