class RuboCop::Cop::Chef::RedundantCode::UnnecessaryNameProperty

There is no need to define a property or attribute named :name in a resource as Chef Infra defines this on all resources by default.

@example

#### incorrect
property :name, String
property :name, String, name_property: true
attribute :name, kind_of: String
attribute :name, kind_of: String, name_attribute: true
attribute :name, name_attribute: true, kind_of: String

Constants

MSG
RESTRICT_ON_SEND

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/chef/redundant/unnecessary_name_property.rb, line 47
def on_send(node)
  name_property?(node) do |hash_vals|
    # It's perfectly valid to redefine the name property if you give it non-default values
    # We do this in a few of our core resources where we give it a default value of "" for nameless resources
    # If there are hash vals in this attribute/property compare them with the default keys and if there's anything
    # else return so we don't alert
    unless hash_vals.empty?
      hash_keys = hash_vals.first.map { |x| x.key.value }
      return unless (hash_keys - [:kind_of, :name_attribute, :name_property]).empty?
    end

    add_offense(node, message: MSG, severity: :refactor) do |corrector|
      corrector.remove(node.source_range)
    end
  end
end