rbs 3.10.4 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (267) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +14 -14
  3. data/.github/workflows/bundle-update.yml +60 -0
  4. data/.github/workflows/c-check.yml +7 -5
  5. data/.github/workflows/comments.yml +2 -2
  6. data/.github/workflows/dependabot.yml +2 -2
  7. data/.github/workflows/ruby.yml +16 -26
  8. data/.github/workflows/rust.yml +95 -0
  9. data/.github/workflows/typecheck.yml +1 -1
  10. data/.github/workflows/windows.yml +2 -2
  11. data/.rubocop.yml +2 -2
  12. data/.vscode/extensions.json +5 -0
  13. data/.vscode/settings.json +19 -0
  14. data/CHANGELOG.md +202 -2
  15. data/Rakefile +9 -23
  16. data/Steepfile +2 -0
  17. data/config.yml +457 -13
  18. data/core/array.rbs +218 -188
  19. data/core/basic_object.rbs +9 -8
  20. data/core/binding.rbs +0 -2
  21. data/core/builtin.rbs +2 -2
  22. data/core/class.rbs +6 -5
  23. data/core/comparable.rbs +45 -31
  24. data/core/complex.rbs +66 -55
  25. data/core/dir.rbs +57 -45
  26. data/core/encoding.rbs +6 -6
  27. data/core/enumerable.rbs +105 -91
  28. data/core/enumerator/arithmetic_sequence.rbs +70 -0
  29. data/core/enumerator.rbs +24 -3
  30. data/core/errno.rbs +3 -2
  31. data/core/errors.rbs +31 -29
  32. data/core/exception.rbs +12 -12
  33. data/core/fiber.rbs +47 -36
  34. data/core/file.rbs +242 -169
  35. data/core/file_test.rbs +2 -2
  36. data/core/float.rbs +42 -68
  37. data/core/gc.rbs +78 -70
  38. data/core/hash.rbs +70 -60
  39. data/core/integer.rbs +32 -75
  40. data/core/io/buffer.rbs +36 -36
  41. data/core/io/wait.rbs +7 -7
  42. data/core/io.rbs +192 -146
  43. data/core/kernel.rbs +198 -147
  44. data/core/marshal.rbs +3 -3
  45. data/core/match_data.rbs +14 -12
  46. data/core/math.rbs +69 -67
  47. data/core/method.rbs +6 -8
  48. data/core/module.rbs +148 -88
  49. data/core/nil_class.rbs +4 -3
  50. data/core/numeric.rbs +53 -50
  51. data/core/object.rbs +6 -8
  52. data/core/object_space.rbs +11 -10
  53. data/core/pathname.rbs +131 -81
  54. data/core/proc.rbs +65 -34
  55. data/core/process.rbs +221 -201
  56. data/core/ractor.rbs +15 -11
  57. data/core/random.rbs +21 -3
  58. data/core/range.rbs +152 -49
  59. data/core/rational.rbs +5 -56
  60. data/core/rbs/unnamed/argf.rbs +58 -51
  61. data/core/rbs/unnamed/env_class.rbs +18 -13
  62. data/core/rbs/unnamed/main_class.rbs +123 -0
  63. data/core/rbs/unnamed/random.rbs +7 -116
  64. data/core/regexp.rbs +236 -197
  65. data/core/ruby.rbs +1 -1
  66. data/core/ruby_vm.rbs +32 -30
  67. data/core/rubygems/config_file.rbs +5 -5
  68. data/core/rubygems/errors.rbs +1 -1
  69. data/core/rubygems/requirement.rbs +5 -5
  70. data/core/rubygems/rubygems.rbs +5 -3
  71. data/core/set.rbs +17 -16
  72. data/core/signal.rbs +2 -2
  73. data/core/string.rbs +318 -298
  74. data/core/struct.rbs +26 -25
  75. data/core/symbol.rbs +25 -24
  76. data/core/thread.rbs +40 -41
  77. data/core/time.rbs +47 -42
  78. data/core/trace_point.rbs +34 -31
  79. data/core/true_class.rbs +2 -2
  80. data/core/unbound_method.rbs +10 -10
  81. data/core/warning.rbs +7 -7
  82. data/docs/collection.md +1 -1
  83. data/docs/config.md +171 -0
  84. data/docs/inline.md +576 -0
  85. data/docs/syntax.md +46 -16
  86. data/docs/type_fingerprint.md +21 -0
  87. data/exe/rbs +1 -1
  88. data/ext/rbs_extension/ast_translation.c +595 -98
  89. data/ext/rbs_extension/class_constants.c +30 -0
  90. data/ext/rbs_extension/class_constants.h +15 -0
  91. data/ext/rbs_extension/legacy_location.c +30 -53
  92. data/ext/rbs_extension/legacy_location.h +37 -0
  93. data/ext/rbs_extension/main.c +125 -24
  94. data/include/rbs/ast.h +485 -150
  95. data/include/rbs/lexer.h +11 -4
  96. data/include/rbs/location.h +25 -44
  97. data/include/rbs/parser.h +20 -2
  98. data/include/rbs/util/rbs_constant_pool.h +0 -3
  99. data/include/rbs.h +8 -0
  100. data/lib/rbs/ast/annotation.rb +1 -1
  101. data/lib/rbs/ast/comment.rb +1 -1
  102. data/lib/rbs/ast/declarations.rb +10 -10
  103. data/lib/rbs/ast/members.rb +14 -14
  104. data/lib/rbs/ast/ruby/annotations.rb +409 -0
  105. data/lib/rbs/ast/ruby/comment_block.rb +245 -0
  106. data/lib/rbs/ast/ruby/declarations.rb +281 -0
  107. data/lib/rbs/ast/ruby/helpers/constant_helper.rb +28 -0
  108. data/lib/rbs/ast/ruby/helpers/location_helper.rb +15 -0
  109. data/lib/rbs/ast/ruby/members.rb +723 -0
  110. data/lib/rbs/ast/type_param.rb +24 -4
  111. data/lib/rbs/buffer.rb +105 -20
  112. data/lib/rbs/cli/diff.rb +16 -15
  113. data/lib/rbs/cli/validate.rb +62 -125
  114. data/lib/rbs/cli.rb +55 -23
  115. data/lib/rbs/collection/config/lockfile_generator.rb +8 -4
  116. data/lib/rbs/collection/sources/git.rb +1 -0
  117. data/lib/rbs/collection.rb +0 -1
  118. data/lib/rbs/definition.rb +6 -1
  119. data/lib/rbs/definition_builder/ancestor_builder.rb +119 -63
  120. data/lib/rbs/definition_builder/method_builder.rb +65 -30
  121. data/lib/rbs/definition_builder.rb +177 -20
  122. data/lib/rbs/diff.rb +7 -1
  123. data/lib/rbs/environment/class_entry.rb +69 -0
  124. data/lib/rbs/environment/module_entry.rb +66 -0
  125. data/lib/rbs/environment.rb +338 -155
  126. data/lib/rbs/environment_loader.rb +2 -2
  127. data/lib/rbs/errors.rb +30 -20
  128. data/lib/rbs/inline_parser/comment_association.rb +117 -0
  129. data/lib/rbs/inline_parser.rb +542 -0
  130. data/lib/rbs/location_aux.rb +36 -4
  131. data/lib/rbs/locator.rb +5 -1
  132. data/lib/rbs/method_type.rb +5 -3
  133. data/lib/rbs/namespace.rb +0 -7
  134. data/lib/rbs/parser_aux.rb +31 -8
  135. data/lib/rbs/prototype/helpers.rb +57 -0
  136. data/lib/rbs/prototype/rb.rb +3 -28
  137. data/lib/rbs/prototype/rbi.rb +3 -20
  138. data/lib/rbs/prototype/runtime.rb +10 -2
  139. data/lib/rbs/resolver/type_name_resolver.rb +0 -8
  140. data/lib/rbs/source.rb +99 -0
  141. data/lib/rbs/subtractor.rb +4 -3
  142. data/lib/rbs/test/type_check.rb +5 -2
  143. data/lib/rbs/type_name.rb +1 -8
  144. data/lib/rbs/types.rb +88 -78
  145. data/lib/rbs/unit_test/convertibles.rb +1 -0
  146. data/lib/rbs/unit_test/type_assertions.rb +35 -8
  147. data/lib/rbs/validator.rb +2 -2
  148. data/lib/rbs/version.rb +1 -1
  149. data/lib/rbs.rb +12 -1
  150. data/rbs.gemspec +3 -2
  151. data/rust/.gitignore +1 -0
  152. data/rust/Cargo.lock +378 -0
  153. data/rust/Cargo.toml +7 -0
  154. data/rust/ruby-rbs/Cargo.toml +22 -0
  155. data/rust/ruby-rbs/build.rs +764 -0
  156. data/rust/ruby-rbs/examples/locations.rs +60 -0
  157. data/rust/ruby-rbs/src/lib.rs +1 -0
  158. data/rust/ruby-rbs/src/node/mod.rs +742 -0
  159. data/rust/ruby-rbs/tests/sanity.rs +47 -0
  160. data/rust/ruby-rbs/vendor/rbs/config.yml +1 -0
  161. data/rust/ruby-rbs-sys/Cargo.toml +23 -0
  162. data/rust/ruby-rbs-sys/build.rs +204 -0
  163. data/rust/ruby-rbs-sys/src/lib.rs +50 -0
  164. data/rust/ruby-rbs-sys/vendor/rbs/include +1 -0
  165. data/rust/ruby-rbs-sys/vendor/rbs/src +1 -0
  166. data/rust/ruby-rbs-sys/wrapper.h +1 -0
  167. data/schema/typeParam.json +17 -1
  168. data/sig/ancestor_builder.rbs +1 -1
  169. data/sig/ast/ruby/annotations.rbs +421 -0
  170. data/sig/ast/ruby/comment_block.rbs +127 -0
  171. data/sig/ast/ruby/declarations.rbs +158 -0
  172. data/sig/ast/ruby/helpers/constant_helper.rbs +11 -0
  173. data/sig/ast/ruby/helpers/location_helper.rbs +15 -0
  174. data/sig/ast/ruby/members.rbs +178 -0
  175. data/sig/buffer.rbs +63 -5
  176. data/sig/cli/diff.rbs +5 -11
  177. data/sig/cli/validate.rbs +12 -8
  178. data/sig/cli.rbs +18 -18
  179. data/sig/definition.rbs +6 -0
  180. data/sig/definition_builder.rbs +3 -1
  181. data/sig/environment/class_entry.rbs +50 -0
  182. data/sig/environment/module_entry.rbs +50 -0
  183. data/sig/environment.rbs +37 -81
  184. data/sig/errors.rbs +26 -20
  185. data/sig/inline_parser/comment_association.rbs +71 -0
  186. data/sig/inline_parser.rbs +124 -0
  187. data/sig/location.rbs +32 -7
  188. data/sig/locator.rbs +0 -2
  189. data/sig/method_builder.rbs +9 -4
  190. data/sig/namespace.rbs +0 -5
  191. data/sig/parser.rbs +47 -13
  192. data/sig/prototype/helpers.rbs +2 -0
  193. data/sig/resolver/type_name_resolver.rbs +0 -3
  194. data/sig/source.rbs +48 -0
  195. data/sig/type_param.rbs +13 -8
  196. data/sig/typename.rbs +0 -5
  197. data/sig/types.rbs +6 -7
  198. data/sig/unit_test/spy.rbs +0 -8
  199. data/sig/unit_test/type_assertions.rbs +11 -0
  200. data/src/ast.c +491 -143
  201. data/src/lexer.c +1552 -1314
  202. data/src/lexer.re +7 -0
  203. data/src/lexstate.c +8 -1
  204. data/src/location.c +8 -48
  205. data/src/parser.c +1107 -409
  206. data/src/util/rbs_constant_pool.c +0 -4
  207. data/stdlib/bigdecimal/0/big_decimal.rbs +16 -16
  208. data/stdlib/cgi-escape/0/escape.rbs +4 -4
  209. data/stdlib/coverage/0/coverage.rbs +4 -3
  210. data/stdlib/date/0/date.rbs +33 -28
  211. data/stdlib/date/0/date_time.rbs +24 -23
  212. data/stdlib/did_you_mean/0/did_you_mean.rbs +17 -16
  213. data/stdlib/digest/0/digest.rbs +110 -0
  214. data/stdlib/erb/0/erb.rbs +64 -53
  215. data/stdlib/etc/0/etc.rbs +55 -50
  216. data/stdlib/fileutils/0/fileutils.rbs +140 -126
  217. data/stdlib/forwardable/0/forwardable.rbs +10 -10
  218. data/stdlib/io-console/0/io-console.rbs +2 -2
  219. data/stdlib/json/0/json.rbs +158 -131
  220. data/stdlib/monitor/0/monitor.rbs +3 -3
  221. data/stdlib/net-http/0/net-http.rbs +159 -134
  222. data/stdlib/objspace/0/objspace.rbs +8 -30
  223. data/stdlib/open-uri/0/open-uri.rbs +8 -8
  224. data/stdlib/open3/0/open3.rbs +469 -10
  225. data/stdlib/openssl/0/openssl.rbs +144 -129
  226. data/stdlib/optparse/0/optparse.rbs +23 -14
  227. data/stdlib/pathname/0/pathname.rbs +2 -2
  228. data/stdlib/pp/0/pp.rbs +9 -8
  229. data/stdlib/prettyprint/0/prettyprint.rbs +7 -7
  230. data/stdlib/pstore/0/pstore.rbs +35 -30
  231. data/stdlib/psych/0/psych.rbs +62 -9
  232. data/stdlib/psych/0/store.rbs +2 -4
  233. data/stdlib/pty/0/pty.rbs +9 -6
  234. data/stdlib/random-formatter/0/random-formatter.rbs +277 -0
  235. data/stdlib/rdoc/0/code_object.rbs +2 -1
  236. data/stdlib/rdoc/0/parser.rbs +1 -1
  237. data/stdlib/rdoc/0/store.rbs +1 -1
  238. data/stdlib/ripper/0/ripper.rbs +20 -17
  239. data/stdlib/securerandom/0/manifest.yaml +2 -0
  240. data/stdlib/securerandom/0/securerandom.rbs +7 -20
  241. data/stdlib/shellwords/0/shellwords.rbs +2 -2
  242. data/stdlib/socket/0/addrinfo.rbs +9 -9
  243. data/stdlib/socket/0/basic_socket.rbs +3 -3
  244. data/stdlib/socket/0/ip_socket.rbs +10 -8
  245. data/stdlib/socket/0/socket.rbs +10 -9
  246. data/stdlib/socket/0/tcp_server.rbs +1 -1
  247. data/stdlib/socket/0/tcp_socket.rbs +1 -1
  248. data/stdlib/socket/0/udp_socket.rbs +1 -1
  249. data/stdlib/socket/0/unix_server.rbs +1 -1
  250. data/stdlib/stringio/0/stringio.rbs +55 -54
  251. data/stdlib/strscan/0/string_scanner.rbs +46 -44
  252. data/stdlib/tempfile/0/tempfile.rbs +24 -20
  253. data/stdlib/time/0/time.rbs +7 -5
  254. data/stdlib/tsort/0/tsort.rbs +7 -6
  255. data/stdlib/uri/0/common.rbs +31 -18
  256. data/stdlib/uri/0/file.rbs +2 -2
  257. data/stdlib/uri/0/generic.rbs +9 -2
  258. data/stdlib/uri/0/http.rbs +2 -2
  259. data/stdlib/uri/0/ldap.rbs +2 -2
  260. data/stdlib/uri/0/mailto.rbs +3 -3
  261. data/stdlib/uri/0/rfc2396_parser.rbs +6 -5
  262. data/stdlib/zlib/0/deflate.rbs +4 -3
  263. data/stdlib/zlib/0/gzip_reader.rbs +6 -6
  264. data/stdlib/zlib/0/gzip_writer.rbs +14 -12
  265. data/stdlib/zlib/0/inflate.rbs +1 -1
  266. data/stdlib/zlib/0/need_dict.rbs +1 -1
  267. metadata +66 -3
