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

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

Constant Summary collapse

MSG =
'Description should be started with %<suggestion>s instead of `This cop ...`.'
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

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, autocorrect_incompatible_with, badge, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, #cop_name, cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #ready, 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

rubocop:disable Metrics/CyclomaticComplexity


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rubocop/cop/internal_affairs/cop_description.rb', line 32

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

  description_beginning = first_comment_line(module_node)
  return unless description_beginning

  start_with_subject = description_beginning.match(COP_DESC_OFFENSE_REGEX)
  return unless start_with_subject

  suggestion = start_with_subject['word']&.capitalize
  range = range(module_node, description_beginning)
  suggestion_for_message = suggestion_for_message(suggestion, start_with_subject)
  message = format(MSG, suggestion: suggestion_for_message)

  add_offense(range, message: message) do |corrector|
    if suggestion && !start_with_subject['special']
      replace_with_suggestion(corrector, range, suggestion, description_beginning)
    end
  end
end