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'

# good
platform?('ubuntu')

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#autocorrect(node) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/rubocop/cop/chef/style/use_platform_helpers.rb', line 46

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

#on_send(node) ⇒ Object



40
41
42
43
44
# File 'lib/rubocop/cop/chef/style/use_platform_helpers.rb', line 40

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