Created
November 13, 2020 19:56
-
-
Save jhawthorn/147bd0e0ffc3978f9473a09c2081c507 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "ripper" | |
class LocalDetector < ::Ripper | |
Ident = Struct.new(:value) | |
attr_reader :vcalls, :var_assigns, :var_refs | |
def initialize(code) | |
@vcalls = Set.new | |
@var_assigns = Set.new | |
@var_refs = Set.new | |
super(code) | |
end | |
def possible_locals | |
@vcalls | @var_assigns | @var_refs | |
end | |
def on_var_field(name, *args) | |
@var_assigns << name.value if Ident === name | |
super | |
end | |
def on_var_ref(name, *args) | |
@var_refs << name.value if Ident === name | |
super | |
end | |
def on_vcall(name, *args) | |
@vcalls << name.value if Ident === name | |
super | |
end | |
def on_ident(value) | |
Ident.new(value) | |
end | |
def self.call(src) | |
parser = new(src) | |
parser.parse | |
parser | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment