Class: Shirobai::Cop::Style::ArgumentsForwarding

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

Overview

Drop-in Rust reimplementation of Style/ArgumentsForwarding.

Rust does the whole on_def walk: for every method definition it snapshots each descendant call / super / yield, collects the local variable references in the body, and (when the def node closes) classifies each call against the def's rest / kwrest / block params. It returns, in stock's exact add_offense order, one record per offense: the highlight range, the message code, and the precise corrector op stream (add_parentheses reproduced byte for byte, including stock's double ) / (( when two different-range offenses each add parentheses to a paren-less def / call). RuboCop dedups offenses by range through a Set, so a repeated range's corrector is dropped here exactly like in stock.

The cop is TargetRubyVersion-gated (minimum_target_ruby_version 2.7); shirobai always parses with prism Latest, but the version only gates the cop's LOGIC, which is passed through bundle_args from config.target_ruby_version (pure config, parser-independent). The cross-cop Naming/BlockForwarding EnforcedStyle == 'explicit' read and the two boolean options plus the three redundant-name lists are packed the same way.

Constant Summary collapse

MESSAGES =
[
  "Use shorthand syntax `...` for arguments forwarding.",
  "Use anonymous positional arguments forwarding (`*`).",
  "Use anonymous keyword arguments forwarding (`**`).",
  "Use anonymous block arguments forwarding (`&`)."
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.autocorrect_incompatible_withObject



44
45
46
# File 'lib/shirobai/cop/style/arguments_forwarding.rb', line 44

def self.autocorrect_incompatible_with
  [RuboCop::Cop::Naming::BlockForwarding]
end

.badgeObject



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

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

.bundle_args(config) ⇒ Object

[[target_ruby, allow_only_rest, use_anon, explicit_block], [redundant_rest, redundant_kwrest, redundant_block]].



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

def self.bundle_args(config)
  cop_config = config.for_badge(badge)
  block_forwarding = config.for_enabled_cop("Naming/BlockForwarding")
  nums = [
    (config.target_ruby_version * 10).round,
    cop_config.fetch("AllowOnlyRestArgument", true) ? 1 : 0,
    cop_config.fetch("UseAnonymousForwarding", false) ? 1 : 0,
    block_forwarding["EnforcedStyle"] == "explicit" ? 1 : 0
  ]
  lists = [
    Array(cop_config.fetch("RedundantRestArgumentNames", [])),
    Array(cop_config.fetch("RedundantKeywordRestArgumentNames", [])),
    Array(cop_config.fetch("RedundantBlockArgumentNames", []))
  ]
  [nums, lists]
end

.cop_nameObject



41
# File 'lib/shirobai/cop/style/arguments_forwarding.rb', line 41

def self.cop_name = "Style/ArgumentsForwarding"

Instance Method Details

#on_new_investigationObject



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/shirobai/cop/style/arguments_forwarding.rb', line 67

def on_new_investigation
  buffer = processed_source.buffer
  off = SourceOffsets.for(bundle_eligible? ? processed_source.raw_source : buffer.source)

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