Class: RuboCop::Cop::Chef::ChefModernize::RespondToInMetadata

Inherits:
RuboCop::Cop
  • Object
show all
Defined in:
lib/rubocop/cop/chef/modernize/respond_to_metadata.rb

Overview

It is not longer necessary respond_to?(:foo) or defined?(foo) in metadata. This was used to support new metadata methods in Chef 11 and early versions of Chef 12.

Examples:


# bad
chef_version '>= 13' if respond_to?(:chef_version)
chef_version '>= 13' if defined?(chef_version)

# good
chef_version '>= 13'

Constant Summary collapse

MSG =
'It is no longer necessary to use respond_to? or if_defined? in metadata.rb in Chef Infra Client 12.15 and later'.freeze

Instance Method Summary collapse

Instance Method Details

#autocorrect(node) ⇒ Object



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

def autocorrect(node)
  lambda do |corrector|
    corrector.replace(node.loc.expression, node.children[1].source)
  end
end

#on_defined?(node) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/rubocop/cop/chef/modernize/respond_to_metadata.rb', line 42

def on_defined?(node)
  node = node.parent if node.parent.if? # we want the whole if statement
  add_offense(node, location: :expression, message: MSG, severity: :refactor)
end

#on_if(node) ⇒ Object



36
37
38
39
40
# File 'lib/rubocop/cop/chef/modernize/respond_to_metadata.rb', line 36

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