Sha256: 794a96f81fffc21643e1bf78f7af7efb9fd7e2415f779645582f4013bf4e9989

Contents?: true

Size: 1.59 KB

Versions: 30

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop 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
        extend AutoCorrector

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

        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')

            add_offense(name_node) do |corrector|
              correction = name_node.value.sub(/\.rb\z/, '')

              corrector.replace(name_node, "'#{correction}'")
            end
          end
        end
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 2 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/redundant_file_extension_in_require.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/redundant_file_extension_in_require.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/redundant_file_extension_in_require.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/redundant_file_extension_in_require.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/redundant_file_extension_in_require.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.10.0 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.9.1 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.9.0 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.8.1 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.8.0 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.7.0 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.6.1 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.6.0 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.5.2 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.5.1 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.5.0 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.4.2 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.4.1 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.4.0 lib/rubocop/cop/style/redundant_file_extension_in_require.rb