Sha256: 307141c875dc1cef2628bc1aa369470ebb220dbb6a443eea73df14ed36b64376

Contents?: true

Size: 1.83 KB

Versions: 181

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for the presence of superfluous `.rb` extension in
      # the filename provided to `require` and `require_relative`.
      #
      # Note: If the extension is omitted, Ruby tries adding '.rb', '.so',
      #       and so on to the name until found. If the file named cannot be found,
      #       a `LoadError` will be raised.
      #       There is an edge case where `foo.so` file is loaded instead of a `LoadError`
      #       if `foo.so` file exists when `require 'foo.rb'` will be changed to `require 'foo'`,
      #       but that seems harmless.
      #
      # @example
      #   # bad
      #   require 'foo.rb'
      #   require_relative '../foo.rb'
      #
      #   # good
      #   require 'foo'
      #   require 'foo.so'
      #   require_relative '../foo'
      #   require_relative '../foo.so'
      #
      class RedundantFileExtensionInRequire < Base
        include RangeHelp
        extend AutoCorrector

        MSG = 'Redundant `.rb` file extension detected.'
        RESTRICT_ON_SEND = %i[require require_relative].freeze

        # @!method require_call?(node)
        def_node_matcher :require_call?, <<~PATTERN
          (send nil? {:require :require_relative} $str_type?)
        PATTERN

        def on_send(node)
          require_call?(node) do |name_node|
            return unless name_node.value.end_with?('.rb')

            extension_range = extension_range(name_node)

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

        private

        def extension_range(name_node)
          end_of_path_string = name_node.source_range.end_pos

          range_between(end_of_path_string - 4, end_of_path_string - 1)
        end
      end
    end
  end
end

Version data entries

181 entries across 174 versions & 18 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/redundant_file_extension_in_require.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/redundant_file_extension_in_require.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/redundant_file_extension_in_require.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/redundant_file_extension_in_require.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/redundant_file_extension_in_require.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.64.1 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.63.4 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.63.3 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.63.2 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.63.1 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.63.0 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.62.1 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.62.0 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.61.0 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.60.2 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
study_line-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/redundant_file_extension_in_require.rb