Class: RuboCop::Cop::Chef::ChefDeprecations::ChefRewind

Inherits:
RuboCop::Cop
  • Object
show all
Includes:
RuboCop::Chef::CookbookHelpers
Defined in:
lib/rubocop/cop/chef/deprecation/chef_rewind.rb

Overview

Use delete_resource / edit_resource instead of functionality in the deprecated chef-rewind gem

Examples:


chef_gem 'chef-rewind'

require 'chef/rewind'

rewind "user[postgres]" do
  home '/var/lib/pgsql/9.2'
  cookbook 'my-postgresql'    # or `cookbook cookbook_name()`
end

unwind "user[postgres]"

Constant Summary collapse

MAPPING =
{
  rewind: 'edit_resource',
  unwind: 'delete_resource',
}.freeze
MSG =
'Use delete_resource / edit_resource instead of functionality in the deprecated chef-rewind 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

Instance Method Details

#autocorrect(node) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rubocop/cop/chef/deprecation/chef_rewind.rb', line 79

def autocorrect(node)
  lambda do |corrector|
    rewind_gem_install?(node) do
      node = node.parent if node.parent&.block_type? # make sure we get the whole block not just the method in the block
      corrector.remove(node.loc.expression)
      return
    end

    require_rewind?(node) do
      corrector.remove(node.loc.expression)
      return
    end

    match_property_in_resource?(:chef_gem, 'package_name', node) do |pkg_name|
      corrector.remove(node.loc.expression) if pkg_name.arguments&.first&.str_content == 'chef-rewind'
      return
    end

    rewind_resources?(node) do |string|
      corrector.replace(node.loc.expression, node.source.gsub(string.to_s, MAPPING[string]))
    end
  end
end

#on_block(node) ⇒ Object



73
74
75
76
77
# File 'lib/rubocop/cop/chef/deprecation/chef_rewind.rb', line 73

def on_block(node)
  match_property_in_resource?(:chef_gem, 'package_name', node) do |pkg_name|
    add_offense(node, location: :expression, message: MSG, severity: :refactor) if pkg_name.arguments&.first&.str_content == 'chef-rewind'
  end
end

#on_send(node) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rubocop/cop/chef/deprecation/chef_rewind.rb', line 59

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

  require_rewind?(node) do
    add_offense(node, location: :expression, message: MSG, severity: :refactor)
  end

  rewind_resources?(node) do
    add_offense(node, location: :expression, message: MSG, severity: :refactor)
  end
end