Module: Moult::SymbolScanner

Defined in:
lib/moult/symbol_scanner.rb

Overview

Collects method names that are referenced as symbols passed to Rails-style DSL methods — before_action :authenticate, validate :check, helper_method :current_user, delegate :name, to: :user. rubydex's reference index only sees real call sites, so these symbol arguments look like nothing references the method and it would be a false-positive dead-code candidate. This scanner harvests them so RailsConventions can treat the named methods as live.

It is intentionally name-based and lexically scoped to the enclosing class/module: it returns the set of bare method names referenced by DSL symbols in a file, qualified by the surrounding namespace where known. Over- collecting is safe — a spurious "reference" only lowers a finding's confidence, it can never invent a finding.

Defined Under Namespace

Classes: Visitor

Constant Summary collapse

CALLBACK_DSL =

DSL methods whose Symbol arguments name a method of the surrounding class.

%w[
  before_action after_action around_action
  append_before_action prepend_before_action
  skip_before_action skip_after_action skip_around_action
  before_filter after_filter around_filter
  before_save after_save before_create after_create
  before_update after_update before_destroy after_destroy
  before_validation after_validation
  after_commit after_rollback after_initialize after_find
  before_action_callback
  validate validates_each
  helper_method
  scope delegate
].freeze

Class Method Summary collapse

Class Method Details

.scan_file(path) ⇒ Array<String>

Parameters:

  • path (String)

Returns:

  • (Array<String>)


50
51
52
# File 'lib/moult/symbol_scanner.rb', line 50

def scan_file(path)
  scan_source(File.read(path))
end

.scan_source(source) ⇒ Array<String>

Returns referenced names: bare ("authenticate") and, where a lexical namespace is known, qualified ("Foo::Bar#authenticate").

Parameters:

  • source (String)

    Ruby source

Returns:

  • (Array<String>)

    referenced names: bare ("authenticate") and, where a lexical namespace is known, qualified ("Foo::Bar#authenticate").



41
42
43
44
45
46
# File 'lib/moult/symbol_scanner.rb', line 41

def scan_source(source)
  result = Prism.parse(source)
  visitor = Visitor.new
  result.value.accept(visitor)
  visitor.referenced_names.to_a
end