Sha256: 2ca8812f4a44f86b274dada0e94d0ac57a400a03ae02e3db58ade335ae91d77c

Contents?: true

Size: 1.84 KB

Versions: 2

Compression:

Stored size: 1.84 KB

Contents

module Steep
  module AST
    module Node
      class TypeAssertion
        attr_reader :location

        def initialize(location)
          @location = location
        end

        def source
          location.source
        end

        def line
          location.start_line
        end

        def type(context, subtyping, type_vars)
          if ty = RBS::Parser.parse_type(type_location.buffer, range: type_location.range, variables: type_vars, require_eof: true)
            validator = Signature::Validator.new(checker: subtyping)
            validator.validate_type(ty)

            unless validator.has_error?
              ty = subtyping.factory.type(ty)
              subtyping.factory.absolute_type(ty, context: context)
            end
          else
            nil
          end
        rescue ::RBS::ParsingError => exn
          exn
        end

        def type_syntax?
          RBS::Parser.parse_type(type_location.buffer, range: type_location.range, variables: [], require_eof: true)
          true
        rescue::RBS::ParsingError
          false
        end

        def type?(context, subtyping, type_vars)
          type = type(context, subtyping, type_vars)

          case type
          when RBS::ParsingError, nil
            nil
          else
            type
          end
        end

        def type_str
          @type_str ||= source.delete_prefix(":").lstrip
        end

        def type_location
          offset = source.size - type_str.size
          RBS::Location.new(location.buffer, location.start_pos + offset, location.end_pos)
        end

        def self.parse(location)
          source = location.source.strip

          if source =~/\A:\s*(.+)/
            assertion = TypeAssertion.new(location)
            if assertion.type_syntax?
              assertion
            end
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
steep-1.5.0.pre.2 lib/steep/ast/node/type_assertion.rb
steep-1.5.0.pre.1 lib/steep/ast/node/type_assertion.rb