class RuboCop::Cop::Chef::Correctness::IncorrectLibraryInjection

Libraries should be injected into the ‘Chef::DSL::Recipe` class and not `Chef::Recipe` or `Chef::Provider` classes directly.

@example

#### incorrect
::Chef::Recipe.send(:include, Filebeat::Helpers)
::Chef::Provider.send(:include, Filebeat::Helpers)
::Chef::Recipe.include Filebeat::Helpers
::Chef::Provider.include Filebeat::Helpers

#### correct
::Chef::DSL::Recipe.send(:include, Filebeat::Helpers) # covers previous Recipe & Provider classes

Constants

MSG
RESTRICT_ON_SEND

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/chef/correctness/incorrect_library_injection.rb, line 61
def on_send(node)
  legacy_injection?(node) do
    add_offense(node, severity: :refactor) do |corrector|
      if node.parent && correct_injection?(node.parent)
        corrector.remove(range_with_surrounding_space(range: node.loc.expression, side: :left))
      else
        corrector.replace(node,
          node.source.gsub(/Chef::(Provider|Recipe)/, 'Chef::DSL::Recipe'))
      end
    end
  end
end