Class: Shirobai::Cop::Style::StringLiteralsInInterpolation

Inherits:
RuboCop::Cop::Base
  • Object
show all
Extended by:
RuboCop::Cop::AutoCorrector
Includes:
RuboCop::Cop::ConfigurableEnforcedStyle, RuboCop::Cop::Util
Defined in:
lib/shirobai/cop/style/string_literals_in_interpolation.rb

Overview

Drop-in Rust reimplementation of ‘Style/StringLiteralsInInterpolation`.

The interpolation counterpart of ‘Style/StringLiterals`: Rust walks every string literal once, replicating stock’s ‘StringHelp#on_str` with this cop’s inverted interpolation guard. A ‘:str` node offends iff it is inside an interpolation (`#…` of a string / symbol / regexp) and its quotes are `wrong_quotes?` for the configured `EnforcedStyle`; a non-offending `:str` with a begin loc emits a `correct_style_detected` marker. Heredoc and physical-newline (multi-line) `:str` nodes are skipped exactly as parser-gem skips them (no begin loc / `:dstr` split).

For each record Rust returns, in walk order, whether it offends, the caret range, the detection marker to replay, and — for an offense — which autocorrect to apply (‘single` / `double`) plus the decoded string content. The replacement text is computed here with stock’s genuine ‘RuboCop::Cop::Util` helpers (`to_string_literal` / `String#inspect`), and the detection markers are replayed through the genuine `ConfigurableEnforcedStyle` methods so `config_to_allow_offenses` matches stock exactly.

Always bundle-eligible: the result is purely config-driven.

Constant Summary collapse

STYLES =
{
  "single_quotes" => 0,
  "double_quotes" => 1
}.freeze
FIX_SINGLE =

Fix kinds (mirror ‘string_literals_in_interpolation.rs`).

0
FIX_DOUBLE =
1

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



42
# File 'lib/shirobai/cop/style/string_literals_in_interpolation.rb', line 42

def self.badge = RuboCop::Cop::Badge.parse("Style/StringLiteralsInInterpolation")

.bundle_args(config) ⇒ Object

Packed config nums: ‘[style]`.



45
46
47
48
49
50
# File 'lib/shirobai/cop/style/string_literals_in_interpolation.rb', line 45

def self.bundle_args(config)
  cop_config = config.for_badge(badge)
  # An unrecognized `EnforcedStyle` defaults to single here; the genuine
  # error is raised by the `style` accessor in `on_new_investigation`.
  [[STYLES.fetch(cop_config["EnforcedStyle"] || "single_quotes", 0)]]
end

.cop_nameObject



41
# File 'lib/shirobai/cop/style/string_literals_in_interpolation.rb', line 41

def self.cop_name = "Style/StringLiteralsInInterpolation"

Instance Method Details

#alternative_styleObject



76
77
78
79
80
81
82
83
# File 'lib/shirobai/cop/style/string_literals_in_interpolation.rb', line 76

def alternative_style
  case style
  when :single_quotes
    :double_quotes
  else
    :single_quotes
  end
end

#on_new_investigationObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/shirobai/cop/style/string_literals_in_interpolation.rb', line 52

def on_new_investigation
  # Validate `EnforcedStyle` through the genuine accessor first: stock
  # raises `RuntimeError` for an unrecognized style, and this must fire
  # before we derive the bundle config (which would otherwise raise a
  # `KeyError` for the unknown key).
  style

  buffer = processed_source.buffer
  off = SourceOffsets.for(processed_source.raw_source)

  resolved_result.each do |is_offense, start, fin, detect, fix, content|
    unless is_offense
      replay_detection(detect)
      next
    end

    range = Parser::Source::Range.new(buffer, off[start], off[fin])
    add_offense(range, message: message) do |corrector|
      apply_fix(corrector, range, fix, content)
      replay_detection(detect)
    end
  end
end