Class: Steep::AST::Annotation::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/ast/annotation/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(annotations:, builder:, current_module:) ⇒ Collection

Returns a new instance of Collection.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/steep/ast/annotation/collection.rb', line 22

def initialize(annotations:, builder:, current_module:)
  @annotations = annotations
  @builder = builder
  @current_module = current_module

  @var_type_annotations = {}
  @method_type_annotations = {}
  @const_type_annotations = {}
  @ivar_type_annotations = {}
  @dynamic_annotations = []

  annotations.each do |annotation|
    case annotation
    when VarType
      var_type_annotations[annotation.name] = annotation
    when MethodType
      method_type_annotations[annotation.name] = annotation
    when BlockType
      @block_type_annotation = annotation
    when ReturnType
      @return_type_annotation = annotation
    when SelfType
      @self_type_annotation = annotation
    when ConstType
      @const_type_annotations[annotation.name] = annotation
    when InstanceType
      @instance_type_annotation = annotation
    when ModuleType
      @module_type_annotation = annotation
    when Implements
      @implement_module_annotation = annotation
    when IvarType
      @ivar_type_annotations[annotation.name] = annotation
    when Dynamic
      @dynamic_annotations << annotation
    when BreakType
      @break_type_annotation = annotation
    else
      raise "Unexpected annotation: #{annotation.inspect}"
    end
  end
end

Instance Attribute Details

#annotationsObject (readonly)

Returns the value of attribute annotations.



5
6
7
# File 'lib/steep/ast/annotation/collection.rb', line 5

def annotations
  @annotations
end

#block_type_annotationObject (readonly)

Returns the value of attribute block_type_annotation.



13
14
15
# File 'lib/steep/ast/annotation/collection.rb', line 13

def block_type_annotation
  @block_type_annotation
end

#break_type_annotationObject (readonly)

Returns the value of attribute break_type_annotation.



20
21
22
# File 'lib/steep/ast/annotation/collection.rb', line 20

def break_type_annotation
  @break_type_annotation
end

#builderObject (readonly)

Returns the value of attribute builder.



6
7
8
# File 'lib/steep/ast/annotation/collection.rb', line 6

def builder
  @builder
end

#const_type_annotationsObject (readonly)

Returns the value of attribute const_type_annotations.



10
11
12
# File 'lib/steep/ast/annotation/collection.rb', line 10

def const_type_annotations
  @const_type_annotations
end

#current_moduleObject (readonly)

Returns the value of attribute current_module.



7
8
9
# File 'lib/steep/ast/annotation/collection.rb', line 7

def current_module
  @current_module
end

#dynamic_annotationsObject (readonly)

Returns the value of attribute dynamic_annotations.



19
20
21
# File 'lib/steep/ast/annotation/collection.rb', line 19

def dynamic_annotations
  @dynamic_annotations
end

#implement_module_annotationObject (readonly)

Returns the value of attribute implement_module_annotation.



18
19
20
# File 'lib/steep/ast/annotation/collection.rb', line 18

def implement_module_annotation
  @implement_module_annotation
end

#instance_type_annotationObject (readonly)

Returns the value of attribute instance_type_annotation.



16
17
18
# File 'lib/steep/ast/annotation/collection.rb', line 16

def instance_type_annotation
  @instance_type_annotation
end

#ivar_type_annotationsObject (readonly)

Returns the value of attribute ivar_type_annotations.



11
12
13
# File 'lib/steep/ast/annotation/collection.rb', line 11

def ivar_type_annotations
  @ivar_type_annotations
end

#method_type_annotationsObject (readonly)

Returns the value of attribute method_type_annotations.



12
13
14
# File 'lib/steep/ast/annotation/collection.rb', line 12

def method_type_annotations
  @method_type_annotations
end

#module_type_annotationObject (readonly)

Returns the value of attribute module_type_annotation.



17
18
19
# File 'lib/steep/ast/annotation/collection.rb', line 17

def module_type_annotation
  @module_type_annotation
end

#return_type_annotationObject (readonly)

Returns the value of attribute return_type_annotation.



14
15
16
# File 'lib/steep/ast/annotation/collection.rb', line 14

def return_type_annotation
  @return_type_annotation
end

#self_type_annotationObject (readonly)

Returns the value of attribute self_type_annotation.



15
16
17
# File 'lib/steep/ast/annotation/collection.rb', line 15

def self_type_annotation
  @self_type_annotation
