Class: RuboCop::Cop::InternalAffairs::CopDescription

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/internal_affairs/cop_description.rb

Overview

Enforces the cop description to start with a word such as verb.

Examples:

# bad
# This cop checks ....
class SomeCop < Base
  ....
end

# bad
#
# Checks ...
class SomeCop < Base
  ...
end

# good
# Checks ...
class SomeCop < Base
  ...
end

Constant Summary collapse

MSG_STARTS_WITH_WRONG_WORD =
'Description should be started with %<suggestion>s instead of `This cop ...`.'
MSG_STARTS_WITH_EMPTY_COMMENT_LINE =
'Description should not start with an empty comment line.'
SPECIAL_WORDS =
%w[is can could should will would must may].freeze
COP_DESC_OFFENSE_REGEX =
/^\s+# This cop (?<special>#{SPECIAL_WORDS.join('|')})?\s*(?<word>.+?) .*/.freeze
REPLACEMENT_REGEX =
/^\s+# This cop (#{SPECIAL_WORDS.join('|')})?\s*(.+?) /.freeze
EMPTY_COMMENT_LINE_REGEX =
/\A\s*#\s*\n\z/.freeze

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from RuboCop::Cop::IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#on_class(node) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rubocop/cop/internal_affairs/cop_description.rb', line 42

def on_class(node)
  return unless (module_node = node.parent) && node.parent_class

  description_beginning = first_comment_line(module_node)
  return unless description_beginning

  if description_beginning.match?(EMPTY_COMMENT_LINE_REGEX)
    register_offense_for_empty_comment_line(module_node, description_beginning)
  else
    start_with_subject = description_beginning.match(COP_DESC_OFFENSE_REGEX)
    return unless start_with_subject

    register_offense_for_wrong_word(module_node, description_beginning, start_with_subject)
  end
end