Sha256: 20afa2efd79c3cbc096daaac71fd3bbfba818dfb8c824d393bdb5bf50bd7b088

Contents?: true

Size: 1.08 KB

Versions: 4

Compression:

Stored size: 1.08 KB

Contents

module Steep
  module Subtyping
    class VariableOccurence
      attr_reader :params
      attr_reader :returns

      def initialize
        @params = Set.new
        @returns = Set.new
      end

      def add_method_type(method_type)
        method_type.params.each_type do |type|
          each_var(type) do |var|
            params << var
          end
        end
        each_var(method_type.return_type) do |var|
          returns << var
        end

        method_type.block&.yield_self do |block|
          block.params.each_type do |type|
            each_var(type) do |var|
              params << var
            end
          end
          each_var(block.return_type) do |var|
            returns << var
          end
        end
      end

      def each_var(type, &block)
        type.free_variables.each(&block)
      end

      def strictly_return?(var)
        !params.member?(var) && returns.member?(var)
      end

      def self.from_method_type(method_type)
        self.new.tap do |occurence|
          occurence.add_method_type(method_type)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
steep-0.4.0 lib/steep/subtyping/variable_occurrence.rb
steep-0.3.0 lib/steep/subtyping/variable_occurrence.rb
steep-0.2.0 lib/steep/subtyping/variable_occurrence.rb
steep-0.1.0 lib/steep/subtyping/variable_occurrence.rb