Class: RuboCop::Cop::Chef::RedundantCode::MultiplePlatformChecks

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

Overview

You can pass multiple values to the platform? and platform_family? helpers instead of calling the helpers multiple times.

Examples:


### incorrect
platform?('redhat') || platform?('ubuntu')
platform_family?('debian') || platform_family?('rhel')

### correct
platform?('redhat', 'ubuntu')
platform_family?('debian', 'rhel')

Constant Summary collapse

MSG =
'You can pass multiple values to the platform? and platform_family? helpers instead of calling the helpers multiple times.'

Instance Method Summary collapse

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_or(node) ⇒ Object



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

def on_or(node)
  or_platform_helpers?(node) do |helpers, plats|
    # if the helper types were the same it's an offense, but platform_family?('rhel') || platform?('ubuntu') is legit
    return unless helpers.uniq.size == 1

    add_offense(node, severity: :refactor) do |corrector|
      new_string = "#{helpers.first}(#{plats.map(&:source).join(', ')})"
      corrector.replace(node, new_string)
    end
  end
end