steep 0.13.0 → 0.16.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (214) hide show
  1. checksums.yaml +4 -4
  2. data/.gitmodules +0 -3
  3. data/CHANGELOG.md +28 -0
  4. data/Rakefile +0 -13
  5. data/bin/setup +0 -2
  6. data/bin/smoke_runner.rb +0 -1
  7. data/exe/steep +0 -1
  8. data/lib/steep.rb +33 -1
  9. data/lib/steep/annotation_parser.rb +4 -4
  10. data/lib/steep/ast/buffer.rb +11 -7
  11. data/lib/steep/ast/builtin.rb +8 -0
  12. data/lib/steep/ast/types/factory.rb +124 -89
  13. data/lib/steep/cli.rb +16 -1
  14. data/lib/steep/drivers/annotations.rb +1 -1
  15. data/lib/steep/drivers/check.rb +20 -4
  16. data/lib/steep/drivers/init.rb +5 -5
  17. data/lib/steep/drivers/langserver.rb +13 -287
  18. data/lib/steep/drivers/utils/driver_helper.rb +1 -1
  19. data/lib/steep/drivers/vendor.rb +2 -2
  20. data/lib/steep/drivers/watch.rb +97 -85
  21. data/lib/steep/drivers/worker.rb +51 -0
  22. data/lib/steep/project.rb +9 -5
  23. data/lib/steep/project/completion_provider.rb +298 -0
  24. data/lib/steep/project/dsl.rb +14 -0
  25. data/lib/steep/project/file.rb +54 -47
  26. data/lib/steep/project/hover_content.rb +17 -8
  27. data/lib/steep/project/options.rb +25 -3
  28. data/lib/steep/project/target.rb +40 -24
  29. data/lib/steep/server/base_worker.rb +56 -0
  30. data/lib/steep/server/code_worker.rb +151 -0
  31. data/lib/steep/server/interaction_worker.rb +281 -0
  32. data/lib/steep/server/master.rb +196 -0
  33. data/lib/steep/server/signature_worker.rb +148 -0
  34. data/lib/steep/server/utils.rb +36 -0
  35. data/lib/steep/server/worker_process.rb +62 -0
  36. data/lib/steep/signature/errors.rb +1 -1
  37. data/lib/steep/signature/validator.rb +13 -13
  38. data/lib/steep/source.rb +1 -1
  39. data/lib/steep/type_construction.rb +1004 -727
  40. data/lib/steep/type_inference/constant_env.rb +3 -11
  41. data/lib/steep/type_inference/context.rb +8 -3
  42. data/lib/steep/type_inference/context_array.rb +111 -0
  43. data/lib/steep/type_inference/local_variable_type_env.rb +226 -0
  44. data/lib/steep/type_inference/logic.rb +130 -0
  45. data/lib/steep/type_inference/type_env.rb +5 -69
  46. data/lib/steep/typing.rb +91 -23
  47. data/lib/steep/version.rb +1 -1
  48. data/smoke/alias/Steepfile +1 -0
  49. data/smoke/alias/a.rb +1 -1
  50. data/smoke/and/Steepfile +1 -0
  51. data/smoke/array/Steepfile +1 -0
  52. data/smoke/array/b.rb +0 -2
  53. data/smoke/block/Steepfile +1 -0
  54. data/smoke/case/Steepfile +1 -0
  55. data/smoke/class/Steepfile +1 -0
  56. data/smoke/const/Steepfile +1 -0
  57. data/smoke/dstr/Steepfile +1 -0
  58. data/smoke/ensure/Steepfile +1 -0
  59. data/smoke/enumerator/Steepfile +1 -0
  60. data/smoke/extension/Steepfile +1 -0
  61. data/smoke/extension/c.rb +1 -0
  62. data/smoke/hash/Steepfile +1 -0
  63. data/smoke/hello/Steepfile +1 -0
  64. data/smoke/if/Steepfile +1 -0
  65. data/smoke/if/a.rb +1 -1
  66. data/smoke/implements/Steepfile +1 -0
  67. data/smoke/initialize/Steepfile +1 -0
  68. data/smoke/integer/Steepfile +1 -0
  69. data/smoke/interface/Steepfile +1 -0
  70. data/smoke/kwbegin/Steepfile +1 -0
  71. data/smoke/lambda/Steepfile +1 -0
  72. data/smoke/literal/Steepfile +1 -0
  73. data/smoke/map/Steepfile +1 -0
  74. data/smoke/method/Steepfile +1 -0
  75. data/smoke/module/Steepfile +1 -0
  76. data/smoke/regexp/Steepfile +1 -0
  77. data/smoke/regexp/b.rb +4 -4
  78. data/smoke/regression/Steepfile +1 -0
  79. data/smoke/rescue/Steepfile +1 -0
  80. data/smoke/rescue/a.rb +1 -1
  81. data/smoke/self/Steepfile +1 -0
  82. data/smoke/skip/Steepfile +1 -0
  83. data/smoke/stdout/Steepfile +1 -0
  84. data/smoke/super/Steepfile +1 -0
  85. data/smoke/type_case/Steepfile +1 -0
  86. data/smoke/yield/Steepfile +1 -0
  87. data/steep.gemspec +8 -8
  88. metadata +38 -138
  89. data/exe/rbs +0 -3
  90. data/exe/ruby-signature +0 -3
  91. data/vendor/ruby-signature/.github/workflows/ruby.yml +0 -27
  92. data/vendor/ruby-signature/.gitignore +0 -12
  93. data/vendor/ruby-signature/.rubocop.yml +0 -15
  94. data/vendor/ruby-signature/BSDL +0 -22
  95. data/vendor/ruby-signature/COPYING +0 -56
  96. data/vendor/ruby-signature/Gemfile +0 -6
  97. data/vendor/ruby-signature/README.md +0 -93
  98. data/vendor/ruby-signature/Rakefile +0 -66
  99. data/vendor/ruby-signature/bin/annotate-with-rdoc +0 -156
  100. data/vendor/ruby-signature/bin/console +0 -14
  101. data/vendor/ruby-signature/bin/query-rdoc +0 -103
  102. data/vendor/ruby-signature/bin/setup +0 -10
  103. data/vendor/ruby-signature/bin/sort +0 -88
  104. data/vendor/ruby-signature/bin/test_runner.rb +0 -17
  105. data/vendor/ruby-signature/docs/CONTRIBUTING.md +0 -97
  106. data/vendor/ruby-signature/docs/sigs.md +0 -148
  107. data/vendor/ruby-signature/docs/stdlib.md +0 -152
  108. data/vendor/ruby-signature/docs/syntax.md +0 -528
  109. data/vendor/ruby-signature/exe/rbs +0 -3
  110. data/vendor/ruby-signature/exe/ruby-signature +0 -7
  111. data/vendor/ruby-signature/lib/ruby/signature.rb +0 -64
  112. data/vendor/ruby-signature/lib/ruby/signature/ast/annotation.rb +0 -29
  113. data/vendor/ruby-signature/lib/ruby/signature/ast/comment.rb +0 -29
  114. data/vendor/ruby-signature/lib/ruby/signature/ast/declarations.rb +0 -391
  115. data/vendor/ruby-signature/lib/ruby/signature/ast/members.rb +0 -364
  116. data/vendor/ruby-signature/lib/ruby/signature/buffer.rb +0 -52
  117. data/vendor/ruby-signature/lib/ruby/signature/builtin_names.rb +0 -54
  118. data/vendor/ruby-signature/lib/ruby/signature/cli.rb +0 -534
  119. data/vendor/ruby-signature/lib/ruby/signature/constant.rb +0 -28
  120. data/vendor/ruby-signature/lib/ruby/signature/constant_table.rb +0 -152
  121. data/vendor/ruby-signature/lib/ruby/signature/definition.rb +0 -172
  122. data/vendor/ruby-signature/lib/ruby/signature/definition_builder.rb +0 -921
  123. data/vendor/ruby-signature/lib/ruby/signature/environment.rb +0 -283
  124. data/vendor/ruby-signature/lib/ruby/signature/environment_loader.rb +0 -138
  125. data/vendor/ruby-signature/lib/ruby/signature/environment_walker.rb +0 -126
  126. data/vendor/ruby-signature/lib/ruby/signature/errors.rb +0 -189
  127. data/vendor/ruby-signature/lib/ruby/signature/location.rb +0 -104
  128. data/vendor/ruby-signature/lib/ruby/signature/method_type.rb +0 -125
  129. data/vendor/ruby-signature/lib/ruby/signature/namespace.rb +0 -93
  130. data/vendor/ruby-signature/lib/ruby/signature/parser.y +0 -1343
  131. data/vendor/ruby-signature/lib/ruby/signature/prototype/rb.rb +0 -441
  132. data/vendor/ruby-signature/lib/ruby/signature/prototype/rbi.rb +0 -579
  133. data/vendor/ruby-signature/lib/ruby/signature/prototype/runtime.rb +0 -383
  134. data/vendor/ruby-signature/lib/ruby/signature/substitution.rb +0 -48
  135. data/vendor/ruby-signature/lib/ruby/signature/test.rb +0 -28
  136. data/vendor/ruby-signature/lib/ruby/signature/test/errors.rb +0 -63
  137. data/vendor/ruby-signature/lib/ruby/signature/test/hook.rb +0 -290
  138. data/vendor/ruby-signature/lib/ruby/signature/test/setup.rb +0 -58
  139. data/vendor/ruby-signature/lib/ruby/signature/test/spy.rb +0 -324
  140. data/vendor/ruby-signature/lib/ruby/signature/test/test_helper.rb +0 -185
  141. data/vendor/ruby-signature/lib/ruby/signature/test/type_check.rb +0 -256
  142. data/vendor/ruby-signature/lib/ruby/signature/type_name.rb +0 -72
  143. data/vendor/ruby-signature/lib/ruby/signature/types.rb +0 -932
  144. data/vendor/ruby-signature/lib/ruby/signature/variance_calculator.rb +0 -140
  145. data/vendor/ruby-signature/lib/ruby/signature/vendorer.rb +0 -49
  146. data/vendor/ruby-signature/lib/ruby/signature/version.rb +0 -5
  147. data/vendor/ruby-signature/lib/ruby/signature/writer.rb +0 -271
  148. data/vendor/ruby-signature/ruby-signature.gemspec +0 -45
  149. data/vendor/ruby-signature/stdlib/abbrev/abbrev.rbs +0 -3
  150. data/vendor/ruby-signature/stdlib/base64/base64.rbs +0 -15
  151. data/vendor/ruby-signature/stdlib/builtin/array.rbs +0 -1997
  152. data/vendor/ruby-signature/stdlib/builtin/basic_object.rbs +0 -280
  153. data/vendor/ruby-signature/stdlib/builtin/binding.rbs +0 -177
  154. data/vendor/ruby-signature/stdlib/builtin/builtin.rbs +0 -35
  155. data/vendor/ruby-signature/stdlib/builtin/class.rbs +0 -145
  156. data/vendor/ruby-signature/stdlib/builtin/comparable.rbs +0 -116
  157. data/vendor/ruby-signature/stdlib/builtin/complex.rbs +0 -400
  158. data/vendor/ruby-signature/stdlib/builtin/constants.rbs +0 -37
  159. data/vendor/ruby-signature/stdlib/builtin/data.rbs +0 -5
  160. data/vendor/ruby-signature/stdlib/builtin/deprecated.rbs +0 -2
  161. data/vendor/ruby-signature/stdlib/builtin/dir.rbs +0 -419
  162. data/vendor/ruby-signature/stdlib/builtin/encoding.rbs +0 -606
  163. data/vendor/ruby-signature/stdlib/builtin/enumerable.rbs +0 -404
  164. data/vendor/ruby-signature/stdlib/builtin/enumerator.rbs +0 -260
  165. data/vendor/ruby-signature/stdlib/builtin/errno.rbs +0 -781
  166. data/vendor/ruby-signature/stdlib/builtin/errors.rbs +0 -582
  167. data/vendor/ruby-signature/stdlib/builtin/exception.rbs +0 -193
  168. data/vendor/ruby-signature/stdlib/builtin/false_class.rbs +0 -40
  169. data/vendor/ruby-signature/stdlib/builtin/fiber.rbs +0 -68
  170. data/vendor/ruby-signature/stdlib/builtin/fiber_error.rbs +0 -12
  171. data/vendor/ruby-signature/stdlib/builtin/file.rbs +0 -476
  172. data/vendor/ruby-signature/stdlib/builtin/file_test.rbs +0 -59
  173. data/vendor/ruby-signature/stdlib/builtin/float.rbs +0 -696
  174. data/vendor/ruby-signature/stdlib/builtin/gc.rbs +0 -121
  175. data/vendor/ruby-signature/stdlib/builtin/hash.rbs +0 -1029
  176. data/vendor/ruby-signature/stdlib/builtin/integer.rbs +0 -710
  177. data/vendor/ruby-signature/stdlib/builtin/io.rbs +0 -683
  178. data/vendor/ruby-signature/stdlib/builtin/kernel.rbs +0 -574
  179. data/vendor/ruby-signature/stdlib/builtin/marshal.rbs +0 -135
  180. data/vendor/ruby-signature/stdlib/builtin/match_data.rbs +0 -141
  181. data/vendor/ruby-signature/stdlib/builtin/math.rbs +0 -66
  182. data/vendor/ruby-signature/stdlib/builtin/method.rbs +0 -182
  183. data/vendor/ruby-signature/stdlib/builtin/module.rbs +0 -248
  184. data/vendor/ruby-signature/stdlib/builtin/nil_class.rbs +0 -82
  185. data/vendor/ruby-signature/stdlib/builtin/numeric.rbs +0 -409
  186. data/vendor/ruby-signature/stdlib/builtin/object.rbs +0 -824
  187. data/vendor/ruby-signature/stdlib/builtin/proc.rbs +0 -426
  188. data/vendor/ruby-signature/stdlib/builtin/process.rbs +0 -354
  189. data/vendor/ruby-signature/stdlib/builtin/random.rbs +0 -93
  190. data/vendor/ruby-signature/stdlib/builtin/range.rbs +0 -226
  191. data/vendor/ruby-signature/stdlib/builtin/rational.rbs +0 -424
  192. data/vendor/ruby-signature/stdlib/builtin/rb_config.rbs +0 -10
  193. data/vendor/ruby-signature/stdlib/builtin/regexp.rbs +0 -131
  194. data/vendor/ruby-signature/stdlib/builtin/ruby_vm.rbs +0 -14
  195. data/vendor/ruby-signature/stdlib/builtin/signal.rbs +0 -55
  196. data/vendor/ruby-signature/stdlib/builtin/string.rbs +0 -770
  197. data/vendor/ruby-signature/stdlib/builtin/string_io.rbs +0 -13
  198. data/vendor/ruby-signature/stdlib/builtin/struct.rbs +0 -40
  199. data/vendor/ruby-signature/stdlib/builtin/symbol.rbs +0 -230
  200. data/vendor/ruby-signature/stdlib/builtin/thread.rbs +0 -1112
  201. data/vendor/ruby-signature/stdlib/builtin/thread_group.rbs +0 -23
  202. data/vendor/ruby-signature/stdlib/builtin/time.rbs +0 -739
  203. data/vendor/ruby-signature/stdlib/builtin/trace_point.rbs +0 -91
  204. data/vendor/ruby-signature/stdlib/builtin/true_class.rbs +0 -46
  205. data/vendor/ruby-signature/stdlib/builtin/unbound_method.rbs +0 -159
  206. data/vendor/ruby-signature/stdlib/builtin/warning.rbs +0 -17
  207. data/vendor/ruby-signature/stdlib/erb/erb.rbs +0 -18
  208. data/vendor/ruby-signature/stdlib/find/find.rbs +0 -44
  209. data/vendor/ruby-signature/stdlib/pathname/pathname.rbs +0 -21
  210. data/vendor/ruby-signature/stdlib/prime/integer-extension.rbs +0 -23
  211. data/vendor/ruby-signature/stdlib/prime/prime.rbs +0 -188
  212. data/vendor/ruby-signature/stdlib/securerandom/securerandom.rbs +0 -9
  213. data/vendor/ruby-signature/stdlib/set/set.rbs +0 -77
  214. data/vendor/ruby-signature/stdlib/tmpdir/tmpdir.rbs +0 -53
