Class: RuboCop::Cop::Chef::RedundantCode::DoubleCompileTime

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

Overview

If a resource includes the compile_time property there's no need to also use .run_action(:some_action) on the resource block

Examples:


# bad
chef_gem 'deep_merge' do
  action :nothing
  compile_time true
end.run_action(:install)

# good
chef_gem 'deep_merge' do
  action :install
  compile_time true
end

Constant Summary collapse

MSG =
"If a resource includes the `compile_time` property there's no need to also use `.run_action(:some_action)` on the resource block."
RESTRICT_ON_SEND =
[:run_action].freeze

Instance Method Summary collapse

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_send(node) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rubocop/cop/chef/redundant/double_compile_time.rb', line 56

def on_send(node)
  run_action_on_resource?(node) do |resource, run_action|
    next unless compile_time_true?(resource.body)

    # without an explicit action there's nothing to rewrite: the resource runs its
    # default action, and picking the replacement would mean knowing what that is.
    # report it, but leave the fix to a human.
    action = action_nodes(resource.body).first
    unless action
      add_offense(node.loc.selector, severity: :refactor)
      next
    end

    add_offense(node.loc.selector, severity: :refactor) do |corrector|
      # rewrite the action's value and drop the trailing .run_action(...). rewriting the
      # whole block source instead would replace the action name wherever else it appears,
      # renaming the resource itself in something like chef_gem 'nothing'
      corrector.replace(action, ":#{run_action}")
      corrector.remove(node.loc.dot.join(node.source_range.end))
    end
  end
end