Sha256: b5407903493ef6cf2f3f8e0620480bed187f4fed6cfc62a0703c8f200fd941e4

Contents?: true

Size: 1.63 KB

Versions: 17

Compression:

Stored size: 1.63 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

        # @!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')

            add_offense(name_node) do |corrector|
              correction = name_node.value.delete_suffix('.rb')

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

Version data entries

17 entries across 17 versions & 3 rubygems

Version Path
rubocop-1.21.0 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.20.0 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.19.1 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/rubocop-1.18.3/lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.19.0 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.18.4 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.18.3 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.18.2 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.18.1 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.18.0 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.17.0 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.16.1 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.16.0 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.15.0 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
cocRb-0.1.0 .bundle/ruby/3.0.0/gems/rubocop-1.14.0/lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.14.0 lib/rubocop/cop/style/redundant_file_extension_in_require.rb
rubocop-1.13.0 lib/rubocop/cop/style/redundant_file_extension_in_require.rb