Class: Shirobai::Cop::Style::TrailingCommaInArguments

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

Overview

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

Rust walks every method call once, replicating stock’s ‘on_send` / `on_csend` and the shared `TrailingComma#check` mixin: for a parenthesized call (or an index `[]` call) with arguments it decides whether a trailing comma is present-but-unwanted (`avoid_comma`) or wanted-but-missing (`put_comma`), honouring `EnforcedStyleForMultiline` (`no_comma` / `comma` / `consistent_comma` / `diff_comma`). For each offense it returns the caret range, the message selector, and the single corrector op (remove the trailing comma, or insert one after a range —stock’s ‘PunctuationCorrector.swap_comma`).

The corrector op needs no Ruby string semantics, so it is fully computed in Rust; the wrapper only turns it into the ‘corrector` call and selects the message text. The `EnforcedStyleForMultiline` accessor is the genuine `ConfigurableEnforcedStyle#style`, so an unrecognized style raises exactly as stock does and `config_to_allow_offenses` is unaffected (this cop emits no detection markers).

Always bundle-eligible: the result is purely config-driven.

Constant Summary collapse

MSG =
"%<command>s comma after the last %<unit>s."
MSG_AVOID_NO_COMMA =

Message selectors (mirror ‘trailing_comma_in_arguments.rs`).

0
MSG_AVOID_COMMA =
1
MSG_AVOID_CONSISTENT_COMMA =
2
MSG_AVOID_DIFF_COMMA =
3
MSG_PUT =
4
MESSAGES =

Fully formatted messages, matching stock’s ‘avoid_comma` / `put_comma` (kind = ’parameter of %<article>s method call’, article ‘a’ for avoid and ‘a multiline’ for put, plus the style-specific ‘extra_avoid_comma_info` suffix).

[
  format(MSG, command: "Avoid", unit: "parameter of a method call"),
  format(MSG, command: "Avoid",
              unit: "parameter of a method call, unless each item is on its own line"),
  format(MSG, command: "Avoid",
              unit: "parameter of a method call, unless items are split onto multiple lines"),
  format(MSG, command: "Avoid",
              unit: "parameter of a method call, unless that item immediately precedes a newline"),
  format(MSG, command: "Put a", unit: "parameter of a multiline method call")
].freeze
STYLES =
{
  "no_comma" => 0,
  "comma" => 1,
  "consistent_comma" => 2,
  "diff_comma" => 3
}.freeze
FIX_AVOID =

Corrector op kinds (mirror ‘trailing_comma_in_arguments.rs`).

0
FIX_PUT =
1

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



66
# File 'lib/shirobai/cop/style/trailing_comma_in_arguments.rb', line 66

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

.bundle_args(config) ⇒ Object

Packed config nums: ‘[style]`.



69
70
71
72
73
74
# File 'lib/shirobai/cop/style/trailing_comma_in_arguments.rb', line 69

def self.bundle_args(config)
  cop_config = config.for_badge(badge)
  # An unrecognized style defaults to no_comma here; the genuine error is
  # raised by the `style` accessor in `on_new_investigation`.
  [[STYLES.fetch(cop_config["EnforcedStyleForMultiline"] || "no_comma", 0)]]
end

.cop_nameObject



65
# File 'lib/shirobai/cop/style/trailing_comma_in_arguments.rb', line 65

def self.cop_name = "Style/TrailingCommaInArguments"

Instance Method Details

#on_new_investigationObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/shirobai/cop/style/trailing_comma_in_arguments.rb', line 80

def on_new_investigation
  # Validate `EnforcedStyleForMultiline` through the genuine accessor:
  # stock raises for an unrecognized style, and this must fire before we
  # derive the bundle config (which would otherwise mask it).
  style

  buffer = processed_source.buffer
  off = SourceOffsets.for(processed_source.raw_source)

  resolved_result.each do |start, fin, message_idx, fix|
    range = Parser::Source::Range.new(buffer, off[start], off[fin])
    add_offense(range, message: MESSAGES[message_idx]) do |corrector|
      apply_fix(corrector, range, fix)
    end
  end
end

#style_parameter_nameObject



76
77
78
# File 'lib/shirobai/cop/style/trailing_comma_in_arguments.rb', line 76

def style_parameter_name
  "EnforcedStyleForMultiline"
end