Class: Shirobai::Cop::Style::StringLiterals

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.rb

Overview

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

Rust walks every string literal once, replicating stock’s two callbacks: ‘StringHelp#on_str` (the per-`:str` quote check, with the begin-loc / heredoc / ignored-node / interpolation guards and the `correct_style_detected` markers) and `StringLiterals#on_dstr` (the `ConsistentQuotesInMultiline` multi-line continued-string handling, whose autocorrect is a stock no-op). For each record it returns, in walk order, whether it offends, the caret range, the message selector, the detection marker to replay, and — for an `on_str` 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`), because the quote conversion and escape handling are Ruby string semantics; 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

MSG_SINGLE =
"Prefer single-quoted strings when you don't need string " \
"interpolation or special symbols."
MSG_DOUBLE =
"Prefer double-quoted strings unless you need single quotes to " \
"avoid extra backslashes for escaping."
MSG_INCONSISTENT =
"Inconsistent quote style."
MESSAGES =
[MSG_SINGLE, MSG_DOUBLE, MSG_INCONSISTENT].freeze
STYLES =
{
  "single_quotes" => 0,
  "double_quotes" => 1
}.freeze
FIX_SINGLE =

Fix kinds (mirror ‘string_literals.rs`).

0
FIX_DOUBLE =
1
FIX_NONE =
2

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



49
# File 'lib/shirobai/cop/style/string_literals.rb', line 49

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

.bundle_args(config) ⇒ Object

Packed config nums: ‘[style, consistent_multiline]`.



52
53
54
55
56
57
58
59
60
# File 'lib/shirobai/cop/style/string_literals.rb', line 52

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),
    cop_config["ConsistentQuotesInMultiline"] ? 1 : 0
  ]]
end

.cop_nameObject



48
# File 'lib/shirobai/cop/style/string_literals.rb', line 48

def self.cop_name = "Style/StringLiterals"

Instance Method Details

#alternative_styleObject



86
87
88
89
90
91
92
93
# File 'lib/shirobai/cop/style/string_literals.rb', line 86

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

#on_new_investigationObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/shirobai/cop/style/string_literals.rb', line 62

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, message_idx, 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: MESSAGES[message_idx]) do |corrector|
      apply_fix(corrector, range, fix, content)
      replay_detection(detect)
    end
  end
end