steep 0.11.1 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (299) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +27 -0
  3. data/.gitmodules +3 -0
  4. data/CHANGELOG.md +5 -0
  5. data/README.md +48 -90
  6. data/Rakefile +10 -6
  7. data/Steepfile +1 -0
  8. data/bin/setup +1 -0
  9. data/bin/smoke_runner.rb +9 -14
  10. data/exe/rbs +3 -0
  11. data/exe/ruby-signature +3 -0
  12. data/exe/steep +1 -0
  13. data/lib/steep.rb +32 -26
  14. data/lib/steep/annotation_parser.rb +167 -0
  15. data/lib/steep/ast/annotation/collection.rb +7 -7
  16. data/lib/steep/ast/types.rb +60 -0
  17. data/lib/steep/ast/types/any.rb +1 -1
  18. data/lib/steep/ast/types/factory.rb +535 -0
  19. data/lib/steep/ast/types/name.rb +3 -3
  20. data/lib/steep/ast/types/var.rb +1 -1
  21. data/lib/steep/cli.rb +56 -240
  22. data/lib/steep/drivers/annotations.rb +36 -19
  23. data/lib/steep/drivers/check.rb +55 -91
  24. data/lib/steep/drivers/init.rb +54 -0
  25. data/lib/steep/drivers/langserver.rb +241 -150
  26. data/lib/steep/drivers/print_project.rb +56 -0
  27. data/lib/steep/drivers/signature_error_printer.rb +25 -0
  28. data/lib/steep/drivers/trace_printer.rb +25 -0
  29. data/lib/steep/drivers/utils/driver_helper.rb +26 -0
  30. data/lib/steep/drivers/validate.rb +18 -38
  31. data/lib/steep/drivers/vendor.rb +46 -0
  32. data/lib/steep/drivers/watch.rb +78 -140
  33. data/lib/steep/errors.rb +22 -13
  34. data/lib/steep/interface/interface.rb +91 -0
  35. data/lib/steep/interface/method.rb +0 -4
  36. data/lib/steep/interface/method_type.rb +362 -2
  37. data/lib/steep/interface/substitution.rb +22 -0
  38. data/lib/steep/project.rb +25 -233
  39. data/lib/steep/project/dsl.rb +132 -0
  40. data/lib/steep/project/file.rb +93 -76
  41. data/lib/steep/project/file_loader.rb +63 -0
  42. data/lib/steep/project/options.rb +7 -0
  43. data/lib/steep/project/target.rb +190 -0
  44. data/lib/steep/signature/errors.rb +25 -77
  45. data/lib/steep/signature/validator.rb +122 -0
  46. data/lib/steep/source.rb +12 -7
  47. data/lib/steep/subtyping/check.rb +357 -633
  48. data/lib/steep/subtyping/constraints.rb +2 -2
  49. data/lib/steep/subtyping/trace.rb +23 -0
  50. data/lib/steep/type_construction.rb +509 -455
  51. data/lib/steep/type_inference/constant_env.rb +16 -24
  52. data/lib/steep/type_inference/type_env.rb +26 -18
  53. data/lib/steep/version.rb +1 -1
  54. data/sample/Steepfile +6 -0
  55. data/sample/lib/conference.rb +12 -0
  56. data/sample/sig/conference.rbs +6 -0
  57. data/smoke/alias/Steepfile +4 -0
  58. data/smoke/alias/a.rb +2 -2
  59. data/smoke/alias/{a.rbi → a.rbs} +1 -1
  60. data/smoke/and/Steepfile +4 -0
  61. data/smoke/array/Steepfile +4 -0
  62. data/smoke/array/a.rb +2 -2
  63. data/smoke/array/b.rb +4 -4
  64. data/smoke/array/c.rb +2 -2
  65. data/smoke/block/Steepfile +5 -0
  66. data/smoke/block/{a.rbi → a.rbs} +1 -1
  67. data/smoke/block/{c.rbi → c.rbs} +0 -0
  68. data/smoke/block/d.rb +6 -6
  69. data/smoke/case/Steepfile +4 -0
  70. data/smoke/case/a.rb +4 -3
  71. data/smoke/class/Steepfile +4 -0
  72. data/smoke/class/a.rb +1 -4
  73. data/smoke/class/a.rbs +24 -0
  74. data/smoke/class/h.rb +6 -2
  75. data/smoke/class/{h.rbi → h.rbs} +1 -2
  76. data/smoke/class/i.rb +1 -2
  77. data/smoke/class/i.rbs +9 -0
  78. data/smoke/const/Steepfile +4 -0
  79. data/smoke/dstr/Steepfile +4 -0
  80. data/smoke/ensure/Steepfile +4 -0
  81. data/smoke/ensure/a.rb +1 -1
  82. data/smoke/enumerator/Steepfile +4 -0
  83. data/smoke/enumerator/a.rb +7 -7
  84. data/smoke/enumerator/b.rb +6 -6
  85. data/smoke/extension/Steepfile +4 -0
  86. data/smoke/extension/{a.rbi → a.rbs} +2 -2
  87. data/smoke/extension/{e.rbi → e.rbs} +2 -2
  88. data/smoke/hash/Steepfile +4 -0
  89. data/smoke/hash/{a.rbi → a.rbs} +0 -0
  90. data/smoke/hash/b.rb +2 -2
  91. data/smoke/hash/c.rb +1 -1
  92. data/smoke/hash/e.rbs +3 -0
  93. data/smoke/hash/f.rb +1 -1
  94. data/smoke/hello/Steepfile +4 -0
  95. data/smoke/hello/hello.rbs +7 -0
  96. data/smoke/if/Steepfile +4 -0
  97. data/smoke/implements/Steepfile +4 -0
  98. data/smoke/implements/a.rbs +6 -0
  99. data/smoke/initialize/Steepfile +4 -0
  100. data/smoke/initialize/a.rbs +3 -0
  101. data/smoke/integer/Steepfile +4 -0
  102. data/smoke/integer/a.rb +5 -3
  103. data/smoke/interface/Steepfile +4 -0
  104. data/smoke/interface/{a.rbi → a.rbs} +0 -0
  105. data/smoke/kwbegin/Steepfile +4 -0
  106. data/smoke/lambda/Steepfile +4 -0
  107. data/smoke/lambda/a.rb +9 -2
  108. data/smoke/literal/Steepfile +4 -0
  109. data/smoke/literal/{literal_methods.rbi → literal_methods.rbs} +0 -0
  110. data/smoke/map/Steepfile +4 -0
  111. data/smoke/map/a.rb +1 -1
  112. data/smoke/method/Steepfile +4 -0
  113. data/smoke/method/{a.rbi → a.rbs} +0 -0
  114. data/smoke/method/b.rb +1 -4
  115. data/smoke/method/d.rb +1 -0
  116. data/smoke/method/d.rbs +3 -0
  117. data/smoke/module/Steepfile +4 -0
  118. data/smoke/module/a.rb +1 -1
  119. data/smoke/module/a.rbs +16 -0
  120. data/smoke/module/c.rb +1 -1
  121. data/smoke/regexp/Steepfile +4 -0
  122. data/smoke/regexp/a.rb +2 -2
  123. data/smoke/regexp/b.rb +16 -16
  124. data/smoke/regression/Steepfile +5 -0
  125. data/smoke/regression/array.rb +2 -2
  126. data/smoke/regression/hash.rb +2 -2
  127. data/smoke/regression/poly_new.rb +2 -0
  128. data/smoke/regression/poly_new.rbs +4 -0
  129. data/smoke/regression/set_divide.rb +2 -2
  130. data/smoke/rescue/Steepfile +4 -0
  131. data/smoke/rescue/a.rb +1 -1
  132. data/smoke/self/Steepfile +4 -0
  133. data/smoke/self/a.rbs +4 -0
  134. data/smoke/skip/Steepfile +4 -0
  135. data/smoke/stdout/Steepfile +4 -0
  136. data/smoke/stdout/{a.rbi → a.rbs} +1 -1
  137. data/smoke/super/Steepfile +4 -0
  138. data/smoke/super/a.rbs +10 -0
  139. data/smoke/type_case/Steepfile +4 -0
  140. data/smoke/type_case/a.rb +1 -1
  141. data/smoke/yield/Steepfile +4 -0
  142. data/smoke/yield/a.rb +2 -2
  143. data/steep.gemspec +14 -7
  144. data/vendor/ruby-signature/.github/workflows/ruby.yml +27 -0
  145. data/vendor/ruby-signature/.gitignore +12 -0
  146. data/vendor/ruby-signature/.rubocop.yml +15 -0
  147. data/vendor/ruby-signature/BSDL +22 -0
  148. data/vendor/ruby-signature/COPYING +56 -0
  149. data/vendor/ruby-signature/Gemfile +6 -0
  150. data/vendor/ruby-signature/README.md +93 -0
  151. data/vendor/ruby-signature/Rakefile +66 -0
  152. data/vendor/ruby-signature/bin/annotate-with-rdoc +156 -0
  153. data/vendor/ruby-signature/bin/console +14 -0
  154. data/vendor/ruby-signature/bin/query-rdoc +103 -0
  155. data/vendor/ruby-signature/bin/setup +10 -0
  156. data/vendor/ruby-signature/bin/sort +88 -0
  157. data/vendor/ruby-signature/bin/test_runner.rb +17 -0
  158. data/vendor/ruby-signature/docs/CONTRIBUTING.md +97 -0
  159. data/vendor/ruby-signature/docs/sigs.md +148 -0
  160. data/vendor/ruby-signature/docs/stdlib.md +152 -0
  161. data/vendor/ruby-signature/docs/syntax.md +528 -0
  162. data/vendor/ruby-signature/exe/rbs +3 -0
  163. data/vendor/ruby-signature/exe/ruby-signature +7 -0
  164. data/vendor/ruby-signature/lib/ruby/signature.rb +64 -0
  165. data/vendor/ruby-signature/lib/ruby/signature/ast/annotation.rb +29 -0
  166. data/vendor/ruby-signature/lib/ruby/signature/ast/comment.rb +29 -0
  167. data/vendor/ruby-signature/lib/ruby/signature/ast/declarations.rb +391 -0
  168. data/vendor/ruby-signature/lib/ruby/signature/ast/members.rb +364 -0
  169. data/vendor/ruby-signature/lib/ruby/signature/buffer.rb +52 -0
  170. data/vendor/ruby-signature/lib/ruby/signature/builtin_names.rb +54 -0
  171. data/vendor/ruby-signature/lib/ruby/signature/cli.rb +534 -0
  172. data/vendor/ruby-signature/lib/ruby/signature/constant.rb +28 -0
  173. data/vendor/ruby-signature/lib/ruby/signature/constant_table.rb +152 -0
  174. data/vendor/ruby-signature/lib/ruby/signature/definition.rb +172 -0
  175. data/vendor/ruby-signature/lib/ruby/signature/definition_builder.rb +921 -0
  176. data/vendor/ruby-signature/lib/ruby/signature/environment.rb +283 -0
  177. data/vendor/ruby-signature/lib/ruby/signature/environment_loader.rb +138 -0
  178. data/vendor/ruby-signature/lib/ruby/signature/environment_walker.rb +126 -0
  179. data/vendor/ruby-signature/lib/ruby/signature/errors.rb +189 -0
  180. data/vendor/ruby-signature/lib/ruby/signature/location.rb +104 -0
  181. data/vendor/ruby-signature/lib/ruby/signature/method_type.rb +125 -0
  182. data/vendor/ruby-signature/lib/ruby/signature/namespace.rb +93 -0
  183. data/vendor/ruby-signature/lib/ruby/signature/parser.y +1343 -0
  184. data/vendor/ruby-signature/lib/ruby/signature/prototype/rb.rb +441 -0
  185. data/vendor/ruby-signature/lib/ruby/signature/prototype/rbi.rb +579 -0
  186. data/vendor/ruby-signature/lib/ruby/signature/prototype/runtime.rb +383 -0
  187. data/vendor/ruby-signature/lib/ruby/signature/substitution.rb +48 -0
  188. data/vendor/ruby-signature/lib/ruby/signature/test.rb +28 -0
  189. data/vendor/ruby-signature/lib/ruby/signature/test/errors.rb +63 -0
  190. data/vendor/ruby-signature/lib/ruby/signature/test/hook.rb +290 -0
  191. data/vendor/ruby-signature/lib/ruby/signature/test/setup.rb +58 -0
  192. data/vendor/ruby-signature/lib/ruby/signature/test/spy.rb +324 -0
  193. data/vendor/ruby-signature/lib/ruby/signature/test/test_helper.rb +185 -0
  194. data/vendor/ruby-signature/lib/ruby/signature/test/type_check.rb +256 -0
  195. data/vendor/ruby-signature/lib/ruby/signature/type_name.rb +72 -0
  196. data/vendor/ruby-signature/lib/ruby/signature/types.rb +932 -0
  197. data/vendor/ruby-signature/lib/ruby/signature/variance_calculator.rb +140 -0
  198. data/vendor/ruby-signature/lib/ruby/signature/vendorer.rb +49 -0
  199. data/vendor/ruby-signature/lib/ruby/signature/version.rb +5 -0
  200. data/vendor/ruby-signature/lib/ruby/signature/writer.rb +271 -0
  201. data/vendor/ruby-signature/ruby-signature.gemspec +45 -0
  202. data/vendor/ruby-signature/stdlib/abbrev/abbrev.rbs +3 -0
  203. data/vendor/ruby-signature/stdlib/base64/base64.rbs +15 -0
  204. data/vendor/ruby-signature/stdlib/builtin/array.rbs +1997 -0
  205. data/vendor/ruby-signature/stdlib/builtin/basic_object.rbs +280 -0
  206. data/vendor/ruby-signature/stdlib/builtin/binding.rbs +177 -0
  207. data/vendor/ruby-signature/stdlib/builtin/builtin.rbs +35 -0
  208. data/vendor/ruby-signature/stdlib/builtin/class.rbs +145 -0
  209. data/vendor/ruby-signature/stdlib/builtin/comparable.rbs +116 -0
  210. data/vendor/ruby-signature/stdlib/builtin/complex.rbs +400 -0
  211. data/vendor/ruby-signature/stdlib/builtin/constants.rbs +37 -0
  212. data/vendor/ruby-signature/stdlib/builtin/data.rbs +5 -0
  213. data/vendor/ruby-signature/stdlib/builtin/deprecated.rbs +2 -0
  214. data/vendor/ruby-signature/stdlib/builtin/dir.rbs +419 -0
  215. data/vendor/ruby-signature/stdlib/builtin/encoding.rbs +606 -0
  216. data/vendor/ruby-signature/stdlib/builtin/enumerable.rbs +404 -0
  217. data/vendor/ruby-signature/stdlib/builtin/enumerator.rbs +260 -0
  218. data/vendor/ruby-signature/stdlib/builtin/errno.rbs +781 -0
  219. data/vendor/ruby-signature/stdlib/builtin/errors.rbs +582 -0
  220. data/vendor/ruby-signature/stdlib/builtin/exception.rbs +193 -0
  221. data/vendor/ruby-signature/stdlib/builtin/false_class.rbs +40 -0
  222. data/vendor/ruby-signature/stdlib/builtin/fiber.rbs +68 -0
  223. data/vendor/ruby-signature/stdlib/builtin/fiber_error.rbs +12 -0
  224. data/vendor/ruby-signature/stdlib/builtin/file.rbs +476 -0
  225. data/vendor/ruby-signature/stdlib/builtin/file_test.rbs +59 -0
  226. data/vendor/ruby-signature/stdlib/builtin/float.rbs +696 -0
  227. data/vendor/ruby-signature/stdlib/builtin/gc.rbs +121 -0
  228. data/vendor/ruby-signature/stdlib/builtin/hash.rbs +1029 -0
  229. data/vendor/ruby-signature/stdlib/builtin/integer.rbs +710 -0
  230. data/vendor/ruby-signature/stdlib/builtin/io.rbs +683 -0
  231. data/vendor/ruby-signature/stdlib/builtin/kernel.rbs +574 -0
  232. data/vendor/ruby-signature/stdlib/builtin/marshal.rbs +135 -0
  233. data/vendor/ruby-signature/stdlib/builtin/match_data.rbs +141 -0
  234. data/vendor/ruby-signature/stdlib/builtin/math.rbs +66 -0
  235. data/vendor/ruby-signature/stdlib/builtin/method.rbs +182 -0
  236. data/vendor/ruby-signature/stdlib/builtin/module.rbs +248 -0
  237. data/vendor/ruby-signature/stdlib/builtin/nil_class.rbs +82 -0
  238. data/vendor/ruby-signature/stdlib/builtin/numeric.rbs +409 -0
  239. data/vendor/ruby-signature/stdlib/builtin/object.rbs +824 -0
  240. data/vendor/ruby-signature/stdlib/builtin/proc.rbs +426 -0
  241. data/vendor/ruby-signature/stdlib/builtin/process.rbs +354 -0
  242. data/vendor/ruby-signature/stdlib/builtin/random.rbs +93 -0
  243. data/vendor/ruby-signature/stdlib/builtin/range.rbs +226 -0
  244. data/vendor/ruby-signature/stdlib/builtin/rational.rbs +424 -0
  245. data/vendor/ruby-signature/stdlib/builtin/rb_config.rbs +10 -0
  246. data/vendor/ruby-signature/stdlib/builtin/regexp.rbs +131 -0
  247. data/vendor/ruby-signature/stdlib/builtin/ruby_vm.rbs +14 -0
  248. data/vendor/ruby-signature/stdlib/builtin/signal.rbs +55 -0
  249. data/vendor/ruby-signature/stdlib/builtin/string.rbs +770 -0
  250. data/vendor/ruby-signature/stdlib/builtin/string_io.rbs +13 -0
  251. data/vendor/ruby-signature/stdlib/builtin/struct.rbs +40 -0
  252. data/vendor/ruby-signature/stdlib/builtin/symbol.rbs +230 -0
  253. data/vendor/ruby-signature/stdlib/builtin/thread.rbs +1112 -0
  254. data/vendor/ruby-signature/stdlib/builtin/thread_group.rbs +23 -0
  255. data/vendor/ruby-signature/stdlib/builtin/time.rbs +739 -0
  256. data/vendor/ruby-signature/stdlib/builtin/trace_point.rbs +91 -0
  257. data/vendor/ruby-signature/stdlib/builtin/true_class.rbs +46 -0
  258. data/vendor/ruby-signature/stdlib/builtin/unbound_method.rbs +159 -0
  259. data/vendor/ruby-signature/stdlib/builtin/warning.rbs +17 -0
  260. data/vendor/ruby-signature/stdlib/erb/erb.rbs +18 -0
  261. data/vendor/ruby-signature/stdlib/find/find.rbs +44 -0
  262. data/vendor/ruby-signature/stdlib/pathname/pathname.rbs +21 -0
  263. data/vendor/ruby-signature/stdlib/prime/integer-extension.rbs +23 -0
  264. data/vendor/ruby-signature/stdlib/prime/prime.rbs +188 -0
  265. data/vendor/ruby-signature/stdlib/securerandom/securerandom.rbs +9 -0
  266. data/vendor/ruby-signature/stdlib/set/set.rbs +77 -0
  267. data/vendor/ruby-signature/stdlib/tmpdir/tmpdir.rbs +53 -0
  268. metadata +244 -54
  269. data/.travis.yml +0 -7
  270. data/lib/steep/ast/signature/alias.rb +0 -19
  271. data/lib/steep/ast/signature/class.rb +0 -33
  272. data/lib/steep/ast/signature/const.rb +0 -17
  273. data/lib/steep/ast/signature/env.rb +0 -138
  274. data/lib/steep/ast/signature/extension.rb +0 -21
  275. data/lib/steep/ast/signature/gvar.rb +0 -17
  276. data/lib/steep/ast/signature/interface.rb +0 -31
  277. data/lib/steep/ast/signature/members.rb +0 -115
  278. data/lib/steep/ast/signature/module.rb +0 -21
  279. data/lib/steep/drivers/print_interface.rb +0 -94
  280. data/lib/steep/drivers/scaffold.rb +0 -321
  281. data/lib/steep/drivers/utils/each_signature.rb +0 -31
  282. data/lib/steep/interface/abstract.rb +0 -68
  283. data/lib/steep/interface/builder.rb +0 -637
  284. data/lib/steep/interface/instantiated.rb +0 -163
  285. data/lib/steep/interface/ivar_chain.rb +0 -26
  286. data/lib/steep/parser.y +0 -1278
  287. data/lib/steep/project/listener.rb +0 -53
  288. data/smoke/class/a.rbi +0 -24
  289. data/smoke/class/d.rb +0 -9
  290. data/smoke/class/e.rb +0 -12
  291. data/smoke/class/i.rbi +0 -9
  292. data/smoke/hash/e.rbi +0 -3
  293. data/smoke/hello/hello.rbi +0 -7
  294. data/smoke/implements/a.rbi +0 -6
  295. data/smoke/initialize/a.rbi +0 -3
  296. data/smoke/module/a.rbi +0 -16
  297. data/smoke/self/a.rbi +0 -4
  298. data/smoke/super/a.rbi +0 -10
  299. data/stdlib/builtin.rbi +0 -787
@@ -0,0 +1,283 @@
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
@@ -0,0 +1,138 @@
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
@@ -0,0 +1,126 @@
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