Class: Shirobai::Cop::Metrics::MethodLength

Inherits:
RuboCop::Cop::Base
  • Object
show all
Extended by:
RuboCop::ExcludeLimit
Includes:
RuboCop::Cop::AllowedMethods, RuboCop::Cop::AllowedPattern
Defined in:
lib/shirobai/cop/metrics/method_length.rb

Overview

Drop-in Rust reimplementation of ‘Metrics/MethodLength`.

Rust parses the source, finds every method definition (‘def` / `def self.` / `define_method` blocks, incl. numbered/`it` blocks), measures the body length with the shared `CodeLength` calculator (comment and `CountAsOne` handling shared with `Metrics/BlockLength`) and returns those exceeding `Max`. `AllowedMethods` / `AllowedPatterns` filtering stays on the Ruby side, which has the exact symbol/regexp semantics; Rust marks each candidate `filterable` (false for a `define_method` whose name argument is not a basic literal, which stock never filters).

Constant Summary collapse

LABEL =
"Method"
MSG =
"%<label>s has too many lines. [%<length>d/%<max>d]"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



27
# File 'lib/shirobai/cop/metrics/method_length.rb', line 27

def self.badge = RuboCop::Cop::Badge.parse("Metrics/MethodLength")

.bundle_args(config) ⇒ Object

Packed args for the bundled run: ‘[max, count_comments, count_as_one]`. `Max` defaults to 10 (default.yml) so a config that does not mention this cop still packs cleanly; the computed slice is discarded in that case.



33
34
35
36
37
38
39
40
# File 'lib/shirobai/cop/metrics/method_length.rb', line 33

def self.bundle_args(config)
  cop_config = config.for_badge(badge)
  [
    cop_config["Max"] || 10,
    !!cop_config["CountComments"],
    Array(cop_config["CountAsOne"]).map(&:to_s)
  ]
end

.cop_nameObject



26
# File 'lib/shirobai/cop/metrics/method_length.rb', line 26

def self.cop_name = "Metrics/MethodLength"

Instance Method Details

#on_new_investigationObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/shirobai/cop/metrics/method_length.rb', line 42

def on_new_investigation
  candidates = Dispatch.offenses_for(processed_source, config, :method_length)
  off = SourceOffsets.for(processed_source.raw_source)
  candidates.each do |start, fin, head_end, length, name, filterable|
    next if filterable && (allowed_method?(name) || matches_allowed_pattern?(name))

    stop = RuboCop::LSP.enabled? ? head_end : fin
    range = Parser::Source::Range.new(processed_source.buffer, off[start], off[stop])
    add_offense(range, message: format(MSG, label: LABEL, length: length, max: max_length)) do
      self.max = length
    end
  end
end