class RuboCop::Cop::Chef::Correctness::InvalidPlatformFamilyHelper

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.

@example

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

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

Constants

MSG
RESTRICT_ON_SEND

Public Instance Methods

on_send(node) click to toggle source
# 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, message: MSG, 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