Class: Shirobai::Cop::Naming::MethodName

Inherits:
RuboCop::Cop::Base
  • Object
show all
Includes:
RuboCop::Cop::AllowedPattern, RuboCop::Cop::ConfigurableNaming, RuboCop::Cop::ForbiddenIdentifiers, RuboCop::Cop::ForbiddenPattern
Defined in:
lib/shirobai/cop/naming/method_name.rb

Overview

Drop-in Rust reimplementation of ‘Naming/MethodName`.

Rust walks the method-name sites (‘def`/`defs`, `define_method`, `Struct.new`/`Data.define` members, `alias`/`alias_method` arguments and `attr_*` accessors), filters out operator methods, computes whether each name matches the configured `EnforcedStyle` (and which alternative style it matches otherwise), including the class-emitter-method exception, and returns the offense candidates. Ruby keeps the `AllowedPatterns`, `ForbiddenIdentifiers` and `ForbiddenPatterns` filters and the `ConfigurableEnforcedStyle` bookkeeping (`config_to_allow_offenses`). When none of those filters is configured, Rust only returns the invalid sites (see `on_new_investigation`).

Constant Summary collapse

MSG =
"Use %<style>s for method names."
MSG_FORBIDDEN =
"`%<identifier>s` is forbidden, use another method name instead."
STYLE_INDEX =
{ "snake_case" => 0, "camelCase" => 1 }.freeze
INDEX_STYLE =
%i[snake_case camelCase].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



31
# File 'lib/shirobai/cop/naming/method_name.rb', line 31

def self.badge = RuboCop::Cop::Badge.parse("Naming/MethodName")

.bundle_args(config) ⇒ Object

Packed args for the bundled run: ‘[style]`. The bundle always computes the filtered flavor (see `bundle_eligible?`). `EnforcedStyle` may be absent when the config does not mention this cop (the slice is then discarded); default to style 0 in that case.



37
38
39
# File 'lib/shirobai/cop/naming/method_name.rb', line 37

def self.bundle_args(config)
  [STYLE_INDEX[config.for_badge(badge)["EnforcedStyle"]] || 0]
end

.cop_nameObject



30
# File 'lib/shirobai/cop/naming/method_name.rb', line 30

def self.cop_name = "Naming/MethodName"

Instance Method Details

#on_new_investigationObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/shirobai/cop/naming/method_name.rb', line 41

def on_new_investigation
  # Fast path: with no AllowedPatterns / ForbiddenIdentifiers /
  # ForbiddenPatterns configured (the default), the style-compliant
  # sites can only ever feed `correct_style_detected`, so Rust drops
  # them and reports their existence as a flag instead. `style_detected`
  # is idempotent set-intersection bookkeeping (sticky
  # `no_acceptable_style!`), so one call is observably identical to one
  # call per compliant site, in any order relative to
  # `unexpected_style_detected`.
  fast = bundle_eligible?

  candidates, had_valid =
    if fast
      Dispatch.offenses_for(processed_source, config, :method_name)
    else
      Shirobai.check_method_name(processed_source.raw_source, style_u8, false)
    end
  correct_style_detected if fast && had_valid

  off = SourceOffsets.for(processed_source.raw_source)
  candidates.each do |start, fin, name, valid, alt, fb_start, fb_end, fb_name|
    next if matches_allowed_pattern?(name)

    if forbidden_name?(name)
      fb_range = Parser::Source::Range.new(processed_source.buffer, off[fb_start], off[fb_end])
      add_offense(fb_range, message: format(MSG_FORBIDDEN, identifier: fb_name))
    elsif valid
      correct_style_detected
    else
      range = Parser::Source::Range.new(processed_source.buffer, off[start], off[fin])
      add_offense(range, message: format(MSG, style: style)) do
        if alt == 255
          unrecognized_style_detected
        else
          unexpected_style_detected(INDEX_STYLE[alt])
        end
      end
    end
  end
end