class RuboCop::Cop::Chef::Style::CopyrightCommentFormat
Checks for incorrectly formatted copyright comments.
@example
#### incorrect Copyright:: 2013-2022 Opscode, Inc. Copyright:: 2013-2022 Chef Inc. Copyright:: 2013-2022 Chef Software Inc. Copyright:: 2009-2010 2013-2022 Chef Software Inc. Copyright:: Chef Software Inc. Copyright:: Tim Smith Copyright:: Copyright (c) 2015-2022 Chef Software, Inc. #### correct Copyright:: 2013-2022 Chef Software, Inc. Copyright:: 2013-2022 Tim Smith Copyright:: 2019 37Signals, Inc.
Constants
- MSG
Public Instance Methods
on_new_investigation()
click to toggle source
# File lib/rubocop/cop/chef/style/comments_copyright_format.rb, line 46 def on_new_investigation return unless processed_source.ast processed_source.comments.each do |comment| next unless comment.inline? && invalid_copyright_comment?(comment) # headers aren't in blocks add_offense(comment, severity: :refactor) do |corrector| correct_comment = "# Copyright:: #{copyright_date_range(comment)}, #{copyright_holder(comment)}" corrector.replace(comment, correct_comment) end end end
Private Instance Methods
copyright_date_range(comment)
click to toggle source
# File lib/rubocop/cop/chef/style/comments_copyright_format.rb, line 61 def copyright_date_range(comment) dates = comment.text.scan(/([0-9]{4})/) # no copyright year present so return this year return Time.new.year if dates.empty? oldest_date = dates.min[0].to_i # Avoid returning THIS_YEAR - THIS_YEAR if oldest_date == Time.new.year oldest_date else "#{oldest_date}-#{Time.new.year}" end end
copyright_holder(comment)
click to toggle source
# File lib/rubocop/cop/chef/style/comments_copyright_format.rb, line 77 def copyright_holder(comment) # Grab just the company / individual name w/o :: or dates match = /(?:.*[0-9]{4}|Copyright\W*)(?:,)?(?:\s)?(.*)/.match(comment.text) marketing_sanitizer(match.captures[0]) end
invalid_copyright_comment?(comment)
click to toggle source
# File lib/rubocop/cop/chef/style/comments_copyright_format.rb, line 88 def invalid_copyright_comment?(comment) match = /# (?:Copyright\W*)(.*)/.match(comment.text) return false unless match # it's not even a copyright current_text = match.captures[0] desired_text = "#{copyright_date_range(comment)}, #{copyright_holder(comment)}" return true unless current_text == desired_text end
marketing_sanitizer(name)
click to toggle source
Flush Opscode down the memory hole and Chef
Inc is not a company
# File lib/rubocop/cop/chef/style/comments_copyright_format.rb, line 84 def marketing_sanitizer(name) name.gsub('Opscode', 'Chef Software').gsub(/Chef(?:,)? Inc.*/, 'Chef Software, Inc.') end