@@ -3,12 +3,13 @@
3
3
  module RBS
4
4
  module AST
5
5
  class TypeParam
6
- attr_reader :name, :variance, :location, :upper_bound_type, :default_type
6
+ attr_reader :name, :variance, :location, :upper_bound_type, :lower_bound_type, :default_type
7
7
 
8
- def initialize(name:, variance:, upper_bound:, location:, default_type: nil, unchecked: false)
8
+ def initialize(name:, variance:, upper_bound:, lower_bound:, location:, default_type: nil, unchecked: false)
9
9
  @name = name
10
10
  @variance = variance
11
11
  @upper_bound_type = upper_bound
12
+ @lower_bound_type = lower_bound
12
13
  @location = location
13
14
  @default_type = default_type
14
15
  @unchecked = unchecked
@@ -21,6 +22,13 @@ module RBS
21
22
  end
22
23
  end
23
24
 
25
+ def lower_bound
26
+ case lower_bound_type
27
+ when Types::ClassInstance, Types::ClassSingleton, Types::Interface
28
+ lower_bound_type
29
+ end
30
+ end
31
+
24
32
  def unchecked!(value = true)
25
33
  @unchecked = value ? true : false
26
34
  self
@@ -35,6 +43,7 @@ module RBS
35
43
  other.name == name &&
