Skip to content

Commit

Permalink
Merge pull request #316 from chef/unnecessary_name_prop
Browse files Browse the repository at this point in the history
 Add ChefCorrectness/UnnecessaryNameProperty
  • Loading branch information
tas50 authored Oct 7, 2019
2 parents 6d7302e + d838ba3 commit f983f40
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config/cookstyle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ ChefCorrectness/IncludingOhaiDefaultRecipe:
Exclude:
- '**/metadata.rb'

ChefCorrectness/UnnecessaryNameProperty:
Description: There is no need to define a property named :name in a resource as Chef Infra defines that property for all resources out of the box.
Enabled: true
VersionAdded: '5.8.0'
Include:
- '**/resources/*.rb'
- '**/libraries/*.rb'

###############################
# ChefDeprecations: Resolving Deprecations that block upgrading Chef Infra Client
###############################
Expand Down
53 changes: 53 additions & 0 deletions lib/rubocop/cop/chef/correctness/unnecessary_name_property.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#
# Copyright:: Copyright 2019, Chef Software Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
module RuboCop
module Cop
module Chef
module ChefCorrectness
# There is no need to define a property named :name in a resource as Chef Infra defines that property for all resources out of the box.
#
# @example
#
# # bad
# property :name, String
# property :name, String, name_property: true
#
class UnnecessaryNameProperty < Cop
MSG = 'There is no need to define a property named :name in a resource as Chef Infra defines that property for all resources out of the box.'.freeze

def_node_matcher :name_property?, <<-PATTERN
(send nil? :property (sym :name) (const nil? :String) $...)
PATTERN

def on_send(node)
name_property?(node) do |hash_vals|
if hash_vals.empty? || (hash_vals.first.keys.count == 1 && hash_vals.first.keys.first.source == 'name_property')
add_offense(node, location: :expression, message: MSG, severity: :refactor)
end
end
end

def autocorrect(node)
lambda do |corrector|
corrector.remove(node.source_range)
end
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#
# Copyright:: 2016, Chris Henry
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'spec_helper'

describe RuboCop::Cop::Chef::ChefCorrectness::UnnecessaryNameProperty, :config do
subject(:cop) { described_class.new(config) }

# property :name, String
# property :name, String, name_property: true

it 'registers an offense when a resource has a property called name' do
expect_offense(<<~RUBY)
property :name, String
^^^^^^^^^^^^^^^^^^^^^^ There is no need to define a property named :name in a resource as Chef Infra defines that property for all resources out of the box.
RUBY

expect_correction("\n")
end

it 'registers an offense when a resource has a property called name that is a name_property' do
expect_offense(<<~RUBY)
property :name, String, name_property: true
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ There is no need to define a property named :name in a resource as Chef Infra defines that property for all resources out of the box.
RUBY

expect_correction("\n")
end

it 'does not register an offense when when a resource defined the name property with a default value' do
expect_no_offenses(<<~RUBY)
property :name, String, default: ''
RUBY
end
end

0 comments on commit f983f40

Please sign in to comment.