Class: Tapioca::Static::SymbolTableParser

Inherits:
Object
  • Object
show all
Defined in:
lib/tapioca/static/symbol_table_parser.rb

Constant Summary collapse

SKIP_PARSE_KINDS =

: Array

["CLASS_OR_MODULE", "STATIC_FIELD"].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSymbolTableParser

: -> void



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

def initialize
  @symbols = Set.new #: Set[String]
  @parents = [] #: Array[String]
end

Instance Attribute Details

#symbolsObject (readonly)

: Set



23
24
25
# File 'lib/tapioca/static/symbol_table_parser.rb', line 23

def symbols
  @symbols
end

Class Method Details

.parse_json(json_string) ⇒ Object

: (String json_string) -> Set



11
12
13
14
15
16
17
18
19
# File 'lib/tapioca/static/symbol_table_parser.rb', line 11

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

: (String name) -> String



61
62
63
# File 'lib/tapioca/static/symbol_table_parser.rb', line 61

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

#parse_object(object) ⇒ Object

: (Hash[String, untyped] object) -> void



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

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