36
44
  other.variance == variance &&
37
45
  other.upper_bound_type == upper_bound_type &&
46
+ other.lower_bound_type == lower_bound_type &&
38
47
  other.default_type == default_type &&
39
48
  other.unchecked? == unchecked?
40
49
  end
@@ -42,7 +51,7 @@ module RBS
42
51
  alias eql? ==
43
52
 
44
53
  def hash
45
- self.class.hash ^ name.hash ^ variance.hash ^ upper_bound_type.hash ^ unchecked?.hash ^ default_type.hash
54
+ self.class.hash ^ name.hash ^ variance.hash ^ upper_bound_type.hash ^ lower_bound_type.hash ^ unchecked?.hash ^ default_type.hash
46
55
  end
47
56
 
48
57
  def to_json(state = JSON::State.new)
@@ -52,6 +61,7 @@ module RBS
52
61
  unchecked: unchecked?,
53
62
  location: location,
54
63
  upper_bound: upper_bound_type,
64
+ lower_bound: lower_bound_type,
55
65
  default_type: default_type
56
66
  }.to_json(state)
57
67
  end
@@ -61,6 +71,10 @@ module RBS
61
71
  _upper_bound_type = yield(b)
62
72
  end
63
73
 
74
+ if b = lower_bound_type
75
+ _lower_bound_type = yield(b)
76
+ end
77
+
64
78
  if dt = default_type
