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
Source
# File lib/rubocop/cop/chef/style/attribute_keys.rb, line 71 def on_node_attribute_access(node) if node.str_type? style_detected(:strings) if style == :symbols add_offense(node, message: MSG % style, severity: :refactor) do |corrector| key_string = node.children.first.to_s corrector.replace(node, key_string.to_sym.inspect) end end elsif node.sym_type? style_detected(:symbols) if style == :strings add_offense(node, message: MSG % style, severity: :refactor) do |corrector| key_string = node.children.first.to_s corrector.replace(node, key_string.inspect) end end end end
Source
# File lib/rubocop/cop/chef/style/attribute_keys.rb, line 58 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.send_type? && 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