Skip to content

Commit

Permalink
Enable new cops available in 1.69
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Smith <[email protected]>
  • Loading branch information
tas50 committed Dec 7, 2024
1 parent 3ea9ea1 commit d685aa6
Show file tree
Hide file tree
Showing 13 changed files with 299 additions and 14 deletions.
140 changes: 140 additions & 0 deletions config/chefstyle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,146 @@ Lint/DuplicateRequire:
Lint/AmbiguousAssignment:
Enabled: true

# File.dirname(path, 2)
# instead of
# File.dirname(File.dirname(path))
Style/NestedFileDirname:
Enabled: true

# Use strings instead of regexes when possible
Style/ExactRegexpMatch:
Enabled: true

# Don't use redundant `any?`, `empty?`, `none?` or `one?` with `select`
Style/RedundantFilterChain:
Enabled: true

# Simplify creating arrays
Style/RedundantArrayConstructor:
Enabled: true

# Catch regex that tries to catch A-Z and a-z but fails
Lint/MixedCaseRange:
Enabled: true

# Use require_relative '/path'
# not
# require_relative './path
Style/RedundantCurrentDirectoryInPath:
Enabled: true

# Use strings instead of regex with common helper methods
Style/RedundantRegexpArgument:
Enabled: true

# Simplify loading of YAML from a file
Style/YAMLFileRead:
Enabled: true

# You can't have the same named group in a gemfile twice
Bundler/DuplicatedGroup:
Enabled: true

# Avoid creating and array and interating into it when .map can be used
Style/MapIntoArray:
Enabled: true

# Don't send method names when you don't need to
Style/SendWithLiteralMethodName:
Enabled: true

# add_runtime_dependencies is not preferred in gemspecs
Gemspec/AddRuntimeDependency:
Enabled: true

# Avoid numeric operators that do nothing to the value
Lint/UselessNumericOperation:
Enabled: true

# Don't define a set with duplicate elements since that defeats the point of a set
Lint/DuplicateSetElement:
Enabled: true

# Avoid calling defined twice when you don't need both
Style/CombinableDefined:
Enabled: true

# Catch malformed regexes
Lint/UnescapedBracketInRegexp:
Enabled: true

# Avoid defined calls that are always truthy
Lint/UselessDefined:
Enabled: true

# Call dig with multiple args instead of chaining it
Style/DigChain:
Enabled: true

# Catch deprecation hash creation in Ruby 3.4
Lint/HashNewWithKeywordArgumentsAsDefault:
Enabled: true

# Consistent magic comment formatting
Style/MagicCommentFormat:
Enabled: true

# Reduce duplicate comments
Lint/DuplicateMagicComment:
Enabled: true

# Don't escape strings when you don't need to
Style/RedundantStringEscape:
Enabled: true

# Don't double each something when you don't need to
Style/RedundantEach:
Enabled: true

# Use array.intersect? when on Ruby 3.1+
Style/ArrayIntersect:
Enabled: true

# Use .push to add to arrays which is much faster than .concat
Style/ConcatArrayLiterals:
Enabled: true

# Use .min and .max instead of rolling your own
Style/MinMaxComparison:
Enabled: true

# use .to_set instead of .map/.collect and then .to_set
Style/MapToSet:
Enabled: true

# Simplify how you check for empty files
Style/FileEmpty:
Enabled: true

# Simplify checking for empty directories
Style/DirEmpty:
Enabled: true

# Make it easier to read line continuations
Layout/LineContinuationSpacing:
Enabled: true

# Catch range statements that produce a Ruby warning
Lint/RequireRangeParentheses:
Enabled: true

# Don't attributes in the gemspec that are deprecated and/or ignored now
Gemspec/DeprecatedAttributeAssignment:
Enabled: true

# Don't .map and then .compact when you can use .selct or .reject instead
Style/MapCompactWithConditionalBlock:
Enabled: true

# Don't allow case statements with the same condition twice
Lint/DuplicateMatchPattern:
Enabled: true

