class RuboCop::Cop::Chef::Style::AttributeKeys
Check which style of keys are used to access node attributes.
There are two supported styles: “symbols” and “strings”.
@example when configuration is `EnforcedStyle: symbols`
#### incorrect node['foo'] node["foo"] #### correct node[:foo]
@example when configuration is `EnforcedStyle: strings`
#### incorrect node[:foo] #### correct node['foo'] node["foo"]
Constants
- MSG
- RESTRICT_ON_SEND
Public Instance Methods
autocorrect(node)
click to toggle source
# File lib/rubocop/cop/chef/style/attribute_keys.rb, line 80 def autocorrect(node) lambda do |corrector| key_string = node.children.first.to_s key_replacement = if style == :symbols key_string.to_sym.inspect else # strings key_string.inspect end corrector.replace(node, key_replacement) end end
on_node_attribute_access(node)
click to toggle source
# File lib/rubocop/cop/chef/style/attribute_keys.rb, line 70 def on_node_attribute_access(node) if node.type == :str style_detected(:strings) add_offense(node, location: :expression, message: MSG % style, severity: :refactor) if style == :symbols elsif node.type == :sym style_detected(:symbols) add_offense(node, location: :expression, message: MSG % style, severity: :refactor) if style == :strings end end
on_send(node)
click to toggle source
# File lib/rubocop/cop/chef/style/attribute_keys.rb, line 57 def on_send(node) if node_attribute_access?(node) || node_level_attribute_access?(node) # node is first child for #[], need to look for the outermost parent too. outer_node = node while outer_node.parent && outer_node.parent.type == :send && outer_node.parent.children[1] == :[] on_node_attribute_access(outer_node.children[2]) outer_node = outer_node.parent end # One last time for the outer-most access. on_node_attribute_access(outer_node.children[2]) end end