Module: Shirobai::Cop::RSpec::MetadataSupport

Extended by:
RuboCop::AST::NodePattern::Macros
Includes:
RuboCop::RSpec::Language, BundleEligible
Included in:
DuplicatedMetadata, EmptyMetadata, MetadataStyle, SortMetadata
Defined in:
lib/shirobai/cop/rspec/metadata_support.rb

Overview

Shared harness for the four Metadata-mixin cops (MetadataStyle, DuplicatedMetadata, EmptyMetadata, SortMetadata).

Rust identifies the metadata-anchor BLOCK ranges once on the shared walk (direct example/group/hook blocks plus RSpec.configure blocks). This module relocates each parser block node and runs stock's RuboCop::Cop::RSpec::Metadata#on_block logic VERBATIM on the real parser AST — so block-kind / receiver / arity filtering and the metadata_in_block search happen exactly as stock does them, and each cop's on_metadata(symbols, hash) sees identical (symbols, hash).

The stock entry method is renamed (process_metadata_block) so the Commissioner never dispatches a per-node on_block; only on_new_investigation is a real callback.

Instance Method Summary collapse

Instance Method Details

#metadata_in_block(node) ⇒ Object



45
46
47
# File 'lib/shirobai/cop/rspec/metadata_support.rb', line 45

def_node_search :metadata_in_block, <<~PATTERN
  (send (lvar %) #Hooks.all _ $...)
PATTERN

#on_metadata(_symbols, _hash) ⇒ Object

Raises:

  • (::NotImplementedError)


65
66
67
# File 'lib/shirobai/cop/rspec/metadata_support.rb', line 65

def (_symbols, _hash)
  raise ::NotImplementedError
end

#on_new_investigationObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/shirobai/cop/rspec/metadata_support.rb', line 49

def on_new_investigation
  RuboCop::RSpec::Language.config = config["RSpec"]["Language"]
  ranges = resolved_anchors
  return if ranges.nil? || ranges.empty?

  source = bundle_eligible? ? processed_source.raw_source : processed_source.buffer.source
  off = SourceOffsets.for(source)
  keys = ranges.map { |(start, fin)| [off[start], off[fin]] }
  located = Shirobai::RSpec::NodeLocator.locate(processed_source, keys)

  keys.each do |key|
    node = located[key]
    (node) if node
  end
end

#rspec_configure(node) ⇒ Object



40
41
42
# File 'lib/shirobai/cop/rspec/metadata_support.rb', line 40

def_node_matcher :rspec_configure, <<~PATTERN
  (block (send #rspec? :configure) (args (arg $_)) ...)
PATTERN

#rspec_metadata(node) ⇒ Object



32
33
34
35
36
37
# File 'lib/shirobai/cop/rspec/metadata_support.rb', line 32

def_node_matcher :rspec_metadata, <<~PATTERN
  (block
    (send
      #rspec? {#Examples.all #ExampleGroups.all #SharedGroups.all #Hooks.all} _ $...)
    ...)
PATTERN