Class: RuboCop::Cop::Chef::Modernize::UseRequireRelative

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/chef/modernize/use_require_relative.rb

Overview

Instead of using require with a File.expand_path and __FILE__ use the simpler require_relative method.

Examples:


### incorrect
require File.expand_path('../../libraries/helpers', __FILE__)

### correct
require_relative '../libraries/helpers'

Constant Summary collapse

MSG =
'Instead of using require with a File.expand_path and __FILE__ use the simpler require_relative method.'
RESTRICT_ON_SEND =
[:require].freeze

Instance Method Summary collapse

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_send(node) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/rubocop/cop/chef/modernize/use_require_relative.rb', line 46

def on_send(node)
  require_with_expand_path?(node) do |file, path|
    return unless path.source == '__FILE__'
    add_offense(node, severity: :refactor) do |corrector|
      corrected_value = file.value
      corrected_value.slice!(%r{^../}) # take the first ../ off the path
      corrector.replace(node, "require_relative '#{corrected_value}'")
    end
  end
end