Class: RuboCop::Cop::Chef::Deprecations::NamePropertyWithDefaultValue

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/chef/deprecation/name_property_and_default.rb

Overview

A resource property (or attribute) can’t be marked as a name_property (or name_attribute) and also have a default value. The name property is a special property that is derived from the name of the resource block in and thus always has a value passed to the resource. For example if you define ‘my_resource ’foo’‘ in recipe, then the name property of `my_resource` will automatically be set to `foo`. Setting a property to be both a name_property and have a default value will cause Chef Infra Client failures in 13.0 and later releases.

Examples:


### incorrect
property :config_file, String, default: 'foo', name_property: true
attribute :config_file, String, default: 'foo', name_attribute: true

### correct
property :config_file, String, name_property: true
attribute :config_file, String, name_attribute: true

Constant Summary collapse

MSG =
"A resource property can't be marked as a name_property and also have a default value. This will fail in Chef Infra Client 13 or later."
RESTRICT_ON_SEND =
[:attribute, :property].freeze

Instance Method Summary collapse

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_send(node) ⇒ Object



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

def on_send(node)
  name_property_with_default?(node) do |default|
    add_offense(node, severity: :warning) do |corrector|
      range = range_with_surrounding_comma(range_with_surrounding_space(range: default.loc.expression, side: :left), :left)
      corrector.remove(range)
    end
  end
end