Class: RuboCop::Cop::Chef::Modernize::PowerShellGuardInterpreter

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector, TargetChefVersion
Includes:
RangeHelp, RuboCop::Chef::CookbookHelpers
Defined in:
lib/rubocop/cop/chef/modernize/powershell_guard_interpreter.rb

Overview

PowerShell is already set as the default guard interpreter for ‘powershell_script` and `batch` resources in Chef Infra Client 13 and later and does not need to be specified.

Examples:


### incorrect
powershell_script 'Create Directory' do
  code "New-Item -ItemType Directory -Force -Path C:\mydir"
  guard_interpreter :powershell_script
end

batch 'Create Directory' do
  code "mkdir C:\mydir"
  guard_interpreter :powershell_script
end

### correct
powershell_script 'Create Directory' do
  code "New-Item -ItemType Directory -Force -Path C:\mydir"
end

batch 'Create Directory' do
  code "mkdir C:\mydir"
end

Constant Summary collapse

MSG =
'PowerShell is already set as the default guard interpreter for `powershell_script` and `batch` resources in Chef Infra Client 13 and later and does not need to be specified.'

Instance Method Summary collapse

Methods included from TargetChefVersion

minimum_target_chef_version, required_minimum_chef_version, support_target_chef_version?

Methods included from RuboCop::Chef::CookbookHelpers

#match_property_in_resource?, #match_resource_type?, #method_arg_ast_to_string, #resource_block_name_if_string

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_block(node) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/rubocop/cop/chef/modernize/powershell_guard_interpreter.rb', line 56

def on_block(node)
  match_property_in_resource?(%i(powershell_script batch), 'guard_interpreter', node) do |interpreter|
    return unless interpreter.arguments.first.source == ':powershell_script'
    add_offense(interpreter, severity: :refactor) do |corrector|
      corrector.remove(range_with_surrounding_space(range: interpreter.loc.expression, side: :left))
    end
  end
end