Class: Shirobai::Cop::Layout::RescueEnsureAlignment

Inherits:
RuboCop::Cop::Base
  • Object
show all
Extended by:
RuboCop::Cop::AutoCorrector
Includes:
RuboCop::Cop::EndKeywordAlignment, RuboCop::Cop::RangeHelp, BundleEligible
Defined in:
lib/shirobai/cop/layout/rescue_ensure_alignment.rb

Overview

Drop-in Rust-accelerated reimplementation of Layout/RescueEnsureAlignment.

Stock's on_new_investigation materializes the parser-gem token stream on EVERY file (the "toucher" cost) just to collect the keyword position of each modifier rescue (x rescue y), which the cop uses to SKIP those resbody nodes (a modifier rescue is not an alignment target). Everything else — on_resbody / on_ensure, the alignment_node resolution, the offense, and the autocorrect — is cheap AST work.

This wrapper copies stock's body verbatim from vendor/rubocop/lib/rubocop/cop/layout/rescue_ensure_alignment.rb and replaces ONLY on_new_investigation (and its consumer modifier?): prism separates a modifier rescue into its own node type (RescueModifierNode), so the shared walk collects each one's keyword byte range with no token stream. The wrapper turns those into the modifier-position set and runs stock's detection / autocorrect unchanged, so offenses, messages, and corrected bytes match stock by construction. The offense ranges come from node.loc.keyword on the parser AST (never through Rust), so no SourceOffsets conversion is needed for them; only the modifier keyword positions (used purely for set membership) are byte-to-char converted.

Constant Summary collapse

MSG =
'`%<kw_loc>s` at %<kw_loc_line>d, %<kw_loc_column>d is not ' \
'aligned with `%<beginning>s` at ' \
'%<begin_loc_line>d, %<begin_loc_column>d.'
ANCESTOR_TYPES =
%i[kwbegin any_def class module sclass any_block].freeze
ALTERNATIVE_ACCESS_MODIFIERS =
%i[public_class_method private_class_method].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



39
# File 'lib/shirobai/cop/layout/rescue_ensure_alignment.rb', line 39

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

.bundle_args(config) ⇒ Object

Packed as a single enabled gate: 0 disabled (the shared walk skips the modifier-rescue collection), 1 enabled.



43
44
45
# File 'lib/shirobai/cop/layout/rescue_ensure_alignment.rb', line 43

def self.bundle_args(config)
  [[config.for_badge(badge)["Enabled"] == false ? 0 : 1]]
end

.cop_nameObject



38
# File 'lib/shirobai/cop/layout/rescue_ensure_alignment.rb', line 38

def self.cop_name = "Layout/RescueEnsureAlignment"

Instance Method Details

#on_ensure(node) ⇒ Object



51
52
53
# File 'lib/shirobai/cop/layout/rescue_ensure_alignment.rb', line 51

def on_ensure(node)
  check(node)
end

#on_new_investigationObject

Replaces stock's token-scan on_new_investigation: the modifier-rescue keyword positions come from prism's RescueModifierNodes via Rust, not processed_source.tokens.



58
59
60
# File 'lib/shirobai/cop/layout/rescue_ensure_alignment.rb', line 58

def on_new_investigation
  @modifier_positions = resolved_modifier_positions
end

#on_resbody(node) ⇒ Object



47
48
49
# File 'lib/shirobai/cop/layout/rescue_ensure_alignment.rb', line 47

def on_resbody(node)
  check(node) unless modifier?(node)
end