Class: Shirobai::Cop::Rails::Pluck

Inherits:
RuboCop::Cop::Base
  • Object
show all
Extended by:
RuboCop::Cop::AutoCorrector, RuboCop::Cop::TargetRailsVersion
Includes:
BundleEligible
Defined in:
lib/shirobai/cop/rails/pluck.rb

Overview

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

Rust detects map { |x| x[:key] } / collect { |x| x[:key] } patterns replaceable with pluck(:key), including numblock (_1) and itblock (it) forms. The ancestor block-with-receiver guard (N+1 query risk) and the regexp key / block-arg-in-key exclusions are all handled Rust-side.

Autocorrect: replace from the selector (map/collect) through the block end with pluck(<key source>).

Constant Summary collapse

MSG =
"Prefer `%<replacement>s` over `%<current>s`."

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



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

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

.bundle_args(_config) ⇒ Object

No behavioral config to pack into the segment.



30
31
32
# File 'lib/shirobai/cop/rails/pluck.rb', line 30

def self.bundle_args(_config)
  [[], []]
end

.cop_nameObject



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

def self.cop_name = "Rails/Pluck"

Instance Method Details

#on_new_investigationObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/shirobai/cop/rails/pluck.rb', line 34

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 |sel_start, block_end, key_src_start, key_src_end|
    offense_range = Parser::Source::Range.new(buffer, off[sel_start], off[block_end])
    key_source = source.byteslice(key_src_start, key_src_end - key_src_start)
    replacement = "pluck(#{key_source})"
    message = format(MSG, replacement: replacement, current: offense_range.source)
    add_offense(offense_range, message: message) do |corrector|
      corrector.replace(offense_range, replacement)
    end
  end
end