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



71
72
73
74
75
76
77
78
79
80
# File 'lib/steep/ast/node/type_assertion.rb', line 71

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

#rbs_type(context, subtyping, type_vars) ⇒ Object



19
20
21
22
23
24
# File 'lib/steep/ast/node/type_assertion.rb', line 19

def rbs_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.build(subtyping.factory.env)
    ty = ty.map_type_name {|name| resolver.resolve(name, context: context) || name.absolute! }
  end
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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/steep/ast/node/type_assertion.rb', line 26

def type(context, subtyping, type_vars)
  if ty = rbs_type(context, subtyping, type_vars)
    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)


51
52
53
54
55
56
57
58
59
60
# File 'lib/steep/ast/node/type_assertion.rb', line 51

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



66
67
68
69
# File 'lib/steep/ast/node/type_assertion.rb', line 66

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

#type_strObject



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

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

#type_syntax?Boolean

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/steep/ast/node/type_assertion.rb', line 45

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