Class: RuboCop::Cop::Chef::Deprecations::NodeSetWithoutLevel

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/chef/deprecation/node_set_without_level.rb

Overview

When setting a node attribute in Chef Infra Client 11 and later you must specify the precedence level.

Examples:


### incorrect
node['foo']['bar'] = 1
node['foo']['bar'] << 1
node['foo']['bar'] += 1
node['foo']['bar'] -= 1

### correct
node.default['foo']['bar'] = 1
node.default['foo']['bar'] << 1
node.default['foo']['bar'] += 1
node.default['foo']['bar'] -= 1

Constant Summary collapse

MSG =
'When setting a node attribute in Chef Infra Client 11 and later you must specify the precedence level.'
RESTRICT_ON_SEND =
[:[]=, :<<].freeze

Instance Method Summary collapse

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_op_asgn(node) ⇒ Object



41
42
43
44
45
46
# File 'lib/rubocop/cop/chef/deprecation/node_set_without_level.rb', line 41

def on_op_asgn(node)
  # make sure it was a += or -=
  if %i(- +).include?(node.node_parts[1])
    add_offense_for_bare_assignment(node.children&.first)
  end
end

#on_send(node) ⇒ Object



48
49
50
51
52
53
# File 'lib/rubocop/cop/chef/deprecation/node_set_without_level.rb', line 48

def on_send(node)
  # make sure the method being send is []= and then make sure the receiver is a send
  if %i([]= <<).include?(node.method_name) && node.receiver.send_type?
    add_offense_for_bare_assignment(node)
  end
end