Chef/Deprecations/Ruby27KeywordArgumentWarnings:
Description: Pass options to shell_out helpers without the brackets to avoid Ruby 2.7 deprecation warnings.
Enabled: true
Expand Down
147 changes: 146 additions & 1 deletion config/cookstyle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ AllCops:
SuggestExtensions: false
NewCops: disable
DisabledByDefault: true
TargetRubyVersion: 2.5
TargetRubyVersion: 2.6
TargetChefVersion: ~
inherit_mode:
merge:
Expand Down Expand Up @@ -3118,3 +3118,148 @@ Style/FileRead:
# reduce file write complexity
Style/FileWrite:
Enabled: true

# Improve readability of arrays for new users
# Prefer something.first over something[0]
Style/ArrayFirstLast:
Enabled: true

# File.dirname(path, 2)
# instead of
# File.dirname(File.dirname(path))
Style/NestedFileDirname:
Enabled: true

# Use strings instead of regexes when possible
Style/ExactRegexpMatch:
Enabled: true

# Don't use redundant `any?`, `empty?`, `none?` or `one?` with `select`
Style/RedundantFilterChain:
Enabled: true

# Simplify creating arrays
Style/RedundantArrayConstructor:
Enabled: true

# Catch regex that tries to catch A-Z and a-z but fails
Lint/MixedCaseRange:
Enabled: true

# Use require_relative '/path'
# not
# require_relative './path
Style/RedundantCurrentDirectoryInPath:
Enabled: true

# Use strings instead of regex with common helper methods
Style/RedundantRegexpArgument:
Enabled: true

# Simplify loading of YAML from a file
Style/YAMLFileRead:
Enabled: true

# You can't have the same named group in a gemfile twice
Bundler/DuplicatedGroup:
Enabled: true

# Avoid creating and array and interating into it when .map can be used
Style/MapIntoArray:
Enabled: true

# Don't send method names when you don't need to
Style/SendWithLiteralMethodName:
Enabled: true

# add_runtime_dependencies is not preferred in gemspecs
Gemspec/AddRuntimeDependency:
Enabled: true

# Avoid numeric operators that do nothing to the value
Lint/UselessNumericOperation:
Enabled: true

# Don't define a set with duplicate elements since that defeats the point of a set
Lint/DuplicateSetElement:
Enabled: true

# Avoid calling defined twice when you don't need both
Style/CombinableDefined:
Enabled: true

# Catch malformed regexes
Lint/UnescapedBracketInRegexp:
Enabled: true

# Avoid defined calls that are always truthy
Lint/UselessDefined:
Enabled: true

# Call dig with multiple args instead of chaining it
Style/DigChain:
Enabled: true

# Catch deprecation hash creation in Ruby 3.4
Lint/HashNewWithKeywordArgumentsAsDefault:
Enabled: true

# Consistent magic comment formatting
Style/MagicCommentFormat:
Enabled: true

# Reduce duplicate comments
Lint/DuplicateMagicComment:
Enabled: true

# Don't escape strings when you don't need to
Style/RedundantStringEscape:
Enabled: true

# Don't double each something when you don't need to
Style/RedundantEach:
Enabled: true

# Use array.intersect? when on Ruby 3.1+
Style/ArrayIntersect:
Enabled: true

# Use .push to add to arrays which is much faster than .concat
Style/ConcatArrayLiterals:
Enabled: true

# Use .min and .max instead of rolling your own
Style/MinMaxComparison:
Enabled: true

# use .to_set instead of .map/.collect and then .to_set
Style/MapToSet:
Enabled: true

# Simplify how you check for empty files
Style/FileEmpty:
Enabled: true

# Simplify checking for empty directories
Style/DirEmpty:
Enabled: true

# Make it easier to read line continuations
Layout/LineContinuationSpacing:
Enabled: true

# Catch range statements that produce a Ruby warning
Lint/RequireRangeParentheses:
Enabled: true