65
79
  _default_type = yield(dt)
66
80
  end
@@ -69,6 +83,7 @@ module RBS
69
83
  name: name,
70
84
  variance: variance,
71
85
  upper_bound: _upper_bound_type,
86
+ lower_bound: _lower_bound_type,
72
87
  location: location,
73
88
  default_type: _default_type
74
89
  ).unchecked!(unchecked?)
@@ -108,6 +123,7 @@ module RBS
108
123
  name: new_name,
109
124
  variance: param.variance,
110
125
  upper_bound: param.upper_bound_type&.map_type {|type| type.sub(subst) },
126
+ lower_bound: param.lower_bound_type&.map_type {|type| type.sub(subst) },
111
127
  location: param.location,
112
128
  default_type: param.default_type&.map_type {|type| type.sub(subst) }
113
129
  ).unchecked!(param.unchecked?)
@@ -136,6 +152,10 @@ module RBS
136
152
  s << " < #{type}"
137
153
  end
138
154
 
155
+ if type = lower_bound_type
156
+ s << " > #{type}"
157
+ end
158
+
139
159
  if dt = default_type
140
160
  s << " = #{dt}"
141
161
  end
@@ -176,7 +196,7 @@ module RBS
176
196
  end
177
197
 
178
198
  def self.normalize_args(params, args)
