Class: RuboCop::Cop::Chef::ChefStyle::UsePlatformHelpers

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

Overview

Use the platform?() and platform_family?() helpers instead of node == ‘foo’ and node == ‘bar’. These helpers are easier to read and can accept multiple platform arguments, which greatly simplifies complex platform logic.

Examples:


# bad
node['platform'] == 'ubuntu'
node['platform_family'] == 'debian'
node['platform'] != 'ubuntu'
node['platform_family'] != 'debian'

# good
platform?('ubuntu')
!platform?('ubuntu')
platform_family?('debian')
!platform_family?('debian')

Constant Summary collapse

MSG =
"Use platform? and platform_family? helpers for checking a node's platform".freeze

Instance Method Summary collapse

Instance Method Details

#autocorrect(node) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/rubocop/cop/chef/style/use_platform_helpers.rb', line 50

def autocorrect(node)
  lambda do |corrector|
    platform_check?(node) do |type, operator, plat|
      corrected_string = operator == :!= ? '!' : ''
      corrected_string << "#{type.value}?('#{plat.value}')"
      corrector.replace(node.loc.expression, corrected_string)
    end
  end
end

#on_send(node) ⇒ Object



44
45
46
47
48
# File 'lib/rubocop/cop/chef/style/use_platform_helpers.rb', line 44

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