# Don't attributes in the gemspec that are deprecated and/or ignored now
Gemspec/DeprecatedAttributeAssignment:
Enabled: true

# Don't .map and then .compact when you can use .selct or .reject instead
Style/MapCompactWithConditionalBlock:
Enabled: true

# Don't allow case statements with the same condition twice
Lint/DuplicateMatchPattern:
Enabled: true
2 changes: 1 addition & 1 deletion lib/rubocop/chef/cookbook_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def method_arg_ast_to_string(ast)
return if ast.children[2].nil?
# https://rubular.com/r/6uzOMd6WCHewOu
m = ast.children[2].source.match(/^("|')(.*)("|')$/)
return m[2] unless m.nil?
m[2] unless m.nil?
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/chef/correctness/tmp_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def hardcoded_tmp?(path)

def file_cache_path?(path)
path_str = path.to_s.scan(/"(.*)"/)[0][0]
path_str.start_with?("\#\{Chef::Config[:file_cache_path]\}")
path_str.start_with?("\#{Chef::Config[:file_cache_path]}")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/chef/modernize/execute_tzutil.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def on_block(node)

def calls_tzutil?(ast_obj)
property_data = method_arg_ast_to_string(ast_obj)
return true if property_data && property_data.match?(/^tzutil/i)
true if property_data && property_data.match?(/^tzutil/i)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/chef/style/comments_copyright_format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def invalid_copyright_comment?(comment)
current_text = match.captures[0]

Check failure on line 92 in lib/rubocop/cop/chef/style/comments_copyright_format.rb

View workflow job for this annotation

GitHub Actions / cookstyle

[Correctable] Style/ArrayFirstLast: Use first. (#first-and-last)
desired_text = "#{copyright_date_range(comment)}, #{copyright_holder(comment)}"

return true unless current_text == desired_text
true unless current_text == desired_text
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/chef/style/comments_default_copyright.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def on_new_investigation
return unless processed_source.ast

processed_source.comments.each do |comment|
next unless comment.inline? && # headers aren't in blocks
next unless comment.inline? && # headers aren't in blocks
/# (?:Copyright\W*).*YOUR_(NAME|COMPANY_NAME)/.match?(comment.text)
add_offense(comment, severity: :refactor)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/chef/style/file_mode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def on_send(node)

# we build our own escaped string instead of using .inspect because that way
# we can use single quotes instead of the double quotes that .inspect adds
corrector.replace(mode_int, "\'#{replacement_mode}\'")
corrector.replace(mode_int, "'#{replacement_mode}'")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/monkey_patches/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def roundup_relevant_cops(processed_source)
next true if processed_source.comment_config.cop_opted_in?(cop)
next false if cop.excluded_file?(processed_source.file_path)
next false unless @registry.enabled?(cop, @config)

support_target_ruby_version?(cop) && support_target_rails_version?(cop) && support_target_chef_version?(cop)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
it 'registers an offense when a cookbook contains an invalid supports platform' do
expect_offense(<<~RUBY)
supports 'darwin'
^^^^^^^^ metadata.rb \"supports\" platform is invalid
^^^^^^^^ metadata.rb "supports" platform is invalid
depends 'foo'
RUBY

Expand All @@ -34,7 +34,7 @@
it 'registers offenses for invalid platforms defined in a loop' do
expect_offense(<<~RUBY)
%w(ubuntu centos redhat fedora amazon rhel).each do |os|
^^^^ metadata.rb \"supports\" platform is invalid
^^^^ metadata.rb "supports" platform is invalid
supports os
end
RUBY
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/cop/chef/correctness/tmp_path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

it "doesn't register an offense when using file_cache_path" do
expect_no_offenses(<<~RUBY)
remote_file "\#\{Chef::Config[:file_cache_path]\}/large-file.tar.gz" do
remote_file "\#{Chef::Config[:file_cache_path]}/large-file.tar.gz" do
source 'http://www.example.org/large-file.tar.gz'
end
RUBY
Expand Down
Loading

0 comments on commit d685aa6

Please sign in to comment.