179
- app = application(params, args) or return []
199
+ app = application(params, args) or return args
180
200
 
181
201
  min_count = params.count { _1.default_type.nil? }
182
202
  unless min_count <= args.size && args.size <= params.size
data/lib/rbs/buffer.rb CHANGED
@@ -4,39 +4,58 @@ module RBS
4
4
  class Buffer
5
5
  attr_reader :name
6
6
  attr_reader :content
7
+ attr_reader :parent
7
8
 
8
- def initialize(name:, content:)
9
- @name = name
10
- @content = content
9
+ def initialize(name: nil, content:, parent: nil)
10
+ case
11
+ when name && content
12
+ @name = name
13
+ @content = content
14
+ @parent = nil
15
+ when parent && content
16
+ @name = parent[0].name
17
+ @content = content
18
+ @parent = parent
19
+ end
11
20
  end
12
21
 
13
22
  def lines
14
- @lines ||= content.lines
23
+ ranges.map { self.content[_1] || raise } #$ String
24
+ end
25
+
26
+ def line_count
27
+ ranges.size
15
28
  end
16
29
 
17
30
  def ranges
18
- @ranges ||=
19
- begin
20
- @ranges = []
31
+ @ranges ||= begin
32
+ if content.empty?
33
+ ranges = [0...0] #: Array[Range[Integer]]
34
+ lines = [""]
35
+ else
36
+ lines = content.lines
37
+ lines << "" if content.end_with?("\n")
38
+
39
+ ranges = [] #: Array[Range[Integer]]
21
40
  offset = 0
41
+
22
42
  lines.each do |line|
23
- size = line.size
24
- range = offset...(offset+size)
25
- @ranges << range
26
- offset += size
27
- end
43
+ size0 = line.size
44
+ line = line.chomp
45
+ range = offset...(offset+line.size)
46
+ ranges << range
28
47
 
29
- if !content.end_with?("\n") && content.size > 0
30
- @ranges[-1] = @ranges[-1].begin...(@ranges[-1].end+1)
48
+ offset += size0
31
49
  end
32
-
33
- @ranges
34
50
  end
51
+
52
+ ranges
53
+ end
35
54
  end
36
55
 
37
56
  def pos_to_loc(pos)
38
57
  index = ranges.bsearch_index do |range|
39
- pos < range.end ? true : false
58
+ pos <= range.end ? true : false
40
59
  end
41
60
 
42
61
  if index
@@ -49,7 +68,7 @@ module RBS
49
68
  def loc_to_pos(loc)
50
69
  line, column = loc
51
70
 
52
- if range = ranges[line - 1]
71
+ if range = ranges.fetch(line - 1, nil)
53
72
  range.begin + column
54
73
  else
55
74
  last_position
@@ -57,11 +76,77 @@ module RBS
57
76
  end
58
77
 
59
78
  def last_position
60
- content.size
79
+ if ranges.empty?
80
+ 0
81
+ else
82
+ ranges[-1].end
83
+ end
61
84
  end
62
85
 
63
86
  def inspect
64
- "#<RBS::Buffer:#{__id__} @name=#{name}, @content=#{content.bytesize} bytes, @lines=#{lines.size} lines,>"
87
+ "#<RBS::Buffer:#{__id__} @name=#{name}, @content=#{content.bytesize} bytes, @lines=#{ranges.size} lines,>"
88
+ end
89
+
90
+ def rbs_location(location, loc2=nil)
91
+ if loc2
92
+ Location.new(self.top_buffer, location.start_character_offset, loc2.end_character_offset)
93
+ else
94
+ Location.new(self.top_buffer, location.start_character_offset, location.end_character_offset)
95
+ end
96
+ end
97
+
98
+ def sub_buffer(lines:)
99
+ buf = +""
100
+ lines.each_with_index do |range, index|
101
+ start_pos = range.begin
102
+ end_pos = range.end
103
+ slice = content[start_pos...end_pos] or raise
104
+ if slice.include?("\n")
105
+ raise "Line #{index + 1} cannot contain newline character."
106
+ end
107
+ buf << slice
108
+ buf << "\n"
109
+ end
110
+
111
+ buf.chomp!
112
+
113
+ Buffer.new(content: buf, parent: [self, lines])
114
+ end
115
+
116
+ def parent_buffer
117
+ if parent
118
+ parent[0]
119
+ end
120
+ end
121
+
122
+ def parent_position(position)
123
+ parent or raise "#parent_position is unavailable with buffer without parent"
124
+ return nil unless position <= last_position
125
+
126
+ line, column = pos_to_loc(position)
127
+ parent_range = parent[1][line - 1]
128
+ parent_range.begin + column
129
+ end
130
+
131
+ def absolute_position(position)
132
+ if parent_buffer
133
+ pos = parent_position(position) or return
134
+ parent_buffer.absolute_position(pos)
135
+ else
136
+ position
137
+ end
138
+ end
139
+
140
+ def top_buffer
141
+ if parent_buffer
142
+ parent_buffer.top_buffer
143
+ else
144
+ self
145
+ end
146
+ end
147
+
148
+ def detach
149
+ Buffer.new(name: name, content: content)
65
150
  end
66
151
  end
67
152
  end
data/lib/rbs/cli/diff.rb CHANGED
@@ -3,13 +3,14 @@
3
3
  module RBS
4
4
  class CLI
5
5
  class Diff
6
- def initialize(argv:, library_options:, stdout: $stdout, stderr: $stderr)
7
- @format = nil
6
+ def initialize(stdout: $stdout, stderr: $stderr)
8
7
  @stdout = stdout
9
8
  @stderr = stderr
9
+ end
10
10
 
11
- # @type var type_name: String?
12
- type_name = nil
11
+ def run(argv:, library_options:)
12
+ format = nil #: String?
13
+ type_name = nil #: String?
13
14
  library_options = library_options
14
15
  before_path = [] #: Array[String]
15
16
  after_path = [] #: Array[String]
@@ -32,7 +33,7 @@ module RBS
32
33
  # Confirmation of methods related to Time class added by including stdlib/time
33
34
  $ rbs diff --format diff --type-name Time --after stdlib/time
34
35
  HELP
35
- o.on("--format NAME") { |arg| @format = arg }
36
+ o.on("--format NAME") { |arg| format = arg }
36
37
  o.on("--type-name NAME") { |arg| type_name = arg }
37
38
  o.on("--before DIR") { |arg| before_path << arg }
38
39
  o.on("--after DIR") { |arg| after_path << arg }
@@ -40,28 +41,28 @@ module RBS
40
41
  end
41
42
  opt.parse!(argv)
42
43
 
43
- unless @format && type_name && ["markdown", "diff"].include?(@format)
44
+ unless format && type_name && ["markdown", "diff"].include?(format)
44
45
  @stderr.puts opt.banner
45
- exit 1
46
+ return 1
46
47
  end
47
48
 
