Skip to content

ExcessiveDocstringSpacing does not distinguish between actual example groups and calls to methods with the same name. #2103

@eric-wtfoxtrot

Description

@eric-wtfoxtrot

Hello

We noticed that ExcessiveDocstringSpacing makes changes to code locations that should not actually be handled by this cop. The pattern used by this cop does not take into account the receiver of a method call. If we call a method that is named like one of the methods from RSpecs example (it, ...) or example group (context, describe, ...), it will be flagged by this cop.


Expected behavior

it 'is demo time' do
  # this call to 'context' should be ignored by the cop, because the receiver is neither RSpec nor nil in the ast
  MyModel.context('   has   many    blanks   ')
end

Actual behavior

it 'is demo time' do
  # this will get flagged and changed (-a) by rubocop, which leads to a broken test in our case
  MyModel.context('   has   many    blanks   ')
end

Steps to reproduce the problem

Add the following line to any spec file and run rubocop.
MyModel.context(' has many blanks ')

A possible solution

Changing the pattern from send _ to send #rspec? seems to fix the problem. However, we are not entirely sure what the consequences of this change will be.

        def_node_matcher :example_description, <<~PATTERN
          (send #rspec? {#Examples.all #ExampleGroups.all} ${
            $str
            $(dstr ({str dstr `sym} ...) ...)
          } ...)
        PATTERN

RuboCop RSpec version

❯ rubocop -V
1.80.0 (using Parser 3.3.9.0, Prism 1.4.0, rubocop-ast 1.46.0, analyzing as Ruby 3.4, running on ruby 3.4.2) +server [arm64-darwin23]
  - rubocop-capybara 2.22.1
  - rubocop-factory_bot 2.27.1
  - rubocop-performance 1.25.0
  - rubocop-rails 2.32.0
  - rubocop-rake 0.7.1
  - rubocop-rspec 3.6.0
  - rubocop-rspec_rails 2.31.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions