Class: Steep::AST::Node::TypeAssertion

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/ast/node/type_assertion.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location) ⇒ TypeAssertion

Returns a new instance of TypeAssertion.



7
8
9
# File 'lib/steep/ast/node/type_assertion.rb', line 7

def initialize(location)
  @location = location
end

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



5
6
7
# File 'lib/steep/ast/node/type_assertion.rb', line 5

def location
  @location
end

Class Method Details

.parse(location) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/steep/ast/node/type_assertion.rb', line 68

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

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

Instance Method Details

#lineObject



15
16
17
# File 'lib/steep/ast/node/type_assertion.rb', line 15

def line
  location.start_line
end

#sourceObject



11
12
13
# File 'lib/steep/ast/node/type_assertion.rb', line 11

def source
  location.source
end

#type(context, subtyping, type_vars) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/steep/ast/node/type_assertion.rb', line 19

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)
    resolver = RBS::Resolver::TypeNameResolver.new(subtyping.factory.env)
    ty = ty.map_type_name {|name| resolver.resolve(name, context: context) || name.absolute! }

    validator = Signature::Validator.new(checker: subtyping)
    validator.rescue_validation_errors do
      validator.validate_type(ty)
    end

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

#type?(context, subtyping, type_vars) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
# File 'lib/steep/ast/node/type_assertion.rb', line 48

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

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

#type_locationObject



63
64
65
66
# File 'lib/steep/ast/node/type_assertion.rb', line 63

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

#type_strObject



59
60
61
# File 'lib/steep/ast/node/type_assertion.rb', line 59

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

#type_syntax?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
# File 'lib/steep/ast/node/type_assertion.rb', line 41

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