Class: RuboCop::Cop::Chef::ChefStyle::CopyrightCommentFormat

Inherits:
RuboCop::Cop
  • Object
show all
Defined in:
lib/rubocop/cop/chef/style/comments_copyright_format.rb

Overview

Checks for incorrectly formatted copyright comments.

Examples:


# bad (assuming current year is 2019)
Copyright:: 2013-2019 Opscode, Inc.
Copyright:: 2013-2019 Chef Inc.
Copyright:: 2013-2019 Chef Software Inc.
Copyright:: 2009-2010 2013-2019 Chef Software Inc.
Copyright:: Chef Software Inc.
Copyright:: Tim Smith
Copyright:: Copyright (c) 2015-2019 Chef Software, Inc.

# good (assuming current year is 2019)
Copyright:: 2013-2019 Chef Software, Inc.
Copyright:: 2013-2019 Tim Smith
Copyright:: 2019 37Signals, Inc.

Constant Summary collapse

MSG =
'Properly format copyrights header comments'.freeze

Instance Method Summary collapse

Instance Method Details

#autocorrect(comment) ⇒ Object



57
58
59
60
# File 'lib/rubocop/cop/chef/style/comments_copyright_format.rb', line 57

def autocorrect(comment)
  correct_comment = "# Copyright:: #{copyright_date_range(comment)}, #{copyright_holder(comment)}"
  ->(corrector) { corrector.replace(comment.loc.expression, correct_comment) }
end

#investigate(processed_source) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rubocop/cop/chef/style/comments_copyright_format.rb', line 45

def investigate(processed_source)
  return unless processed_source.ast

  processed_source.comments.each do |comment|
    next unless comment.inline? # headers aren't in blocks

    if invalid_copyright_comment?(comment)
      add_offense(comment, location: comment.loc.expression, message: MSG, severity: :refactor)
    end
  end
end