Sha256: c85b1e412ebd70e6c09d59f502d191899c0d7543aad31fc961d4cc13a8e9cb22

Contents?: true

Size: 1.63 KB

Versions: 5

Compression:

Stored size: 1.63 KB

Contents

# typed: strict
# frozen_string_literal: true

module Tapioca
  module Static
    class SymbolTableParser
      extend T::Sig

      sig { params(json_string: String).returns(T::Set[String]) }
      def self.parse_json(json_string)
        obj = JSON.parse(json_string)

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

      sig { returns(T::Set[String]) }
      attr_reader :symbols

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

      sig { params(object: T::Hash[String, T.untyped]).void }
      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 kind.nil? || name.nil?

          # TODO: CLASS is removed since v0.4.4730 of Sorbet
          # but keeping here for backward compatibility. Remove
          # once the minimum version is moved past that.
          next unless ["CLASS", "CLASS_OR_MODULE", "STATIC_FIELD"].include?(kind)
          next if name =~ /[<>()$]/
          next if name =~ /^[0-9]+$/
          next if name == "T::Helpers"

          @symbols.add(fully_qualified_name(name))

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

      sig { params(name: String).returns(String) }
      def fully_qualified_name(name)
        [*@parents, name].join("::")
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
tapioca-0.9.4 lib/tapioca/static/symbol_table_parser.rb
tapioca-0.9.3 lib/tapioca/static/symbol_table_parser.rb
tapioca-0.9.2 lib/tapioca/static/symbol_table_parser.rb
tapioca-0.9.1 lib/tapioca/static/symbol_table_parser.rb
tapioca-0.9.0 lib/tapioca/static/symbol_table_parser.rb