@@ -1,283 +0,0 @@
1
- module Ruby
2
- module Signature
3
- class Environment
4
- attr_reader :buffers
5
- attr_reader :declarations
6
-
7
- attr_reader :name_to_decl
8
- attr_reader :name_to_extensions
9
- attr_reader :name_to_constant
10
- attr_reader :name_to_global
11
- attr_reader :name_to_alias
12
-
13
- def initialize
14
- @buffers = []
15
- @declarations = []
16
-
17
- @name_to_decl = {}
18
- @name_to_extensions = {}
19
- @name_to_constant = {}
20
- @name_to_global = {}
21
- @name_to_alias = {}
22
- end
23
-
24
- def initialize_copy(other)
25
- @buffers = other.buffers.dup
26
- @declarations = other.declarations.dup
27
-
28
- @name_to_decl = other.name_to_decl.dup
29
- @name_to_extensions = other.name_to_extensions.dup
30
- @name_to_constant = other.name_to_constant.dup
31
- @name_to_global = other.name_to_global.dup
32
- @name_to_alias = other.name_to_alias.dup
33
- end
34
-
35
- def cache_name(cache, name:, decl:)
36
- if cache.key?(name)
37
- raise DuplicatedDeclarationError.new(name, decl, cache[name])
38
- end
39
- cache[name] = decl
40
- end
41
-
42
- def <<(decl)
43
- declarations << decl
44
- case decl
45
- when AST::Declarations::Class, AST::Declarations::Module, AST::Declarations::Interface
46
- cache_name name_to_decl, name: decl.name.absolute!, decl: decl
47
- when AST::Declarations::Extension
48
- yield_self do
49
- name = decl.name.absolute!
50
- exts = name_to_extensions.fetch(name) do
51
- name_to_extensions[name] = []
52
- end
53
- exts << decl
54
- end
55
- when AST::Declarations::Alias
56
- cache_name name_to_alias, name: decl.name.absolute!, decl: decl
57
- when AST::Declarations::Constant
58
- cache_name name_to_constant, name: decl.name.absolute!, decl: decl
59
- when AST::Declarations::Global
60
- cache_name name_to_global, name: decl.name, decl: decl
61
- end
62
- end
63
-
64
- def find_class(type_name)
65
- name_to_decl[type_name]
66
- end
67
-
68
- def each_decl
69
- if block_given?
70
- name_to_decl.each do |name, decl|
71
- yield name, decl
72
- end
73
- else
74
- enum_for :each_decl
75
- end
76
- end
77
-
78
- def each_constant
79
- if block_given?
80
- name_to_constant.each do |name, decl|
81
- yield name, decl
82
- end
83
- else
84
- enum_for :each_constant
85
- end
86
- end
87
-
88
- def each_global
89
- if block_given?
90
- name_to_global.each do |name, global|
91
- yield name, global
92
- end
93
- else
94
- enum_for :each_global
95
- end
96
- end
97
-
98
- def each_alias(&block)
99
- if block_given?
100
- name_to_alias.each(&block)
101
- else
102
- enum_for :each_alias
103
- end
104
- end
105
-
106
- def each_class_name(&block)
107
- each_decl.select {|name,| class?(name) }.each(&block)
108
- end
109
-
110
- def class?(type_name)
111
- find_class(type_name)&.yield_self do |decl|
112
- decl.is_a?(AST::Declarations::Class) || decl.is_a?(AST::Declarations::Module)
113
- end
114
- end
115
-
116
- def find_type_decl(type_name)
117
- name_to_decl[type_name]
118
- end
119
-
120
- def find_extensions(type_name)
121
- name_to_extensions[type_name] || []
122
- end
123
-
124
- def find_alias(type_name)
125
- name_to_alias[type_name]
126
- end
127
-
128
- def each_extension(type_name, &block)
129
- if block_given?
130
- (name_to_extensions[type_name] || []).each(&block)
131
- else
132
- enum_for :each_extension, type_name
133
- end
134
- end
135
-
136
- def absolute_type_name_in(environment, name:, namespace:)
137
- raise "Namespace should be absolute: #{namespace}" unless namespace.absolute?
138
-
139
- if name.absolute?
140
- name if environment.key?(name)
141
- else
142
- absolute_name = name.with_prefix(namespace)
143
-
144
- if environment.key?(absolute_name)
145
- absolute_name
146
- else
147
- if namespace.empty?
148
- nil
149
- else
150
- parent = namespace.parent
151
- absolute_type_name_in environment, name: name, namespace: parent
152
- end
153
- end
154
- end
155
- end
156
-
157
- def absolute_class_name(name, namespace:)
158
- raise "Class name expected: #{name}" unless name.class?
159
- absolute_type_name_in name_to_decl, name: name, namespace: namespace
160
- end
161
-
162
- def absolute_interface_name(name, namespace:)
163
- raise "Interface name expected: #{name}" unless name.interface?
164
- absolute_type_name_in name_to_decl, name: name, namespace: namespace
165
- end
166
-
167
- def absolute_alias_name(name, namespace:)
168
- raise "Alias name expected: #{name}" unless name.alias?
169
- absolute_type_name_in name_to_alias, name: name, namespace: namespace
170
- end
171
-
172
- def absolute_type_name(type_name, namespace:)
173
- absolute_name = case
174
- when type_name.class?
175
- absolute_class_name(type_name, namespace: namespace)
176
- when type_name.alias?
177
- absolute_alias_name(type_name, namespace: namespace)
178
- when type_name.interface?
179
- absolute_interface_name(type_name, namespace: namespace)
180
- end
181
-
182
- absolute_name || yield(type_name)
183
- end
184
-
185
- def absolute_name_or(name, type)
186
- if name.absolute?
187
- type
188
- else
189
- yield
190
- end
191
- end
192
-
193
- def absolute_type(type, namespace:, &block)
194
- case type
195
- when Types::ClassSingleton
196
- absolute_name_or(type.name, type) do
197
- absolute_name = absolute_type_name(type.name, namespace: namespace) { yield(type) }
198
- Types::ClassSingleton.new(name: absolute_name, location: type.location)
199
- end
200
- when Types::ClassInstance
201
- absolute_name = absolute_type_name(type.name, namespace: namespace) { yield(type) }
202
- Types::ClassInstance.new(name: absolute_name,
203
- args: type.args.map {|ty|
204
- absolute_type(ty, namespace: namespace, &block)
205
- },
206
- location: type.location)
207
- when Types::Interface
208
- absolute_name = absolute_type_name(type.name, namespace: namespace) { yield(type) }
209
- Types::Interface.new(name: absolute_name,
210
- args: type.args.map {|ty|
211
- absolute_type(ty, namespace: namespace, &block)
212
- },
213
- location: type.location)
214
- when Types::Alias
215
- absolute_name_or(type.name, type) do
216
- absolute_name = absolute_type_name(type.name, namespace: namespace) { yield(type) }
217
- Types::Alias.new(name: absolute_name, location: type.location)
218
- end
219
- when Types::Tuple
220
- Types::Tuple.new(
221
- types: type.types.map {|ty| absolute_type(ty, namespace: namespace, &block) },
222
- location: type.location
223
- )
224
- when Types::Record
225
- Types::Record.new(
226
- fields: type.fields.transform_values {|ty| absolute_type(ty, namespace: namespace, &block) },
227
- location: type.location
228
- )
229
- when Types::Union
230
- Types::Union.new(
231
- types: type.types.map {|ty| absolute_type(ty, namespace: namespace, &block) },
232
- location: type.location
233
- )
234
- when Types::Intersection
235
- Types::Intersection.new(
236
- types: type.types.map {|ty| absolute_type(ty, namespace: namespace, &block) },
237
- location: type.location
238
- )
239
- when Types::Optional
240
- Types::Optional.new(
241
- type: absolute_type(type.type, namespace: namespace, &block),
242
- location: type.location
243
- )
244
- when Types::Proc
245
- Types::Proc.new(
246
- type: type.type.map_type {|ty| absolute_type(ty, namespace: namespace, &block) },
247
- location: type.location
248
- )
249
- else
250
- type
251
- end
252
- end
253
-
254
- # Validates presence of the relative type, and application arity match.
255
- def validate(type, namespace:)
256
- case type
257
- when Types::ClassInstance, Types::Interface
258
- if type.name.namespace.relative?
259
- type = absolute_type(type, namespace: namespace) do |type|
260
- NoTypeFoundError.check!(type.name.absolute!, env: self, location: type.location)
261
- end
262
- end
263
-
264
- decl = find_class(type.name)
265
- unless decl
266
- raise NoTypeFoundError.new(type_name: type.name, location: type.location)
267
- end
268
-
269
- InvalidTypeApplicationError.check!(
270
- type_name: type.name,
271
- args: type.args,
272
- params: decl.type_params,
273
- location: type.location
274
- )
275
- end
276
-
277
- type.each_type do |type_|
278
- validate(type_, namespace: namespace)
279
- end
280
- end
281
- end
282
- end
283
- end
@@ -1,138 +0,0 @@
1
- module Ruby
2
- module Signature
3
- class EnvironmentLoader
4
- class UnknownLibraryNameError < StandardError
5
- attr_reader :name
6
-
7
- def initialize(name:)
8
- @name = name
9
- super "Cannot find a library or gem: `#{name}`"
10
- end
11
- end
12
-
13
- LibraryPath = Struct.new(:name, :path, keyword_init: true)
14
- GemPath = Struct.new(:name, :version, :path, keyword_init: true)
15
-
16
- attr_reader :paths
17
- attr_reader :stdlib_root
18
- attr_reader :gem_vendor_path
19
-
20
- STDLIB_ROOT = Pathname(__dir__) + "../../../stdlib"
21
-
22
- def self.gem_sig_path(name, version)
23
- Pathname(Gem::Specification.find_by_name(name, version).gem_dir) + "sig"
24
- rescue Gem::MissingSpecError
25
- nil
26
- end
27
-
28
- def initialize(stdlib_root: STDLIB_ROOT, gem_vendor_path: nil)
29
- @stdlib_root = stdlib_root
30
- @gem_vendor_path = gem_vendor_path
31
- @paths = []
32
- @no_builtin = false
33
- end
34
-
35
- def add(path: nil, library: nil)
36
- case
37
- when path
38
- @paths << path
39
- when library
40
- name, version = self.class.parse_library(library)
41
-
42
- case
43
- when !version && path = stdlib?(name)
44
- @paths << LibraryPath.new(name: name, path: path)
45
- when (path = gem?(name, version))
46
- @paths << GemPath.new(name: name, version: version, path: path)
47
- else
48
- raise UnknownLibraryNameError.new(name: library)
49
- end
50
- end
51
- end
52
-
53
- def self.parse_library(lib)
54
- lib.split(/:/)
55
- end
56
-
57
- def stdlib?(name)
58
- if stdlib_root
59
- path = stdlib_root + name
60
- if path.directory?
61
- path
62
- end
63
- end
64
- end
65
-
66
- def gem?(name, version)
67
- if gem_vendor_path
68
- # Try vendored RBS first
69
- gem_dir = gem_vendor_path + name
70
- if gem_dir.directory?
71
- return gem_dir
72
- end
73
- end
74
-
75
- # Try ruby gem library
76
- self.class.gem_sig_path(name, version)
77
- end
78
-
79
- def each_signature(path = nil, immediate: true, &block)
80
- if block_given?
81
- if path
82
- case
83
- when path.file?
84
- if path.extname == ".rbs" || immediate
85
- yield path
86
- end
87
- when path.directory?
88
- path.children.each do |child|
89
- each_signature child, immediate: false, &block
90
- end
91
- end
92
- else
93
- paths.each do |path|
94
- case path
95
- when Pathname
96
- each_signature path, immediate: immediate, &block
97
- when LibraryPath
98
- each_signature path.path, immediate: immediate, &block
99
- when GemPath
100
- each_signature path.path, immediate: immediate, &block
101
- end
102
- end
103
- end
104
- else
105
- enum_for :each_signature, path, immediate: immediate
106
- end
107
- end
108
-
109
- def no_builtin!
110
- @no_builtin = true
111
- end
112
-
113
- def no_builtin?
114
- @no_builtin
115
- end
116
-
117
- def load(env:)
118
- signature_files = []
119
-
120
- unless no_builtin?
121
- signature_files.push(*each_signature(stdlib_root + "builtin"))
122
- end
123
-
124
- each_signature do |path|
125
- signature_files.push path
126
- end
127
-
128
- signature_files.each do |file|
129
- buffer = Buffer.new(name: file.to_s, content: file.read)
130
- env.buffers.push(buffer)
131
- Parser.parse_signature(buffer).each do |decl|
132
- env << decl
133
- end
134
- end
135
- end
136
- end
137
- end
138
- end
@@ -1,126 +0,0 @@
1
- module Ruby
2
- module Signature
3
- class EnvironmentWalker
4
- attr_reader :env
5
-
6
- def initialize(env:)
7
- @env = env
8
- @only_ancestors = nil
9
- end
10
-
11
- def builder
12
- @builder ||= DefinitionBuilder.new(env: env)
13
- end
14
-
15
- def only_ancestors!(only = true)
16
- @only_ancestors = only
17
- self
18
- end
19
-
20
- def only_ancestors?
21
- @only_ancestors
22
- end
23
-
24
- include TSort
25
-
26
- def tsort_each_node(&block)
27
- env.each_decl do |name|
28
- yield name.absolute!
29
- end
30
-
31
- env.each_alias do |name, _|
32
- yield name.absolute!
33
- end
34
- end
35
-
36
- def tsort_each_child(name, &block)
37
- unless name.namespace.empty?
38
- yield name.namespace.to_type_name
39
- end
40
-
41
- case
42
- when name.class?, name.interface?
43
- definitions = []
44
-
45
- case
46
- when name.class?
47
- definitions << builder.build_instance(name)
48
- definitions << builder.build_singleton(name)
49
- when name.interface?
50
- definitions << builder.build_interface(name, env.find_class(name))
51
- end
52
-
53
- definitions.each do |definition|
54
- definition.ancestors.each do |ancestor|
55
- yield ancestor.name
56
-
57
- case ancestor
58
- when Definition::Ancestor::Instance, Definition::Ancestor::ExtensionInstance
59
- ancestor.args.each do |type|
60
- each_type_name type, &block
61
- end
62
- end
63
- end
64
-
65
- unless only_ancestors?
66
- definition.methods.each do |_, method|
67
- method.method_types.each do |method_type|
68
- method_type.type.each_type do |type|
69
- each_type_name type, &block
70
- end
71
- method_type.block&.type&.each_type do |type|
72
- each_type_name type, &block
73
- end
74
- end
75
- end
76
- end
77
- end
78
- when name.alias?
79
- each_type_name builder.expand_alias(name), &block
80
- end
81
- end
82
-
83
- def each_type_name(type, &block)
84
- case type
85
- when Ruby::Signature::Types::Bases::Any
86
- when Ruby::Signature::Types::Bases::Class
87
- when Ruby::Signature::Types::Bases::Instance
88
- when Ruby::Signature::Types::Bases::Self
89
- when Ruby::Signature::Types::Bases::Top
90
- when Ruby::Signature::Types::Bases::Bottom
91
- when Ruby::Signature::Types::Bases::Bool
92
- when Ruby::Signature::Types::Bases::Void
93
- when Ruby::Signature::Types::Bases::Nil
94
- when Ruby::Signature::Types::Variable
95
- when Ruby::Signature::Types::ClassSingleton
96
- yield type.name
97
- when Ruby::Signature::Types::ClassInstance, Ruby::Signature::Types::Interface
98
- yield type.name
99
- type.args.each do |ty|
100
- each_type_name(ty, &block)
101
- end
102
- when Ruby::Signature::Types::Alias
103
- yield type.name
104
- when Ruby::Signature::Types::Union, Ruby::Signature::Types::Intersection, Ruby::Signature::Types::Tuple
105
- type.types.each do |ty|
106
- each_type_name ty, &block
107
- end
108
- when Ruby::Signature::Types::Optional
109
- each_type_name type.type, &block
110
- when Ruby::Signature::Types::Literal
111
- # nop
112
- when Ruby::Signature::Types::Record
113
- type.fields.each_value do |ty|
114
- each_type_name ty, &block
115
- end
116
- when Ruby::Signature::Types::Proc
117
- type.each_type do |ty|
118
- each_type_name ty, &block
119
- end
120
- else
121
- raise "Unexpected type given: #{type}"
122
- end
123
- end
124
- end
125
- end
126
- end