Class: Shirobai::Cop::Performance::Detect

Inherits:
RuboCop::Cop::Base
  • Object
show all
Extended by:
RuboCop::Cop::AutoCorrector
Includes:
BundleEligible
Defined in:
lib/shirobai/cop/performance/detect.rb

Overview

Drop-in Rust reimplementation of Performance/Detect (rubocop-performance 1.26.1).

Rust replicates the four stock pattern branches (select / find_all / filter chained with first / last / [0] / [-1], block and block-pass forms) including the accept_first_call? gates (empty block body, non-block-pass args, lazy chains) and the parser-semantics offense range (inner selector through outer selector, where the sugar index form's selector is the whole [0] bracket construct). The wrapper applies the Rust-computed removal/replacement ranges — including stock's knowingly broken rewrite of the explicit .[](0) form, byte for byte. Messages come from Rust, formatted with the preferred method resolved from Style/CollectionMethods below.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



25
# File 'lib/shirobai/cop/performance/detect.rb', line 25

def self.badge = RuboCop::Cop::Badge.parse(cop_name)

.bundle_args(config) ⇒ Object

Packed args for the bundled run: [preferred_method]. Stock reads config.for_cop('Style/CollectionMethods')['PreferredMethods']['detect'] and falls back to detect; the extra || {} only guards configs where stock would crash (bare RuboCop::Config.new without the default PreferredMethods hash — packing runs for every config, not just the ones that instantiate this cop).



33
34
35
36
37
38
# File 'lib/shirobai/cop/performance/detect.rb', line 33

def self.bundle_args(config)
  preferred =
    (config.for_cop("Style/CollectionMethods")["PreferredMethods"] || {})["detect"] ||
    "detect"
  [preferred.to_s]
end

.cop_nameObject



24
# File 'lib/shirobai/cop/performance/detect.rb', line 24

def self.cop_name = "Performance/Detect"

Instance Method Details

#on_new_investigationObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/shirobai/cop/performance/detect.rb', line 40

def on_new_investigation
  buffer = processed_source.buffer
  source = bundle_eligible? ? processed_source.raw_source : buffer.source
  off = SourceOffsets.for(source)
  resolved_offenses.each do |sel_start, sel_end, recv_end, outer_end, message, replacement|
    range = Parser::Source::Range.new(buffer, off[sel_start], off[outer_end])
    add_offense(range, message: message) do |corrector|
      corrector.remove(Parser::Source::Range.new(buffer, off[recv_end], off[outer_end]))
      corrector.replace(
        Parser::Source::Range.new(buffer, off[sel_start], off[sel_end]), replacement
      )
    end
  end
end