Class: RuboCop::Cop::Chef::ChefDeprecations::LegacyNotifySyntax

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

Overview

Use the new-style notification syntax which allows you to notify resources defined later in a recipe or resource.

Examples:


# bad
template '/etc/www/configures-apache.conf' do
  notifies :restart, resources(service: 'apache')
end

template '/etc/www/configures-apache.conf' do
  notifies :restart, resources(service: 'apache'), :immediately
end

template '/etc/www/configures-apache.conf' do
  notifies :restart, resources(service: service_name_variable), :immediately
end

# good
template '/etc/www/configures-apache.conf' do
  notifies :restart, 'service[apache]'
end

template '/etc/www/configures-apache.conf' do
  notifies :restart, 'service[apache]', :immediately
end

template '/etc/www/configures-apache.conf' do
  notifies :restart, "service[#{service_name_variable}]", :immediately
end

Constant Summary collapse

MSG =
'Use the new-style notification syntax which allows you to notify resources defined later in a recipe or resource.'.freeze

Instance Method Summary collapse

Instance Method Details

#autocorrect(node) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rubocop/cop/chef/deprecation/legacy_notify_syntax.rb', line 64

def autocorrect(node)
  lambda do |corrector|
    legacy_notify?(node) do |action, type, name, timing|
      service_value = case name.type
                      when :str
                        "'#{type.source}[#{name.value}]'"
                      when :dstr
                        "\"#{type.source}[#{name.value.source}]\""
                      else
                        "\"#{type.source}[\#{#{name.source}}]\""
                      end
      new_val = "notifies #{action.source}, #{service_value}"
      new_val << ", #{timing.first.source}" unless timing.empty?
      corrector.replace(node.loc.expression, new_val)
    end
  end
end

#on_send(node) ⇒ Object



58
59
60
61
62
# File 'lib/rubocop/cop/chef/deprecation/legacy_notify_syntax.rb', line 58

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