Class: RuboCop::Cop::Chef::Correctness::ConditionalUnifiedModeTrue

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector, TargetChefVersion
Defined in:
lib/rubocop/cop/chef/correctness/conditional_unified_mode_true.rb

Overview

Setting unified_mode true conditionally leaves a resource running in unified mode on newer Chef Infra Client releases and in legacy mode on older ones. That's not a coherent thing to want: the resource has to be written, tested, and debugged against both execution models.

Pick one. Set unified_mode true unconditionally and drop support for releases that predate it, or set unified_mode false if respond_to?(:unified_mode) to deliberately opt out of unified mode everywhere and keep writing the resource in the traditional style.

Only unified_mode true is flagged. unified_mode false under a guard is the supported way to opt out and is left alone.

Examples:


# bad
unified_mode true if respond_to?(:unified_mode)

# good
unified_mode true

# good - deliberately opting out of unified mode
unified_mode false if respond_to?(:unified_mode)

Constant Summary collapse

MSG =
'Set `unified_mode true` unconditionally. Making it conditional gives you unified mode on newer Chef Infra Client releases and legacy mode on older ones, so the resource has to be tested and reasoned about both ways.'
RESTRICT_ON_SEND =
[:unified_mode].freeze

Instance Method Summary collapse

Methods included from TargetChefVersion

minimum_target_chef_version, required_minimum_chef_version, support_target_chef_version?

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_send(node) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rubocop/cop/chef/correctness/conditional_unified_mode_true.rb', line 55

def on_send(node)
  return unless unified_mode_true?(node)

  conditional = node.each_ancestor(:if).first
  return unless conditional

  add_offense(node, severity: :refactor) do |corrector|
    # only unwrap the conditional when it wraps nothing but this property, otherwise
    # we'd delete whatever else the branch was guarding. an elsif is never safe to unwrap:
    # its node covers only the elsif itself, so replacing it drops that branch's condition
    # and folds the property into the preceding branch
    next if conditional.elsif?
    next unless sole_branch_body?(conditional, node)

    corrector.replace(conditional, node.source)
  end
end