Class: Shirobai::Cop::Style::HashTransformKeys

Inherits:
RuboCop::Cop::Base
  • Object
show all
Extended by:
RuboCop::Cop::AutoCorrector
Defined in:
lib/shirobai/cop/style/hash_transform_keys.rb

Overview

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

Stock matches four shapes via the ‘HashTransformMethod` mixin (`each_with_object({})` / `Hash[_.map …]` / `_.map….to_h` / `_.to_h…`) and emits a sequence of corrector ops that strips `Hash[` / `]` (or the trailing `.to_h`), swaps the selector to `transform_keys`, rewrites the block arguments and replaces the body with just the key transformation. Rust replays the same gating (`hash_receiver?`, `noop_transformation?`, `transformation_uses_both_args?`, `use_transformed_argname?`) on the prism tree and returns the offense span plus an ordered list of `(start, end, replacement)` edits the wrapper plays back verbatim via `corrector.replace`.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



22
# File 'lib/shirobai/cop/style/hash_transform_keys.rb', line 22

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

.bundle_args(_config) ⇒ Object

Config-less from the bundle’s point of view: no per-cop config influences detection or autocorrect. (The cop’s ‘Enabled` / `Exclude` / etc. are honoured by the Team, not by us.)



27
# File 'lib/shirobai/cop/style/hash_transform_keys.rb', line 27

def self.bundle_args(_config) = []

.cop_nameObject



21
# File 'lib/shirobai/cop/style/hash_transform_keys.rb', line 21

def self.cop_name = "Style/HashTransformKeys"

Instance Method Details

#on_new_investigationObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/shirobai/cop/style/hash_transform_keys.rb', line 29

def on_new_investigation
  buffer = processed_source.buffer
  offenses = Dispatch.offenses_for(processed_source, config, :hash_transform_keys)
  off = SourceOffsets.for(processed_source.raw_source)
  offenses.each do |start, fin, message, edits|
    range = Parser::Source::Range.new(buffer, off[start], off[fin])
    add_offense(range, message: message) do |corrector|
      edits.each do |edit_start, edit_end, replacement|
        edit_range = Parser::Source::Range.new(buffer, off[edit_start], off[edit_end])
        corrector.replace(edit_range, replacement)
      end
    end
  end
end