Class: Shirobai::Cop::Rails::UnknownEnv

Inherits:
RuboCop::Cop::Base
  • Object
show all
Includes:
BundleEligible
Defined in:
lib/shirobai/cop/rails/unknown_env.rb

Overview

Drop-in Rust reimplementation of Rails/UnknownEnv (rubocop-rails 2.35.5).

Rust detects the three Rails.env shapes (predicate / comparison / case) and emits [start, end, name] per unknown environment. The message — including the DidYouMean spell suggestion, which must stay Ruby-side — is built here from this cop's own Environments config, so the suggestion text is byte-for-byte stock. No autocorrect (stock has none).

The supports_local view (local is a known environment for the predicate form only, and only on Rails >= 7.1) is packed into the segment so the Rust filter matches; the comparison and case forms never allow local, mirroring stock.

Constant Summary collapse

MSG =
"Unknown environment `%<name>s`."
MSG_SIMILAR =
"Unknown environment `%<name>s`. Did you mean `%<similar>s`?"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



27
# File 'lib/shirobai/cop/rails/unknown_env.rb', line 27

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

.bundle_args(config) ⇒ Object

Contributes [nums, lists] pieces to the rails segment: [[supports_local], [environments]].



31
32
33
34
35
36
# File 'lib/shirobai/cop/rails/unknown_env.rb', line 31

def self.bundle_args(config)
  cop = config.for_badge(badge)
  environments = cop["Environments"] || []
  supports_local = config.target_rails_version >= 7.1
  [[supports_local ? 1 : 0], [environments]]
end

.cop_nameObject



26
# File 'lib/shirobai/cop/rails/unknown_env.rb', line 26

def self.cop_name = "Rails/UnknownEnv"

Instance Method Details

#on_new_investigationObject



38
39
40
41
42
43
44
45
46
# File 'lib/shirobai/cop/rails/unknown_env.rb', line 38

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_offset, end_offset, name|
    range = Parser::Source::Range.new(buffer, off[start_offset], off[end_offset])
    add_offense(range, message: message(name))
  end
end