-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #316 from chef/unnecessary_name_prop
Add ChefCorrectness/UnnecessaryNameProperty
- Loading branch information
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
lib/rubocop/cop/chef/correctness/unnecessary_name_property.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
48 changes: 48 additions & 0 deletions
48
spec/rubocop/cop/chef/correctness/unnecessary_name_property_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |