lib/rubocop/cop/chef/modernize/respond_to_provides.rb in cookstyle-6.2.9 vs lib/rubocop/cop/chef/modernize/respond_to_provides.rb in cookstyle-6.3.4

- old
+ new

@@ -1,7 +1,7 @@ # -# Copyright:: 2019, Chef Software, Inc. +# Copyright:: 2019-2020, Chef Software, Inc. # Author:: Tim Smith (<tsmith@chef.io>) # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -16,31 +16,41 @@ # module RuboCop module Cop module Chef module ChefModernize - # It is not longer necessary respond_to?(:foo) in metadata. This was used to support new metadata - # methods in Chef 11 and early versions of Chef 12. + # In Chef Infra Client 12+ is is no longer necessary to gate the use of the provides methods in resources with `if respond_to?(:provides)` or `if defined? provides`. # # @example # # # bad # provides :foo if respond_to?(:provides) # + # provides :foo if defined? provides + # # # good # provides :foo # class RespondToProvides < Cop - MSG = 'respond_to?(:provides) in resources is no longer necessary in Chef Infra Client 12+'.freeze + MSG = 'Using `respond_to?(:provides)` or `if defined? provides` in resources is no longer necessary in Chef Infra Client 12+.'.freeze def on_if(node) if_respond_to_provides?(node) do add_offense(node, location: :expression, message: MSG, severity: :refactor) end end def_node_matcher :if_respond_to_provides?, <<~PATTERN - (if (send nil? :respond_to? ( :sym :provides ) ) ... ) + (if + { + (send nil? :respond_to? + (sym :provides)) + + (:defined? + (send nil? :provides)) + } + (send nil? :provides + (sym _)) ... ) PATTERN def autocorrect(node) lambda do |corrector| corrector.replace(node.loc.expression, node.children[1].source)