Module: Scryer::MethodExtractor

Defined in:
lib/scryer/method_extractor.rb

Constant Summary collapse

MIN_TOKENS =

skip trivial one-liners — not worth flagging as "duplicated"

12

Class Method Summary collapse

Class Method Details

.extract(file:, source:, sexp:) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/scryer/method_extractor.rb', line 21

def extract(file:, source:, sexp:)
  methods = []

  Ast.each_node(sexp) do |node|
    next unless Ast.tagged?(node, :def, :defs)

    name_node = node[0] == :defs ? node[3] : node[1]
    name = Ast.ident_text(name_node) || "?"
    body_node = node.last

    positions = Ast.each_node(body_node).filter_map { |n| Ast.position_of(n) }
    next if positions.empty? # empty method body (e.g. `def foo; end`) — nothing to compare

    start_line = Ast.line_of(name_node) || positions.map(&:first).min
    end_line = positions.map(&:first).max

    tokens = Ast.normalized_tokens(body_node)
    next if tokens.size < MIN_TOKENS

    methods << MethodInfo.new(
      name: name,
      file: file,
      start_line: start_line,
      end_line: end_line,
      token_stream: tokens,
      source_snippet: source.lines[(start_line - 1)...[end_line, source.lines.size].min]&.join
    )
  end

  methods
end