Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ChefCorrectness/UnnecessaryNameProperty #316

Merged
merged 2 commits into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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