Class: Shirobai::Cop::Performance::StartWith

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

Overview

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

Mirror image of Shirobai::Cop::Performance::EndWith: the Rust side gates on literal_at_start? (\A, or ^ when SafeMultiline is false) and the wrapper drops the anchor with the stock drop_start_metacharacter before rebuilding the replacement with RuboCop's own string helpers.

Constant Summary collapse

MSG =
"Use `String#start_with?` instead of a regex match anchored " \
"to the beginning of the string."

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



23
# File 'lib/shirobai/cop/performance/start_with.rb', line 23

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

.bundle_args(config) ⇒ Object

Packed args for the bundled run: [safe_multiline] (0/1).



26
27
28
# File 'lib/shirobai/cop/performance/start_with.rb', line 26

def self.bundle_args(config)
  [config.for_badge(badge).fetch("SafeMultiline", true) ? 1 : 0]
end

.cop_nameObject



22
# File 'lib/shirobai/cop/performance/start_with.rb', line 22

def self.cop_name = "Performance/StartWith"

Instance Method Details

#on_new_investigationObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/shirobai/cop/performance/start_with.rb', line 30

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, recv_start, recv_end, dot, content|
    range = Parser::Source::Range.new(buffer, off[start], off[fin])
    add_offense(range) do |corrector|
      receiver_source =
        Parser::Source::Range.new(buffer, off[recv_start], off[recv_end]).source
      literal = to_string_literal(
        interpret_string_escapes(drop_start_metacharacter(content))
      )
      corrector.replace(range, "#{receiver_source}#{dot}start_with?(#{literal})")
    end
  end
end