Module: RuboCop::Chef::CookbookHelpers

Included in:
RuboCop::Cop::Chef::ChefCorrectness::NotifiesActionNotSymbol, RuboCop::Cop::Chef::ChefCorrectness::ResourceSetsInternalProperties, RuboCop::Cop::Chef::ChefCorrectness::ResourceSetsNameProperty, RuboCop::Cop::Chef::ChefCorrectness::ResourceWithNoneAction, RuboCop::Cop::Chef::ChefDeprecations::ChefHandlerUsesSupports, RuboCop::Cop::Chef::ChefDeprecations::ChefRewind, RuboCop::Cop::Chef::ChefDeprecations::ChocolateyPackageUninstallAction, RuboCop::Cop::Chef::ChefDeprecations::DeprecatedYumRepositoryProperties, RuboCop::Cop::Chef::ChefDeprecations::LaunchdDeprecatedHashProperty, RuboCop::Cop::Chef::ChefDeprecations::LocaleDeprecatedLcAllProperty, RuboCop::Cop::Chef::ChefDeprecations::PoiseArchiveUsage, RuboCop::Cop::Chef::ChefDeprecations::UserDeprecatedSupportsProperty, RuboCop::Cop::Chef::ChefDeprecations::VerifyPropertyUsesFileExpansion, RuboCop::Cop::Chef::ChefDeprecations::WindowsTaskChangeAction, RuboCop::Cop::Chef::ChefModernize::ChefGemNokogiri, RuboCop::Cop::Chef::ChefModernize::Definitions, RuboCop::Cop::Chef::ChefModernize::ExecuteTzUtil, RuboCop::Cop::Chef::ChefModernize::PowerShellGuardInterpreter, RuboCop::Cop::Chef::ChefModernize::PowershellInstallPackage, RuboCop::Cop::Chef::ChefModernize::PowershellInstallWindowsFeature, RuboCop::Cop::Chef::ChefModernize::PowershellScriptExpandArchive, RuboCop::Cop::Chef::ChefModernize::ShellOutToChocolatey, RuboCop::Cop::Chef::ChefModernize::SysctlParamResource, RuboCop::Cop::Chef::ChefModernize::ZipfileResource
Defined in:
lib/rubocop/chef/cookbook_helpers.rb

Overview

Common node helpers used for matching against Chef Infra Cookbooks

Instance Method Summary collapse

Instance Method Details

#match_property_in_resource?(resource_name, property_name, node) { ... } ⇒ Boolean

Match particular properties within a resource

Parameters:

  • resource_name (String)

    The name of the resource to match

  • property_name (String)

    The name of the property to match (or action)

  • node (RuboCop::AST::Node)

    The rubocop ast node to search

Yields:

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rubocop/chef/cookbook_helpers.rb', line 32

def match_property_in_resource?(resource_name, property_name, node)
  return unless looks_like_resource?(node)
  # bail out if we're not in the resource we care about or nil was passed (all resources)
  return unless resource_name.nil? || node.children.first.method?(resource_name.to_sym) # see if we're in the right resource

  resource_block = node.children[2] # the 3rd child is the actual block in the resource
  return unless resource_block # nil would be an empty block
  if resource_block.begin_type? # if begin_type we need to iterate over the children
    resource_block.children.each do |resource_blk_child|
      extract_send_types(resource_blk_child) do |p|
        yield(p) if p.method_name == property_name.to_sym
      end
    end
  else # there's only a single property to check
    extract_send_types(resource_block) do |p|
      yield(p) if p.method_name == property_name.to_sym
    end
  end
end

#match_resource_type?(resource_name, node) { ... } ⇒ Boolean

Match a particular resource

Parameters:

  • resource_name (String)

    The name of the resource to match

  • node (RuboCop::AST::Node)

    The rubocop ast node to search

Yields:

Returns:

  • (Boolean)


18
19
20
21
22
# File 'lib/rubocop/chef/cookbook_helpers.rb', line 18

def match_resource_type?(resource_name, node)
  return unless looks_like_resource?(node)
  # bail out if we're not in the resource we care about or nil was passed (all resources)
  yield(node) if node.children.first.method?(resource_name.to_sym)
end

#method_arg_ast_to_string(ast) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/rubocop/chef/cookbook_helpers.rb', line 52

def method_arg_ast_to_string(ast)
  # a property without a value. This is totally bogus, but they exist
  return if ast.children[2].nil?
  # https://rubular.com/r/6uzOMd6WCHewOu
  m = ast.children[2].source.match(/^("|')(.*)("|')$/)
  return m[2] unless m.nil?
end

#resource_block_name_if_string(node) ⇒ Object



5
6
7
8
9
# File 'lib/rubocop/chef/cookbook_helpers.rb', line 5

def resource_block_name_if_string(node)
  if looks_like_resource?(node) && node.children.first.arguments.first.respond_to?(:value)
    node.children.first.arguments.first.value
  end
end