Class: RuboCop::Cop::Kaizo::SpecComment

Inherits:
Base
  • Object
show all
Includes:
AllowedPattern
Defined in:
lib/rubocop/cop/kaizo/spec_comment.rb

Overview

Checks for comments in spec files.

A comment in a spec is almost always a sign that the spec is doing the job of its own description. If you need a sentence to explain what an example sets up or asserts, that sentence usually wants to be a context/it description, a clearer example name, or another example -- not prose riding alongside the code.

By default only *_spec.rb files are inspected, and spec/helpers and spec/support are excluded -- they hold infrastructure, not specs. Magic comments (# frozen_string_literal: true, # encoding: ...), RuboCop directives (any # rubocop: comment), and shebangs are never flagged; add further exemptions with AllowedPatterns. There is no autocorrection: turning an explanation into a spec is a design decision.

Configuration

[+Include+] Files the cop runs on. Default: **/_spec.rb. Broaden it to cover support files or a Minitest suite. [+Exclude+] Files the cop skips even when included. Default: **/spec/helpers/**/ and /spec/support/**/*. Set it to [] to inspect those too. [+AllowedPatterns+] Regexps matched against the full comment text, leading # included; a match is exempt. Default: none.

Kaizo/SpecComment:
inherit_mode:
  merge:
    - Include
Include:
  - '**/*_test.rb'     # Minitest too
AllowedPatterns:
  - '\A#\s*@rbs'       # rbs-inline type annotations

Examples:

# bad
it 'permits the request' do
  # an admin can see everything
  user = create(:user, admin: true)
  expect(policy).to permit(user)
end

# good
it 'permits an admin to see everything' do
  admin = create(:user, admin: true)
  expect(policy).to permit(admin)
end

Constant Summary collapse

MSG =
"Avoid comments in specs. Express the intent as a `context`/`it` " \
"description or a clearer example instead.".freeze

Instance Method Summary collapse

Instance Method Details

#on_new_investigationObject



60
61
62
63
64
65
66
# File 'lib/rubocop/cop/kaizo/spec_comment.rb', line 60

def on_new_investigation
  processed_source.comments.each do |comment|
    next if allowed?(comment)

    add_offense(comment)
  end
end