Class: RuboCop::Cop::Chef::Style::AttributeKeys
- Extended by:
- AutoCorrector
- Includes:
- RuboCop::Cop::ConfigurableEnforcedStyle
- Defined in:
- lib/rubocop/cop/chef/style/attribute_keys.rb
Overview
Check which style of keys are used to access node attributes.
There are two supported styles: “symbols” and “strings”.
Constant Summary collapse
- MSG =
'Use %s to access node attributes'- RESTRICT_ON_SEND =
[:[]].freeze
Instance Method Summary collapse
Methods inherited from Base
Instance Method Details
#on_node_attribute_access(node) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# 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 |
#on_send(node) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# 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 |