Class: Shirobai::Cop::RSpec::VariableDefinition

Inherits:
RuboCop::Cop::Base
  • Object
show all
Extended by:
RuboCop::Cop::AutoCorrector
Includes:
RuboCop::Cop::ConfigurableEnforcedStyle, BundleEligible
Defined in:
lib/shirobai/cop/rspec/variable_definition.rb

Overview

Drop-in Rust reimplementation of RSpec/VariableDefinition (rubocop-rspec 3.10.2).

Rust classifies once on the shared walk: the candidate matcher and the top-level-spec-group gate are exactly RSpec/VariableName's (a send-shaped let/subject with a literal first argument inside a top-level spec group). The EnforcedStyle decides the offenders: symbols (default) flags plain str names, strings flags sym AND dsym names; a dstr name is never flagged (probed). Rust already applies the style, so the wire carries only real offenders.

The wrapper reproduces stock's correct_variable: a sym becomes value.inspect (variable.value.to_s.inspect), a str becomes value.to_sym.inspect, and a dsym becomes its source minus the leading colon (variable.source[1..]).

Constant Summary collapse

MSG =
"Use %<style>s for variable names."

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



29
# File 'lib/shirobai/cop/rspec/variable_definition.rb', line 29

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

.bundle_args(config) ⇒ Object

Packed args for the bundled run: [style] (0 symbols, 1 strings).



32
33
34
# File 'lib/shirobai/cop/rspec/variable_definition.rb', line 32

def self.bundle_args(config)
  [config.for_badge(badge).fetch("EnforcedStyle", "symbols") == "strings" ? 1 : 0]
end

.cop_nameObject



28
# File 'lib/shirobai/cop/rspec/variable_definition.rb', line 28

def self.cop_name = "RSpec/VariableDefinition"

Instance Method Details

#on_new_investigationObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/shirobai/cop/rspec/variable_definition.rb', line 36

def on_new_investigation
  offenses = resolved_offenses
  return if offenses.empty?

  buffer = processed_source.buffer
  source = bundle_eligible? ? processed_source.raw_source : buffer.source
  off = SourceOffsets.for(source)
  offenses.each do |(start, fin, kind, value)|
    range = Parser::Source::Range.new(buffer, off[start], off[fin])
    add_offense(range, message: format(MSG, style: style)) do |corrector|
      corrector.replace(range, correct_variable(range, kind, value))
    end
  end
end