class RuboCop::Cop::Chef::Deprecations::NodeSetWithoutLevel

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

@example

#### 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

Constants

MSG
RESTRICT_ON_SEND

Public Instance Methods

on_op_asgn(node) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

add_offense_for_bare_assignment(sub_node) click to toggle source
# File lib/rubocop/cop/chef/deprecation/node_set_without_level.rb, line 57
def add_offense_for_bare_assignment(sub_node)
  if sub_node.receiver == s(:send, nil, :node) # node['foo'] scenario
    add_offense(sub_node.receiver.loc.selector, severity: :warning)
  elsif sub_node.receiver &&
        sub_node.receiver&.node_parts[0] == s(:send, nil, :node) &&
        sub_node.receiver&.node_parts[1] == :[] # node['foo']['bar'] scenario
    add_offense(sub_node.receiver.node_parts.first.loc.selector, severity: :warning)
  end
end