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

Inherits:
Base
  • Object
show all
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_INCORRECT_DESCRIPTIONS =
[
  /^(adds|registers|reports|finds) (an? )?offense/,
  /^(flags|handles|works)\b/
].freeze
EXPECT_OFFENSE_INCORRECT_DESCRIPTIONS =
[
  /^(does not|doesn't) (register|find|flag|report)/,
  /^(does not|doesn't) add (a|an|any )?offense/
].freeze
EXPECT_NO_CORRECTIONS_INCORRECT_DESCRIPTIONS =
[/^(auto[- ]?)?correct/].freeze
EXPECT_CORRECTION_INCORRECT_DESCRIPTIONS =
[
  /\b(does not|doesn't) (auto[- ]?)?correct/
].freeze

Class Attribute Summary collapse

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods inherited from Base

#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, #relevant_file?, 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?, #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

Class Attribute Details

.descriptionsObject

Returns the value of attribute descriptions.


30
31
32
# File 'lib/rubocop/cop/internal_affairs/example_description.rb', line 30

def descriptions
  @descriptions
end

Instance Method Details

#offense_example?(node) ⇒ Object


59
60
61
62
63
64
65
# File 'lib/rubocop/cop/internal_affairs/example_description.rb', line 59

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

#on_send(node) ⇒ Object


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

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

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

  regexp_group = self.class.const_get("#{method_name}_incorrect_descriptions".upcase)
  check_description(description, regexp_group, message)
end