Class: RuboCop::Cop::Chef::Modernize::ActionMethodInResource

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/chef/modernize/action_method_in_resource.rb

Overview

Use the custom resource language’s ‘action :my_action` blocks instead of creating actions with methods.

Examples:


### incorrect
def action_create
 # :create action code here
end

### correct
action :create do
 # :create action code here
end

Constant Summary collapse

MSG =
"Use the custom resource language's `action :my_action` blocks instead of creating actions with methods."

Instance Method Summary collapse

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_def(node) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/rubocop/cop/chef/modernize/action_method_in_resource.rb', line 43

def on_def(node)
  return unless node.method_name.to_s.start_with?('action_') # when we stop support for Ruby < 2.7 the .to_s can go away here
  return if node.arguments? # if they passed in arguments they may actually need this
  return if node.parent && includes_poise?(node.parent)

  add_offense(node, severity: :refactor) do |corrector|
    # @todo when we drop ruby 2.4 support we can convert this to use delete_suffix
    corrector.replace(node, node.source.gsub("def #{node.method_name}", "action :#{node.method_name.to_s.gsub(/^action_/, '')} do"))
  end
end