class RuboCop::Cop::Chef::Style::CommentFormat

Checks for incorrectly formatted headers

@example

#### incorrect
Copyright 2013-2016 Chef Software, Inc.
Recipe default.rb
Attributes default.rb
License Apache2
Cookbook tomcat
Cookbook Name:: Tomcat
Attributes File:: default

#### correct
Copyright:: 2013-2016 Chef Software, Inc.
Recipe:: default.rb
Attributes:: default.rb
License:: Apache License, Version 2.0
Cookbook:: Tomcat

Constants

CHEF_LIKE_COMMENT_REGEX
MSG
VERBOSE_COMMENT_REGEX

Public Instance Methods

on_new_investigation() click to toggle source
# File lib/rubocop/cop/chef/style/comments_format.rb, line 49
def on_new_investigation
  return unless processed_source.ast

  processed_source.comments.each do |comment|
    next if comment.loc.first_line > 10 # avoid false positives when we were checking further down the file
    next unless comment.inline? && CHEF_LIKE_COMMENT_REGEX.match?(comment.text) # headers aren't in blocks

    add_offense(comment, message: MSG, severity: :refactor) do |corrector|
      # Extract the type and the actual value. Strip out "Name" or "File"
      # 'Cookbook Name' should be 'Cookbook'. Also skip a :: if present
      # https://rubular.com/r/Do9fpLWXlCmvdJ
      match = VERBOSE_COMMENT_REGEX.match(comment.text)
      comment_type, value = match.captures
      correct_comment = "# #{comment_type}:: #{value}"
      corrector.replace(comment, correct_comment)
    end
  end
end