Module: RuboCop::RSpec::Corrector::SigAwareMoveNode

Extended by:
AST::NodePattern::Macros
Defined in:
lib/rubocop/gusto/move_node_patch.rb

Overview

Patches MoveNode to treat Sorbet sig { ... } blocks as part of the let/subject/hook they precede.

Sorbet's RSpec mode attaches type signatures to memoized helpers via a sig block immediately above the declaration:

sig { returns(Something) }
let(:thing) { create(:something) }

Several rubocop-rspec cops use MoveNode to relocate let/subject/ hook nodes (ScatteredLet, LeadingSubject, LetBeforeExamples, HooksBeforeExamples). Without this patch, the move strands the sig at the original location. This patch:

  1. Extends the source range of the moved node to include a preceding sig block, so sig is carried with the move.
  2. In move_before, redirects the insertion point above a preceding sig on the destination, so the destination's sig pairing stays intact.

Instance Method Summary collapse

Instance Method Details

#move_before(other) ⇒ Object



35
36
37
38
# File 'lib/rubocop/gusto/move_node_patch.rb', line 35

def move_before(other)
  sig = preceding_sig_block(other)
  super(sig || other)
end

#sig_block?(node) ⇒ Object



31
32
33
# File 'lib/rubocop/gusto/move_node_patch.rb', line 31

def_node_matcher :sig_block?, <<~PATTERN
  (block (send nil? :sig ...) _ _)
PATTERN