48
- @diff = RBS::Diff.new(
49
+ diff = RBS::Diff.new(
49
50
  type_name: TypeName.parse(type_name).absolute!,
50
51
  library_options: library_options,
51
52
  after_path: after_path,
52
53
  before_path: before_path,
53
54
  detail: detail,
54
55
  )
55
- end
56
56
 
57
- def run
58
- public_send("run_#{@format}")
57
+ public_send("run_#{format}", diff)
58
+
59
+ 0
59
60
  end
60
61
 
61
- def run_diff
62
+ def run_diff(diff)
62
63
  first = true
63
64
  io = RBS::CLI::ColoredIO.new(stdout: @stdout)
64
- @diff.each_diff do |before, after|
65
+ diff.each_diff do |before, after|
65
66
  io.puts if !first
66
67
  io.puts_red "- #{before}"
67
68
  io.puts_green "+ #{after}"
@@ -69,10 +70,10 @@ module RBS
69
70
  end
70
71
  end
71
72
 
72
- def run_markdown
73
+ def run_markdown(diff)
73
74
  @stdout.puts "| before | after |"
74
75
  @stdout.puts "| --- | --- |"
75
- @diff.each_diff do |before, after|
76
+ diff.each_diff do |before, after|
76
77
  before.gsub!("|", "\\|")
77
78
  after.gsub!("|", "\\|")
78
79
  @stdout.puts "| `#{before}` | `#{after}` |"
@@ -4,36 +4,33 @@ module RBS
4
4
  class CLI
5
5
  class Validate
6
6
  class Errors
7
- def initialize(limit:, exit_error:)
7
+ def initialize(limit:)
8
8
  @limit = limit
9
- @exit_error = exit_error
10
9
  @errors = []
11
- @has_syntax_error = false
12
10
  end
13
11
 
14
12
  def add(error)
15
- if error.instance_of?(WillSyntaxError)
16
- RBS.logger.warn(build_message(error))
17
- @has_syntax_error = true
18
- else
19
- @errors << error
20
- end
13
+ @errors << error
21
14
  finish if @limit == 1
22
15
  end
23
16
 
17
+ def try(&block)
18
+ catch(:finish) do |tag|
19
+ @tag = tag
20
+ yield
21
+ finish()
22
+ end
23
+ end
24
+
24
25
  def finish
25
- if @errors.empty?
26
- if @exit_error && @has_syntax_error
27
- exit 1
28
- else
29
- # success
30
- end
31
- else
26
+ unless @errors.empty?
32
27
  @errors.each do |error|
33
28
  RBS.logger.error(build_message(error))
34
29
  end
35
- exit 1
30
+ throw @tag, 1
36
31
  end
32
+
33
+ 0
37
34
  end
38
35
 
39
36
  private
@@ -53,7 +50,6 @@ module RBS
53
50
  @env = Environment.from_loader(loader).resolve_type_names
54
51
  @builder = DefinitionBuilder.new(env: @env)
55
52
  @validator = Validator.new(env: @env)
56
- exit_error = false
57
53
  limit = nil #: Integer?
58
54
  OptionParser.new do |opts|
59
55
  opts.banner = <<EOU
@@ -70,25 +66,25 @@ EOU
70
66
  RBS.print_warning { "`--silent` option is deprecated because it's silent by default. You can use --log-level option of rbs command to display more information." }
71
67
  end
72
68
  opts.on("--[no-]exit-error-on-syntax-error", "exit(1) if syntax error is detected") {|bool|
73
- exit_error = bool
69
+ RBS.print_warning { "`--exit-error-on-syntax-error` option is deprecated because it's validated during parsing.." }
74
70
  }
75
71
  opts.on("--fail-fast", "Exit immediately as soon as a validation error is found.") do |arg|
76
72
  limit = 1
77
73
  end
78
74
  end.parse!(args)
79
75
 
80
- @errors = Errors.new(limit: limit, exit_error: exit_error)
76
+ @errors = Errors.new(limit: limit)
81
77
  end
82
78
 
83
79
  def run
84
- validate_class_module_definition
85
- validate_class_module_alias_definition
86
- validate_interface
87
- validate_constant
88
- validate_global
89
- validate_type_alias
90
-
91
- @errors.finish
80
+ @errors.try do
81
+ validate_class_module_definition
82
+ validate_class_module_alias_definition
83
+ validate_interface
84
+ validate_constant
85
+ validate_global
86
+ validate_type_alias
87
+ end
92
88
  end
93
89
 
94
90
  private
@@ -109,23 +105,17 @@ EOU
109
105
 
110
106
  case entry
111
107
  when Environment::ClassEntry
112
- entry.decls.each do |decl|
113
- if super_class = decl.decl.super_class
108
+ entry.each_decl do |decl|
109
+ if super_class = decl.super_class
114
110
  super_class.args.each do |arg|
115
- void_type_context_validator(arg, true)
116
- no_self_type_validator(arg)
117
- no_classish_type_validator(arg)
118
111
  @validator.validate_type(arg, context: nil)
119
112
  end
120
113
  end
121
114
  end
122
115
  when Environment::ModuleEntry
123
- entry.decls.each do |decl|
124
- decl.decl.self_types.each do |self_type|
116
+ entry.each_decl do |decl|
117
+ decl.self_types.each do |self_type|
125
118
  self_type.args.each do |arg|
126
- void_type_context_validator(arg, true)
127
- no_self_type_validator(arg)
128
- no_classish_type_validator(arg)
129
119
  @validator.validate_type(arg, context: nil)
130
120
  end
131
121
 
@@ -143,7 +133,7 @@ EOU
143
133
  end
144
134
  end
145
135
 
146
- d = entry.primary.decl
136
+ d = entry.primary_decl
147
137
 
148
138
  @validator.validate_type_params(
149
139
  d.type_params,
@@ -153,55 +143,43 @@ EOU
153
143
 
154
144
  d.type_params.each do |param|
155
145
  if ub = param.upper_bound_type
156
- void_type_context_validator(ub)
157
- no_self_type_validator(ub)
158
- no_classish_type_validator(ub)
159
146
  @validator.validate_type(ub, context: nil)
160
147
  end
161
148
 
149
+ if lb = param.lower_bound_type
150
+ @validator.validate_type(lb, context: nil)
151
+ end
152
+
162
153
  if dt = param.default_type
163
- void_type_context_validator(dt, true)
164
- no_self_type_validator(dt)
165
- no_classish_type_validator(dt)
166
154
  @validator.validate_type(dt, context: nil)
167
155
  end
168
156
  end
169
157
 
170
158
  TypeParamDefaultReferenceError.check!(d.type_params)
171
159
 
172
- entry.decls.each do |d|
173
- d.decl.each_member do |member|
174
- case member
175
- when AST::Members::MethodDefinition
176
- @validator.validate_method_definition(member, type_name: name)
177
- member.overloads.each do |ov|
178
- void_type_context_validator(ov.method_type)
179
- end
180
- when AST::Members::Attribute
181
- void_type_context_validator(member.type)
182
- when AST::Members::Mixin
183
- member.args.each do |arg|
184
- no_self_type_validator(arg)
185
- unless arg.is_a?(Types::Bases::Void)
186
- void_type_context_validator(arg, true)
187
- end
188
- end
189
- params =
190
- if member.name.class?
191
- module_decl = @env.module_entry(member.name, normalized: true) or raise
192
- module_decl.type_params
193
- else
194
- interface_decl = @env.interface_decls.fetch(member.name)
195
- interface_decl.decl.type_params
196
- end
197
- InvalidTypeApplicationError.check!(type_name: member.name, params: params, args: member.args, location: member.location)
198
- when AST::Members::Var
199
- @validator.validate_variable(member)
200
- void_type_context_validator(member.type)
201
- if member.is_a?(AST::Members::ClassVariable)
202
- no_self_type_validator(member.type)
160
+ entry.each_decl do |decl|
161
+ case decl
162
+ when AST::Declarations::Base
163
+ decl.each_member do |member|
164
+ case member
165
+ when AST::Members::MethodDefinition
166
+ @validator.validate_method_definition(member, type_name: name)
167
+ when AST::Members::Mixin
168
+ params =
169
+ if member.name.class?
170
+ module_decl = @env.module_entry(member.name, normalized: true) or raise
171
+ module_decl.type_params
172
+ else
173
+ interface_decl = @env.interface_decls.fetch(member.name)
174
+ interface_decl.decl.type_params
175
+ end
176
+ InvalidTypeApplicationError.check!(type_name: member.name, params: params, args: member.args, location: member.location)
177
+ when AST::Members::Var
178
+ @validator.validate_variable(member)
203
179
  end
204
180
  end
181
+ else
182
+ raise "Unknown declaration: #{decl.class}"
205
183
  end
206
184
  end
207
185
  rescue BaseError => error
@@ -233,16 +211,14 @@ EOU
233
211
 
234
212
  decl.decl.type_params.each do |param|
235
213
  if ub = param.upper_bound_type
236
- void_type_context_validator(ub)
237
- no_self_type_validator(ub)
238
- no_classish_type_validator(ub)
239
214
  @validator.validate_type(ub, context: nil)
240
215
  end
241
216
 
217
+ if lb = param.lower_bound_type
218
+ @validator.validate_type(lb, context: nil)
219
+ end
220
+
242
221
  if dt = param.default_type
243
- void_type_context_validator(dt, true)
244
- no_self_type_validator(dt)
245
- no_classish_type_validator(dt)
246
222
  @validator.validate_type(dt, context: nil)
247
223
  end
248
224
  end
@@ -253,10 +229,6 @@ EOU
253
229
  case member
254
230
  when AST::Members::MethodDefinition
255
231
  @validator.validate_method_definition(member, type_name: name)
256
- member.overloads.each do |ov|
257
- void_type_context_validator(ov.method_type)
258
- no_classish_type_validator(ov.method_type)
259
- end
260
232
  end
261
233
  end
262
234
  rescue BaseError => error
@@ -269,9 +241,6 @@ EOU
269
241
  RBS.logger.info "Validating constant: `#{name}`..."
270
242
  @validator.validate_type const.decl.type, context: const.context
271
243
  @builder.ensure_namespace!(name.namespace, location: const.decl.location)
272
- no_self_type_validator(const.decl.type)
273
- no_classish_type_validator(const.decl.type)
274
- void_type_context_validator(const.decl.type)
275
244
  rescue BaseError => error
276
245
  @errors.add(error)
277
246
  end
@@ -281,9 +250,6 @@ EOU
281
250
  @env.global_decls.each do |name, global|
282
251
  RBS.logger.info "Validating global: `#{name}`..."
283
252
  @validator.validate_type global.decl.type, context: nil
284
- no_self_type_validator(global.decl.type)
285
- no_classish_type_validator(global.decl.type)
286
- void_type_context_validator(global.decl.type)
287
253
  rescue BaseError => error
288
254
  @errors.add(error)
289
255
  end
@@ -306,52 +272,23 @@ EOU
306
272
 
307
273
  decl.decl.type_params.each do |param|
308
274
  if ub = param.upper_bound_type
309
- void_type_context_validator(ub)
310
- no_self_type_validator(ub)
311
- no_classish_type_validator(ub)
312
275
  @validator.validate_type(ub, context: nil)
313
276
  end
314
277
 
278
+ if lb = param.lower_bound_type
279
+ @validator.validate_type(lb, context: nil)
280
+ end
281
+
315
282
  if dt = param.default_type
316
- void_type_context_validator(dt, true)
317
- no_self_type_validator(dt)
318
- no_classish_type_validator(dt)
319
283
  @validator.validate_type(dt, context: nil)
320
284
  end
321
285
  end
322
286
 
323
287
  TypeParamDefaultReferenceError.check!(decl.decl.type_params)
324
-
325
- no_self_type_validator(decl.decl.type)
326
- no_classish_type_validator(decl.decl.type)
327
- void_type_context_validator(decl.decl.type)
328
288
  rescue BaseError => error
329
289
  @errors.add(error)
330
290
  end
331
291
  end
332
-
333
- private
334
-
335
- def no_self_type_validator(type)
336
- if type.has_self_type?
337
- @errors.add WillSyntaxError.new("`self` type is not allowed in this context", location: type.location)
338
- end
339
- end
340
-
341
- def no_classish_type_validator(type)
342
- if type.has_classish_type?
343
- @errors.add WillSyntaxError.new("`instance` or `class` type is not allowed in this context", location: type.location)
344
- end
345
- end
346
-
347
- def void_type_context_validator(type, allowed_here = false)
348
- if allowed_here
349
- return if type.is_a?(Types::Bases::Void)
350
- end
351
- if type.with_nonreturn_void?
352
- @errors.add WillSyntaxError.new("`void` type is only allowed in return type or generics parameter", location: type.location)
353
- end
354
- end
355
292
  end
356
293
  end
357
294
  end