Class: RuboCop::Cop::Chef::ChefModernize::ChefGemNokogiri

Inherits:
RuboCop::Cop
  • Object
show all
Includes:
RuboCop::Chef::CookbookHelpers
Defined in:
lib/rubocop/cop/chef/modernize/chef_gem_nokogiri.rb

Overview

The nokogiri gem ships in Chef Infra Client 12+ and does not need to be installed before being used

Examples:


# bad
chef_gem 'nokogiri'

Constant Summary collapse

MSG =
'The nokogiri gem ships in Chef Infra Client 12+ and does not need to be installed before being used.'.freeze

Instance Method Summary collapse

Methods included from RuboCop::Chef::CookbookHelpers

#match_property_in_resource?, #match_resource_type?, #method_arg_ast_to_string, #resource_block_name_if_string

Instance Method Details

#autocorrect(node) ⇒ Object



50
51
52
53
54
55
# File 'lib/rubocop/cop/chef/modernize/chef_gem_nokogiri.rb', line 50

def autocorrect(node)
  lambda do |corrector|
    node = node.parent if node.parent&.block_type? # make sure we get the whole block not just the method in the block
    corrector.remove(node.loc.expression)
  end
end

#on_block(node) ⇒ Object



38
39
40
41
42
# File 'lib/rubocop/cop/chef/modernize/chef_gem_nokogiri.rb', line 38

def on_block(node)
  match_property_in_resource?(:chef_gem, 'package_name', node) do |pkg_name|
    add_offense(node, location: :expression, message: MSG, severity: :refactor) if pkg_name.arguments&.first&.str_content == 'nokogiri'
  end
end

#on_send(node) ⇒ Object



44
45
46
47
48
# File 'lib/rubocop/cop/chef/modernize/chef_gem_nokogiri.rb', line 44

def on_send(node)
  nokogiri_install?(node) do
    add_offense(node, location: :expression, message: MSG, severity: :refactor)
  end
end