class Typerb::VariableName

Attributes

file[R]
line[R]

Public Class Methods

new(caller_loc) click to toggle source
# File lib/typerb/variable_name.rb, line 7
def initialize(caller_loc)
  @file = caller_loc[0].path
  @line = caller_loc[0].lineno
end

Public Instance Methods

get() click to toggle source
# File lib/typerb/variable_name.rb, line 12
def get
  return unless defined?(RubyVM::AbstractSyntaxTree)
  return unless File.exist?(file)

  caller_method = caller_locations(1, 1)[0].label.to_sym
  from_ast(caller_method)
end

Private Instance Methods

from_ast(caller_method) click to toggle source
# File lib/typerb/variable_name.rb, line 22
def from_ast(caller_method) # rubocop: disable Metrics/AbcSize not worth fixing
  code = File.read(file).lines[line - 1].strip
  node = RubyVM::AbstractSyntaxTree.parse(code)
  if node.children.last.children.size == 3 && node.children.last.children[1] == caller_method # rubocop: disable Style/IfUnlessModifier, Style/GuardClause
    node.children.last.children.first.children.first
  end
end