Skip to content

Instantly share code, notes, and snippets.

@jhawthorn
Created November 13, 2020 19:56
Show Gist options
  • Save jhawthorn/147bd0e0ffc3978f9473a09c2081c507 to your computer and use it in GitHub Desktop.
Save jhawthorn/147bd0e0ffc3978f9473a09c2081c507 to your computer and use it in GitHub Desktop.
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