end

#var_type_annotationsObject (readonly)

Returns the value of attribute var_type_annotations.



9
10
11
# File 'lib/steep/ast/annotation/collection.rb', line 9

def var_type_annotations
  @var_type_annotations
end

Instance Method Details

#absolute_type(type) ⇒ Object



65
66
67
68
69
# File 'lib/steep/ast/annotation/collection.rb', line 65

def absolute_type(type)
  if type
    builder.absolute_type(type, current: current_module)
  end
end

#any?(&block) ⇒ Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/steep/ast/annotation/collection.rb', line 156

def any?(&block)
  annotations.any?(&block)
end

#block_typeObject



88
89
90
# File 'lib/steep/ast/annotation/collection.rb', line 88

def block_type
  absolute_type(block_type_annotation&.type)
end

#break_typeObject



108
109
110
# File 'lib/steep/ast/annotation/collection.rb', line 108

def break_type
  absolute_type(break_type_annotation&.type)
end

#const_typesObject



124
125
126
127
128
# File 'lib/steep/ast/annotation/collection.rb', line 124

def const_types
  const_type_annotations.each_key.with_object({}) do |name, hash|
    hash[name] = var_type(const: name)
  end
end

#include?(obj) ⇒ Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/steep/ast/annotation/collection.rb', line 164

def include?(obj)
  annotations.include?(obj)
end

#instance_dynamicsObject



130
131
132
133
134
# File 'lib/steep/ast/annotation/collection.rb', line 130

def instance_dynamics
  dynamic_annotations.flat_map do |annot|
    annot.names.select(&:instance_method?).map(&:name)
  end
end

#instance_typeObject



100
101
102
# File 'lib/steep/ast/annotation/collection.rb', line 100

def instance_type
  absolute_type(instance_type_annotation&.type)
end

#ivar_typesObject



118
119
120
121
122
# File 'lib/steep/ast/annotation/collection.rb', line 118

def ivar_types
  ivar_type_annotations.each_key.with_object({}) do |name, hash|
    hash[name] = var_type(ivar: name)
  end
end

#lvar_typesObject



112
113
114
115
116
# File 'lib/steep/ast/annotation/collection.rb', line 112

def lvar_types
  var_type_annotations.each_key.with_object({}) do |name, hash|
    hash[name] = var_type(lvar: name)
  end
end

#merge_block_annotations(annotations) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/steep/ast/annotation/collection.rb', line 142

def merge_block_annotations(annotations)
  if annotations.current_module != current_module || annotations.builder != builder
    raise "Cannot merge another annotation: self=#{self}, other=#{annotations}"
  end

  retained_annotations = self.annotations.reject do |annotation|
    annotation.is_a?(BlockType) || annotation.is_a?(BreakType)
  end

  self.class.new(annotations: retained_annotations + annotations.annotations,
                 builder: builder,
                 current_module: current_module)
end

#method_type(name) ⇒ Object



82
83
84
85
86
# File 'lib/steep/ast/annotation/collection.rb', line 82

def method_type(name)
  if (a = method_type_annotations[name])
    builder.method_type_to_method_type(a.type, current: current_module)
  end
end

#module_dynamicsObject



136
137
138
139
140
# File 'lib/steep/ast/annotation/collection.rb', line 136

def module_dynamics
  dynamic_annotations.flat_map do |annot|
    annot.names.select(&:module_method?).map(&:name)
  end
end

#module_typeObject



104
105
106
# File 'lib/steep/ast/annotation/collection.rb', line 104

def module_type
  absolute_type(module_type_annotation&.type)
end

#return_typeObject



92
93
94
# File 'lib/steep/ast/annotation/collection.rb', line 92

def return_type
  absolute_type(return_type_annotation&.type)
end

#self_typeObject



96
97
98
# File 'lib/steep/ast/annotation/collection.rb', line 96

def self_type
  absolute_type(self_type_annotation&.type)
end

#sizeObject



160
161
162
# File 'lib/steep/ast/annotation/collection.rb', line 160

def size
  annotations.size
end

#var_type(lvar: nil, ivar: nil, const: nil) ⇒ Object



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

def var_type(lvar: nil, ivar: nil, const: nil)
  case
  when lvar
    absolute_type(var_type_annotations[lvar]&.type)
  when ivar
    absolute_type(ivar_type_annotations[ivar]&.type)
  when const
    absolute_type(const_type_annotations[const]&.type)
  end
end