Check blocks with _1 and it in RSpec/ExampleStartingCharacter

What does this MR do?

  • Makes RSpec/ExampleStartingCharacter check examples that use numbered parameters (_1) or Ruby 3.4's it, which it skipped until now.
  • Fixes its autocorrect raising FrozenError in projects that target Ruby 3.4.

Related to #94 (closed). !293 (merged) fixes Cop/AvoidReturnFromBlocks the same way.

it('Does something') { |x| expect(x).to be }  # offense, before and after
it('Does something') { expect(_1).to be }     # offense only after this MR
Cause and fix
  • Skipped blocks: the cop sends numblock and itblock nodes to on_block, but the it_description matcher only matched (block ...). It now matches (any_block ...).
  • FrozenError: the autocorrect called strip! on the string node's value. When parsing Ruby 3.4, that value is frozen, so correcting a plain-string description raised FrozenError, for regular blocks too. It now uses strip. The it examples need Ruby 3.4, so they hit this.
Specs

The new example runs with _1, and with it under :ruby34. Reverting the matcher fails both. Reverting strip fails the Ruby 3.4 one with FrozenError.

Impact: projects using gitlab-styles may get new offenses for descriptions of examples that use _1 or it.

Edited by Abdul Wadood

Merge request reports

Loading
Loading