Class: Tapioca::Static::SymbolTableParser

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/tapioca/static/symbol_table_parser.rb

Constant Summary collapse

SKIP_PARSE_KINDS =
T.let(["CLASS_OR_MODULE", "STATIC_FIELD"].freeze, T::Array[String])

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSymbolTableParser

Returns a new instance of SymbolTableParser.



30
31
32
33
# File 'lib/tapioca/static/symbol_table_parser.rb', line 30

def initialize
  @symbols = T.let(Set.new, T::Set[String])
  @parents = T.let([], T::Array[String])
end

Instance Attribute Details

#symbolsObject (readonly)

Returns the value of attribute symbols.



27
28
29
# File 'lib/tapioca/static/symbol_table_parser.rb', line 27

def symbols
  @symbols
end

Class Method Details

.parse_json(json_string) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/tapioca/static/symbol_table_parser.rb', line 15

def parse_json(json_string)
  obj = JSON.parse(json_string)

  parser = SymbolTableParser.new
  parser.parse_object(obj)
  parser.symbols
rescue JSON::ParserError
  Set.new
end

Instance Method Details

#fully_qualified_name(name) ⇒ Object



65
66
67
# File 'lib/tapioca/static/symbol_table_parser.rb', line 65

def fully_qualified_name(name)
  [*@parents, name].join("::")
end

#parse_object(object) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/tapioca/static/symbol_table_parser.rb', line 36

def parse_object(object)
  children = object.fetch("children", [])

  children.each do |child|
    kind = child.fetch("kind")
    name = child.fetch("name")
    name = name.fetch("name") if name.is_a?(Hash)

    next if name.nil?
    next unless SKIP_PARSE_KINDS.include?(kind)

    # Turn singleton class names to attached class names
    if (match_data = name.match(/<Class:(.*)>/))
      name = match_data[1]
    end

    next if name.match?(/[<>()$]/)
    next if name.match?(/^[0-9]+$/)
    next if name == "T::Helpers"

    @symbols.add(fully_qualified_name(name))

    @parents << name
    parse_object(child)
    @parents.pop
  end
end