Class: Shirobai::Cop::Naming::PredicatePrefix

Inherits:
RuboCop::Cop::Base
  • Object
show all
Includes:
RuboCop::Cop::AllowedMethods
Defined in:
lib/shirobai/cop/naming/predicate_prefix.rb

Overview

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

Rust walks the definition sites (‘def`/`defs` and the configured `MethodDefinitionMacros` calls with a symbol first argument), keeps the rare sites whose name literally starts with a configured `NamePrefix` entry (the same cheap gate the stock cop applies before its regex check) and reports them together with whether a `sig { returns(T::Boolean) }` block immediately precedes the definition. Ruby then replays the stock per-prefix filtering verbatim on those candidates — the `/^#prefix/` interpolation, `expected_name` equality, assignment names, `AllowedMethods` and `UseSorbetSigs` — so every config option keeps its stock semantics (including regex-interpolated prefixes). With every filter applied after the ext call, this cop is always bundle-eligible.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



24
# File 'lib/shirobai/cop/naming/predicate_prefix.rb', line 24

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

.bundle_args(config) ⇒ Object

Packed args for the bundled run: ‘[name_prefixes, macros]` (two of the bundle’s string lists). Both may be absent when the config does not mention this cop (the slice is then discarded); default to empty lists (no candidates).



30
31
32
33
34
35
36
# File 'lib/shirobai/cop/naming/predicate_prefix.rb', line 30

def self.bundle_args(config)
  cop_config = config.for_badge(badge)
  [
    Array(cop_config["NamePrefix"]).map(&:to_s),
    Array(cop_config["MethodDefinitionMacros"]).map(&:to_s)
  ]
end

.cop_nameObject



23
# File 'lib/shirobai/cop/naming/predicate_prefix.rb', line 23

def self.cop_name = "Naming/PredicatePrefix"

Instance Method Details

#on_new_investigationObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/shirobai/cop/naming/predicate_prefix.rb', line 38

def on_new_investigation
  candidates = Dispatch.offenses_for(processed_source, config, :predicate_prefix)

  off = SourceOffsets.for(processed_source.raw_source)
  candidates.each do |start, fin, method_name, is_def, sorbet_boolean_sig|
    predicate_prefixes.each do |prefix|
      next if allowed_method_name?(method_name, prefix)
      # The Sorbet exemption only applies to `def`/`defs` sites; the
      # stock `on_send` (macro) path never consults the sig.
      next if is_def && use_sorbet_sigs? && !sorbet_boolean_sig

      range = Parser::Source::Range.new(processed_source.buffer, off[start], off[fin])
      add_offense(range, message: message(method_name, expected_name(method_name, prefix)))
    end
  end
end

#validate_configObject



55
56
57
58
59
60
61
62
63
# File 'lib/shirobai/cop/naming/predicate_prefix.rb', line 55

def validate_config
  forbidden_prefixes.each do |forbidden_prefix|
    next if predicate_prefixes.include?(forbidden_prefix)

    raise RuboCop::ValidationError, <<~MSG.chomp
      The `Naming/PredicatePrefix` cop is misconfigured. Prefix #{forbidden_prefix} must be included in NamePrefix because it is included in ForbiddenPrefixes.
    MSG
  end
end