Class: RuboCop::Cop::InternalAffairs::ExampleDescription

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

Overview

Checks that RSpec examples that use ‘expects_offense` or `expects_no_offenses` do not have conflicting descriptions.

Examples:

# bad
it 'does not register an offense' do
  expect_offense('...')
end

it 'registers an offense' do
  expect_no_offenses('...')
end

# good
it 'registers an offense' do
  expect_offense('...')
end

it 'does not register an offense' do
  expect_no_offenses('...')
end

Constant Summary collapse

MSG =
'Description does not match use of `%<method_name>s`.'
RESTRICT_ON_SEND =
%i[
  expect_offense
  expect_no_offenses
  expect_correction
  expect_no_corrections
].to_set.freeze
EXPECT_NO_OFFENSES_DESCRIPTION_MAPPING =
{
  /\A(adds|registers|reports|finds) (an? )?offense/ => 'does not register an offense',
  /\A(flags|handles|works)\b/ => 'does not register'
}.freeze
EXPECT_OFFENSE_DESCRIPTION_MAPPING =
{
  /\A(does not|doesn't) (register|find|flag|report)/ => 'registers',
  /\A(does not|doesn't) add (a|an|any )?offense/ => 'registers an offense',
  /\Aregisters no offense/ => 'registers an offense',
  /\A(accepts|register)\b/ => 'registers'
}.freeze
EXPECT_NO_CORRECTIONS_DESCRIPTION_MAPPING =
{
  /\A(auto[- ]?)?correct/ => 'does not correct'
}.freeze
EXPECT_CORRECTION_DESCRIPTION_MAPPING =
{
  /\b(does not|doesn't) (auto[- ]?)?correct/ => 'autocorrects'
}.freeze
EXAMPLE_DESCRIPTION_MAPPING =
{
  expect_no_offenses: EXPECT_NO_OFFENSES_DESCRIPTION_MAPPING,
  expect_offense: EXPECT_OFFENSE_DESCRIPTION_MAPPING,
  expect_no_corrections: EXPECT_NO_CORRECTIONS_DESCRIPTION_MAPPING,
  expect_correction: EXPECT_CORRECTION_DESCRIPTION_MAPPING
}.freeze

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

#offense_example(node) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/rubocop/cop/internal_affairs/example_description.rb', line 68

def_node_matcher :offense_example, <<~PATTERN
  (block
    (send _ {:it :specify} $...)
    _args
    `(send nil? %RESTRICT_ON_SEND ...)
  )
PATTERN

#on_send(node) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/rubocop/cop/internal_affairs/example_description.rb', line 76

def on_send(node)
  parent = node.each_ancestor(:block).first
  return unless parent && (current_description = offense_example(parent)&.first)

  method_name = node.method_name
  message = format(MSG, method_name: method_name)

  description_map = EXAMPLE_DESCRIPTION_MAPPING[method_name]
  check_description(current_description, description_map, message)
end