Class: RuboCop::Cop::Crystal::RequireRelative

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/crystal/require_relative.rb

Overview

In ruby, require_relative attempts to load the library relative to the directory of the currently executing file. Crystal does not have require_relative, but it has the same behavior in the form of require ‘./foo’

Examples:

# bad
require_relative 'foo'
require_relative './bar'
require_relative '../baz'
require_relative '/qux'

# good
require './foo'
require './bar'
require '../baz'
require '/qux'

Constant Summary collapse

MSG =
'Crystal does not support require_relative.'
RESTRICT_ON_SEND =
[:require_relative]

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/rubocop/cop/crystal/require_relative.rb', line 25

def on_send(node)
  add_offense(node) do |corrector|
    if node.first_argument.value.start_with?('.', '/')
      require_value = node.first_argument.value
    else
      require_value = "./#{node.first_argument.value}"
    end
    corrector.replace(node, "require '#{require_value}'")
  end
end