Class: Shirobai::Cop::Style::IfUnlessModifier

Inherits:
RuboCop::Cop::Base
  • Object
show all
Extended by:
RuboCop::Cop::AutoCorrector
Includes:
RuboCop::Cop::AllowedPattern, RuboCop::Cop::LineLengthHelp, RuboCop::Cop::RangeHelp, BundleEligible
Defined in:
lib/shirobai/cop/style/if_unless_modifier.rb

Overview

Drop-in Rust reimplementation of Style/IfUnlessModifier.

Rust walks every if/unless once and returns, in walk order, the candidates for both directions with everything byte/AST-shaped already decided: shape eligibility, the reconstructed modifier form and both of its lengths (with/without the first-line comment), and the exact corrector ops for the block-form rewrite. Ruby finishes the two regex-dependent decisions (the magnus rule keeps regexes here):

  • comment_disables_cop? on the first-line comment picks the with/without-comment variant (it changes the modifier-form length and the replacement text), and
  • the Layout/LineLength exemptions prune the "too long" direction (AllowedPatterns, cop directives, AllowURI, per-line disables via comment_config), reusing the stock mixins.

The wrapper also replays stock's ignore_node bookkeeping with plain char ranges: the store persists across autocorrect passes (stock's @ignored_nodes is never reset), and containment is the same numeric begin/end comparison stock does against stale ranges.

Bundle-eligible whenever the parser buffer equals the raw source (CRLF/BOM sources fall back to the standalone entry point over buffer.source so every offset lines up with parser positions).

Constant Summary collapse

MSG_USE_MODIFIER =
"Favor modifier `%<keyword>s` usage when having a " \
"single-line body. Another good alternative is " \
"the usage of control flow `&&`/`||`."
MSG_USE_NORMAL =
"Modifier form of `%<keyword>s` makes the line too long."

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.autocorrect_incompatible_withObject



45
46
47
# File 'lib/shirobai/cop/style/if_unless_modifier.rb', line 45

def self.autocorrect_incompatible_with
  [RuboCop::Cop::Style::Next, RuboCop::Cop::Style::SoleNestedConditional]
end

.badgeObject



43
# File 'lib/shirobai/cop/style/if_unless_modifier.rb', line 43

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

.bundle_args(config) ⇒ Object

Packed args for the bundled run: [max_line_length, tab_width]. max_line_length mirrors AutocorrectLogic#max_line_length (-1 when Layout/LineLength is disabled); tab_width mirrors LineLengthHelp#tab_indentation_width resolved with THIS cop's cop_config (the configured_indentation_width fallback).



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/shirobai/cop/style/if_unless_modifier.rb', line 54

def self.bundle_args(config)
  cop_config = config.for_badge(badge)
  max = if config.cop_enabled?("Layout/LineLength")
          config.for_cop("Layout/LineLength")["Max"] || 120
        else
          -1
        end
  tab = config.for_cop("Layout/IndentationStyle")["IndentationWidth"] ||
        cop_config["IndentationWidth"] ||
        config.for_cop("Layout/IndentationWidth")["Width"] || 2
  [max, tab]
end

.cop_nameObject



42
# File 'lib/shirobai/cop/style/if_unless_modifier.rb', line 42

def self.cop_name = "Style/IfUnlessModifier"

Instance Method Details

#on_new_investigationObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/shirobai/cop/style/if_unless_modifier.rb', line 67

def on_new_investigation
  # Mirrors stock's `@ignored_nodes`: never reset between passes.
  @ignored_ranges ||= []
  candidates = resolved_result
  return if candidates.empty?

  candidates.each do |candidate|
    kind = candidate[0]
    if kind.zero?
      check_use_modifier(candidate)
    else
      check_too_long(candidate)
    end
  end
end