Class: Shirobai::Cop::Layout::FirstHashElementIndentation

Inherits:
RuboCop::Cop::Base
  • Object
show all
Extended by:
RuboCop::Cop::AutoCorrector
Includes:
RuboCop::Cop::Alignment
Defined in:
lib/shirobai/cop/layout/first_hash_element_indentation.rb

Overview

Drop-in Rust reimplementation of Layout/FirstHashElementIndentation.

Rust parses the source, walks every braced hash literal (replicating the each_argument_node / ignore_node claiming of hashes by a method call's left parenthesis), decides the alignment base for the configured EnforcedStyle (brace / after-paren / parent hash key / start of line), applies the Layout/HashAlignment separator longest-key offset when configured, and returns each misindented first pair and hanging right brace as an offense range plus its column_delta, message and an autocorrect-target marker. Ruby supplies the flattened config and applies the realignment via AlignmentCorrector (the same division of labour as FirstArrayElementIndentation). Offenses come from the per-file bundled run (Shirobai::Dispatch); the config derivation is purely config-driven, so this cop is always bundle-eligible.

Constant Summary collapse

STYLES =
{
  "special_inside_parentheses" => 0,
  "consistent" => 1,
  "align_braces" => 2
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



31
# File 'lib/shirobai/cop/layout/first_hash_element_indentation.rb', line 31

def self.badge = RuboCop::Cop::Badge.parse("Layout/FirstHashElementIndentation")

.bundle_args(config) ⇒ Object

Packed args for the bundled run: [style, indentation_width, enforce_fixed, colon_separator, rocket_separator]. The enforce flag replicates enforce_first_argument_with_fixed_indentation?: when Layout/ArgumentAlignment enforces with_fixed_indentation, the cop stops letting a ( claim a hash (it does NOT stand the cop down, and there is no style exemption — unlike the array cop). The separator flags replicate separator_style?, which reads Layout/HashAlignment's Enforced{Colon,HashRocket}Style.



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/shirobai/cop/layout/first_hash_element_indentation.rb', line 41

def self.bundle_args(config)
  cop_config = config.for_badge(badge)
  arg_alignment_config = config.for_enabled_cop("Layout/ArgumentAlignment")
  hash_alignment_config = config.for_cop("Layout/HashAlignment")
  [
    STYLES.fetch(cop_config["EnforcedStyle"], 0),
    cop_config["IndentationWidth"] || config.for_cop("Layout/IndentationWidth")["Width"] || 2,
    arg_alignment_config["EnforcedStyle"] == "with_fixed_indentation",
    hash_alignment_config["EnforcedColonStyle"] == "separator",
    hash_alignment_config["EnforcedHashRocketStyle"] == "separator"
  ]
end

.cop_nameObject



30
# File 'lib/shirobai/cop/layout/first_hash_element_indentation.rb', line 30

def self.cop_name = "Layout/FirstHashElementIndentation"

Instance Method Details

#on_new_investigationObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/shirobai/cop/layout/first_hash_element_indentation.rb', line 54

def on_new_investigation
  buffer = processed_source.buffer

  offenses = Dispatch.offenses_for(processed_source, config, :first_hash_element_indentation)
  off = SourceOffsets.for(processed_source.raw_source)
  offenses.each do |start, fin, column_delta, message, correct_target|
    range = Parser::Source::Range.new(buffer, off[start], off[fin])
    add_offense(range, message: message) do |corrector|
      target = correction_target(range, correct_target, buffer)
      RuboCop::Cop::AlignmentCorrector.correct(
        corrector, processed_source, target, column_delta
      )
    end
  end
end