Class: Shirobai::Cop::Performance::StringInclude

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

Overview

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

Rust replicates the stock pattern union in document order (regexp argument first, then regexp receiver; !~ and &. only match on the argument side) and the Util::LITERAL_REGEX literal-only gate over the raw pattern source. The wrapper rebuilds the replacement exactly like stock — with RuboCop's own interpret_string_escapes and to_string_literal helpers (available through RuboCop::Cop::Base -> include Util) — so escape interpretation and quote selection cannot drift from stock byte behavior.

Constant Summary collapse

MSG =
"Use `%<negation>sString#include?` instead of a regex match " \
"with literal-only pattern."

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



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

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

.bundle_args(_config) ⇒ Object

Config-less: nothing to pack (the shared segment only carries the department enable flag for this cop).



29
30
31
# File 'lib/shirobai/cop/performance/string_include.rb', line 29

def self.bundle_args(_config)
  []
end

.cop_nameObject



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

def self.cop_name = "Performance/StringInclude"

Instance Method Details

#on_new_investigationObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/shirobai/cop/performance/string_include.rb', line 33

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 |start, fin, negation, recv_start, recv_end, dot, content|
    negation = negation == 1
    range = Parser::Source::Range.new(buffer, off[start], off[fin])
    message = format(MSG, negation: (negation ? "!" : ""))
    add_offense(range, message: message) do |corrector|
      receiver_source =
        Parser::Source::Range.new(buffer, off[recv_start], off[recv_end]).source
      literal = to_string_literal(interpret_string_escapes(content))
      new_source =
        "#{'!' if negation}#{receiver_source}#{dot}include?(#{literal})"
      corrector.replace(range, new_source)
    end
  end
end