Class: RuboCop::Cop::Chef::Correctness::InvalidPlatformFamilyHelper

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp, RuboCop::Chef::PlatformHelpers
Defined in:
lib/rubocop/cop/chef/correctness/invalid_platform_family_helper.rb

Overview

Pass valid platform families to the ‘platform_family?` helper. See [Infra Language: Platform Family](docs.chef.io/infra_language/checking_platforms/#platform_family-values) for a complete list of platform families.

Examples:


### incorrect
platform_family?('redhat')
platform_family?('sles')

### incorrect
platform_family?('rhel')
platform_family?('suse')

Constant Summary collapse

MSG =
'Pass valid platform families to the platform_family? helper.'
RESTRICT_ON_SEND =
[:platform_family?].freeze

Constants included from RuboCop::Chef::PlatformHelpers

RuboCop::Chef::PlatformHelpers::INVALID_PLATFORMS, RuboCop::Chef::PlatformHelpers::INVALID_PLATFORM_FAMILIES

Instance Method Summary collapse

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_send(node) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rubocop/cop/chef/correctness/invalid_platform_family_helper.rb', line 46

def on_send(node)
  platform_family_helper?(node) do |plats|
    plats.to_a.each do |p|
      next unless INVALID_PLATFORM_FAMILIES.key?(p.value)
      add_offense(p, severity: :refactor) do |corrector|
        replacement_platform = INVALID_PLATFORM_FAMILIES[p.value]
        all_passed_platforms = p.parent.arguments.map(&:value)

        # see if we have a replacement platform in our hash. If not we can't autocorrect
        next unless replacement_platform
        # if the replacement platform was one of the other platforms passed we can just delete this bad platform
        if all_passed_platforms.include?(replacement_platform)
          all_passed_platforms.delete(p.value)
          arg_range = p.parent.arguments.first.loc.expression.join(p.parent.arguments[-1].loc.expression.end)
          corrector.replace(arg_range, all_passed_platforms.map { |x| "'#{x}'" }.join(', '))
        else
          corrector.replace(p.loc.expression, p.value.gsub(p.value, "'#{replacement_platform}'")) # gsub to retain quotes
        end
      end
    end
  end
end