Class: RuboCop::Cop::Chef::Modernize::ChefGemNokogiri

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp, 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:


### incorrect
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.'
RESTRICT_ON_SEND =
[:chef_gem].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

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_block(node) ⇒ Object



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

def on_block(node)
  match_property_in_resource?(:chef_gem, 'package_name', node) do |pkg_name|
    return unless pkg_name.arguments&.first&.str_content == 'nokogiri'
    add_offense(node, severity: :refactor) 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(range_with_surrounding_space(range: node.loc.expression, side: :left))
    end
  end
end

#on_send(node) ⇒ Object



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

def on_send(node)
  nokogiri_install?(node) do
    add_offense(node, severity: :refactor) 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(range_with_surrounding_space(range: node.loc.expression, side: :left))
    end
  end
end