Class: RuboCop::Cop::Chef::Correctness::IncorrectLibraryInjection

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/chef/correctness/incorrect_library_injection.rb

Overview

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

Examples:


### 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

Constant Summary collapse

MSG =
'Libraries should be injected into the Chef::DSL::Recipe class and not Chef::Recipe or Chef::Provider classes directly.'
RESTRICT_ON_SEND =
[:send, :include].freeze

Instance Method Summary collapse

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_send(node) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# 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