Sha256: 64b70b52352341e85638c6cc319d06c176d576382903590e923b49c05f7ba816

Contents?: true

Size: 1.54 KB

Versions: 4

Compression:

Stored size: 1.54 KB

Contents

require 'set'

module Yoda
  module Model
    module TypeExpressions
      class UnionType < Base
        attr_reader :types

        # @param types [Array<Base>]
        # @return [Base]
        def self.new(types)
          reduced_types = types.reject { |type| type.is_a?(AnyType) || type.is_a?(UnknownType) }.uniq
          return (types.first || AnyType.new) if reduced_types.length == 0
          return reduced_types.first if reduced_types.length == 1
          super(reduced_types)
        end

        # @param types [Array<Base>]
        def initialize(types)
          @types = types
        end

        def eql?(another)
          another.is_a?(UnionType) && Set.new(types) == Set.new(another.types)
        end

        def hash
          [self.class.name, Set.new(types)].hash
        end

        # @param paths [Array<Path>]
        # @return [self]
        def change_root(paths)
          self.class.new(types.map { |type| type.change_root(paths) })
        end

        # @param registry [Registry]
        # @return [Array<Store::Objects::Base>]
        def resolve(registry)
          types.map { |type| type.resolve(registry) }.flatten.compact
        end

        # @return [String]
        def to_s
          types.map(&:to_s).join(' | ')
        end

        # @param env [Environment]
        def to_rbs_type(env)
          RBS::Types::Union.new(types: types.map { |t| t.to_rbs_type(env) }, location: nil)
        end

        # @return [self]
        def map(&block)
          self.class.new(types.map(&block))
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
yoda-language-server-0.10.1 lib/yoda/model/type_expressions/union_type.rb
yoda-language-server-0.10.0 lib/yoda/model/type_expressions/union_type.rb
yoda-language-server-0.9.0 lib/yoda/model/type_expressions/union_type.rb
yoda-language-server-0.8.0 lib/yoda/model/type_expressions/union_type.rb