rbs 0.11.0 → 0.13.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +9 -9
  3. data/CHANGELOG.md +24 -0
  4. data/Rakefile +16 -6
  5. data/Steepfile +28 -0
  6. data/bin/steep +4 -0
  7. data/bin/test_runner.rb +7 -5
  8. data/lib/rbs/ast/comment.rb +7 -1
  9. data/lib/rbs/ast/declarations.rb +15 -9
  10. data/lib/rbs/buffer.rb +1 -1
  11. data/lib/rbs/cli.rb +12 -4
  12. data/lib/rbs/constant.rb +1 -1
  13. data/lib/rbs/constant_table.rb +9 -8
  14. data/lib/rbs/definition.rb +22 -13
  15. data/lib/rbs/definition_builder.rb +79 -55
  16. data/lib/rbs/environment.rb +28 -10
  17. data/lib/rbs/environment_loader.rb +12 -12
  18. data/lib/rbs/location.rb +1 -5
  19. data/lib/rbs/method_type.rb +5 -5
  20. data/lib/rbs/namespace.rb +14 -3
  21. data/lib/rbs/parser.y +0 -8
  22. data/lib/rbs/prototype/rb.rb +3 -4
  23. data/lib/rbs/prototype/rbi.rb +1 -2
  24. data/lib/rbs/substitution.rb +4 -3
  25. data/lib/rbs/type_name.rb +18 -1
  26. data/lib/rbs/type_name_resolver.rb +10 -3
  27. data/lib/rbs/types.rb +27 -21
  28. data/lib/rbs/variance_calculator.rb +9 -6
  29. data/lib/rbs/version.rb +1 -1
  30. data/lib/rbs/writer.rb +25 -15
  31. data/sig/annotation.rbs +26 -0
  32. data/sig/buffer.rbs +28 -0
  33. data/sig/builtin_names.rbs +41 -0
  34. data/sig/comment.rbs +26 -0
  35. data/sig/constant.rbs +21 -0
  36. data/sig/constant_table.rbs +30 -0
  37. data/sig/declarations.rbs +202 -0
  38. data/sig/definition.rbs +129 -0
  39. data/sig/definition_builder.rbs +94 -0
  40. data/sig/environment.rbs +94 -0
  41. data/sig/environment_loader.rbs +58 -0
  42. data/sig/location.rbs +52 -0
  43. data/sig/members.rbs +160 -0
  44. data/sig/method_types.rbs +40 -0
  45. data/sig/namespace.rbs +124 -0
  46. data/sig/polyfill.rbs +3 -0
  47. data/sig/rbs.rbs +3 -0
  48. data/sig/substitution.rbs +39 -0
  49. data/sig/type_name_resolver.rbs +24 -0
  50. data/sig/typename.rbs +70 -0
  51. data/sig/types.rbs +361 -0
  52. data/sig/util.rbs +13 -0
  53. data/sig/variance_calculator.rbs +35 -0
  54. data/sig/version.rbs +3 -0
  55. data/sig/writer.rbs +40 -0
  56. data/stdlib/bigdecimal/big_decimal.rbs +887 -0
  57. data/stdlib/bigdecimal/math/big_math.rbs +142 -0
  58. data/stdlib/builtin/builtin.rbs +0 -3
  59. data/stdlib/builtin/kernel.rbs +2 -0
  60. data/stdlib/builtin/math.rbs +26 -26
  61. data/stdlib/builtin/struct.rbs +9 -10
  62. data/stdlib/forwardable/forwardable.rbs +204 -0
  63. data/stdlib/pathname/pathname.rbs +2 -0
  64. data/stdlib/pty/pty.rbs +5 -29
  65. data/stdlib/set/set.rbs +1 -1
  66. data/stdlib/uri/file.rbs +167 -0
  67. data/stdlib/uri/generic.rbs +875 -0
  68. data/stdlib/uri/http.rbs +158 -0
  69. data/stdlib/uri/https.rbs +108 -0
  70. data/stdlib/uri/ldap.rbs +224 -0
  71. data/stdlib/uri/ldaps.rbs +108 -0
  72. data/steep/Gemfile +3 -0
  73. data/steep/Gemfile.lock +51 -0
  74. metadata +43 -5
@@ -9,14 +9,14 @@ module RBS
9
9
  end
10
10
  end
11
11
 
12
- LibraryPath = Struct.new(:name, :path, keyword_init: true)
13
- GemPath = Struct.new(:name, :version, :path, keyword_init: true)
12
+ LibraryPath = _ = Struct.new(:name, :path, keyword_init: true)
13
+ GemPath = _ = Struct.new(:name, :version, :path, keyword_init: true)
14
14
 
15
15
  attr_reader :paths
16
16
  attr_reader :stdlib_root
17
17
  attr_reader :gem_vendor_path
18
18
 
19
- STDLIB_ROOT = Pathname(__dir__) + "../../stdlib"
19
+ STDLIB_ROOT = Pathname(_ = __dir__) + "../../stdlib"
20
20
 
21
21
  def self.gem_sig_path(name, version)
22
22
  Pathname(Gem::Specification.find_by_name(name, version).gem_dir) + "sig"
@@ -50,22 +50,20 @@ module RBS
50
50
  end
51
51
 
52
52
  def self.parse_library(lib)
53
- lib.split(/:/)
53
+ _ = lib.split(/:/)
54
54
  end
55
55
 
56
56
  def stdlib?(name)
57
- if stdlib_root
58
- path = stdlib_root + name
59
- if path.directory?
60
- path
61
- end
57
+ path = stdlib_root + name
58
+ if path.directory?
59
+ path
62
60
  end
63
61
  end
64
62
 
65
63
  def gem?(name, version)
66
- if gem_vendor_path
64
+ if path = gem_vendor_path
67
65
  # Try vendored RBS first
68
- gem_dir = gem_vendor_path + name
66
+ gem_dir = path + name
69
67
  if gem_dir.directory?
70
68
  return gem_dir
71
69
  end
@@ -76,7 +74,7 @@ module RBS
76
74
  end
77
75
 
78
76
  def each_signature(path, immediate: true, &block)
79
- if block_given?
77
+ if block
80
78
  case
81
79
  when path.file?
82
80
  if path.extname == ".rbs" || immediate
@@ -116,6 +114,7 @@ module RBS
116
114
 
117
115
  def each_decl
118
116
  if block_given?
117
+ # @type var signature_files: Array[[path | :stdlib, Pathname]]
119
118
  signature_files = []
120
119
 
121
120
  unless no_builtin?
@@ -142,6 +141,7 @@ module RBS
142
141
  end
143
142
 
144
143
  def load(env:)
144
+ # @type var loadeds: Array[[AST::Declarations::t, Pathname, path | :stdlib]]
145
145
  loadeds = []
146
146
 
147
147
  each_decl do |decl, buffer, file_path, lib_path|
@@ -43,7 +43,7 @@ module RBS
43
43
  end
44
44
 
45
45
  def source
46
- @source ||= buffer.content[start_pos...end_pos]
46
+ @source ||= buffer.content[start_pos...end_pos] or raise
47
47
  end
48
48
 
49
49
  def to_s
@@ -73,10 +73,6 @@ module RBS
73
73
  end
74
74
  end
75
75
 
76
- def self.concat(*locations)
77
- locations.inject {|l1, l2| l1 + l2 }
78
- end
79
-
80
76
  def concat(*others)
81
77
  others.each { |other| self << other }
82
78
  self
@@ -93,7 +93,7 @@ module RBS
93
93
  end
94
94
 
95
95
  def each_type(&block)
96
- if block_given?
96
+ if block
97
97
  type.each_type(&block)
98
98
  self.block&.yield_self do |b|
99
99
  b.type.each_type(&block)
@@ -105,10 +105,10 @@ module RBS
105
105
 
106
106
  def to_s
107
107
  s = case
108
- when block && block.required
109
- "(#{type.param_to_s}) { (#{block.type.param_to_s}) -> #{block.type.return_to_s} } -> #{type.return_to_s}"
110
- when block
111
- "(#{type.param_to_s}) ?{ (#{block.type.param_to_s}) -> #{block.type.return_to_s} } -> #{type.return_to_s}"
108
+ when (b = block) && b.required
109
+ "(#{type.param_to_s}) { (#{b.type.param_to_s}) -> #{b.type.return_to_s} } -> #{type.return_to_s}"
110
+ when b = block
111
+ "(#{type.param_to_s}) ?{ (#{b.type.param_to_s}) -> #{b.type.return_to_s} } -> #{type.return_to_s}"
112
112
  else
113
113
  "(#{type.param_to_s}) -> #{type.return_to_s}"
114
114
  end
@@ -63,7 +63,9 @@ module RBS
63
63
  end
64
64
 
65
65
  def split
66
- [parent, path.last]
66
+ last = path.last or return
67
+ parent = self.parent
68
+ [parent, last]
67
69
  end
68
70
 
69
71
  def to_s
@@ -77,6 +79,10 @@ module RBS
77
79
 
78
80
  def to_type_name
79
81
  parent, name = split
82
+
83
+ raise unless name
84
+ raise unless parent
85
+
80
86
  TypeName.new(name: name, namespace: parent)
81
87
  end
82
88
 
@@ -88,14 +94,13 @@ module RBS
88
94
  end
89
95
  end
90
96
 
91
-
92
97
  def ascend
93
98
  if block_given?
94
99
  current = self
95
100
 
96
101
  until current.empty?
97
102
  yield current
98
- current = current.parent
103
+ current = _ = current.parent
99
104
  end
100
105
 
101
106
  yield current
@@ -107,3 +112,9 @@ module RBS
107
112
  end
108
113
  end
109
114
  end
115
+
116
+ module Kernel
117
+ def Namespace(name)
118
+ RBS::Namespace.parse(name)
119
+ end
120
+ end
@@ -435,14 +435,6 @@ rule
435
435
  method_member:
436
436
  annotations attributes overload kDEF method_kind def_name method_types {
437
437
  location = val[3].location + val[6].last.location
438
- types = val[6].map do |type|
439
- case type
440
- when LocatedValue
441
- type.value
442
- else
443
- type
444
- end
445
- end
446
438
 
447
439
  last_type = val[6].last
448
440
  if last_type.is_a?(LocatedValue) && last_type.value == :dot3
@@ -36,7 +36,7 @@ module RBS
36
36
  tokens.each.with_object({}) do |token, hash|
37
37
  if token[1] == :on_comment
38
38
  line = token[0][0]
39
- body = token[2][2..]
39
+ body = token[2][2..-1]
40
40
 
41
41
  body = "\n" if body.empty?
42
42
 
@@ -353,7 +353,6 @@ module RBS
353
353
  Types::Bases::Nil.new(location: nil)
354
354
  when :LIT
355
355
  lit = node.children[0]
356
- name = lit.class.name
357
356
  case lit
358
357
  when Symbol
359
358
  if lit.match?(/\A[ -~]+\z/)
@@ -364,7 +363,7 @@ module RBS
364
363
  when Integer
365
364
  Types::Literal.new(literal: lit, location: nil)
366
365
  else
367
- type_name = TypeName.new(name: name, namespace: Namespace.root)
366
+ type_name = TypeName.new(name: lit.class.name.to_sym, namespace: Namespace.root)
368
367
  Types::ClassInstance.new(name: type_name, args: [], location: nil)
369
368
  end
370
369
  when :ZLIST, :ZARRAY
@@ -420,7 +419,7 @@ module RBS
420
419
 
421
420
  types = types.map do |t|
422
421
  if t.is_a?(Types::Literal)
423
- type_name = TypeName.new(name: t.literal.class.name, namespace: Namespace.root)
422
+ type_name = TypeName.new(name: t.literal.class.name.to_sym, namespace: Namespace.root)
424
423
  Types::ClassInstance.new(name: type_name, args: [], location: nil)
425
424
  else
426
425
  t
@@ -16,7 +16,7 @@ module RBS
16
16
  tokens.each.with_object({}) do |token, hash|
17
17
  if token[1] == :on_comment
18
18
  line = token[0][0]
19
- body = token[2][2..]
19
+ body = token[2][2..-1]
20
20
 
21
21
  body = "\n" if body.empty?
22
22
 
@@ -500,7 +500,6 @@ module RBS
500
500
  else
501
501
  type_node.type == :CALL && proc_type?(type_node.children[0])
502
502
  end
503
-
504
503
  end
505
504
 
506
505
  def call_node?(node, name:, receiver: -> (node) { node.type == :CONST && node.children[0] == :T }, args: -> (node) { true })
@@ -31,10 +31,11 @@ module RBS
31
31
  def apply(ty)
32
32
  case ty
33
33
  when Types::Variable
34
+ # @type var ty: Types::Variable
34
35
  mapping[ty.name] || ty
35
36
  when Types::Bases::Instance
36
- if instance_type
37
- instance_type
37
+ if t = instance_type
38
+ t
38
39
  else
39
40
  ty
40
41
  end
@@ -44,7 +45,7 @@ module RBS
44
45
  end
45
46
 
46
47
  def without(*vars)
47
- self.class.new.tap do |subst|
48
+ Substitution.new.tap do |subst|
48
49
  subst.mapping.merge!(mapping)
49
50
  vars.each do |var|
50
51
  subst.mapping.delete(var)
@@ -1,3 +1,4 @@
1
+
1
2
  module RBS
2
3
  class TypeName
3
4
  attr_reader :namespace
@@ -14,13 +15,15 @@ module RBS
14
15
  :alias
15
16
  when "_"
16
17
  :interface
18
+ else
19
+ raise
17
20
  end
18
21
  end
19
22
 
20
23
  def ==(other)
21
24
  other.is_a?(self.class) && other.namespace == namespace && other.name == name
22
25
  end
23
-
26
+
24
27
  alias eql? ==
25
28
 
26
29
  def hash
@@ -68,3 +71,17 @@ module RBS
68
71
  end
69
72
  end
70
73
  end
74
+
75
+ module Kernel
76
+ def TypeName(string)
77
+ absolute = string.start_with?("::")
78
+
79
+ *path, name = string.delete_prefix("::").split("::").map(&:to_sym)
80
+ raise unless name
81
+
82
+ RBS::TypeName.new(
83
+ name: name,
84
+ namespace: RBS::Namespace.new(path: path, absolute: absolute)
85
+ )
86
+ end
87
+ end
@@ -1,6 +1,6 @@
1
1
  module RBS
2
2
  class TypeNameResolver
3
- Query = Struct.new(:type_name, :context, keyword_init: true)
3
+ Query = _ = Struct.new(:type_name, :context, keyword_init: true)
4
4
 
5
5
  attr_reader :all_names
6
6
  attr_reader :cache
@@ -36,15 +36,22 @@ module RBS
36
36
  query = Query.new(type_name: type_name, context: context)
37
37
  try_cache(query) do
38
38
  path_head, *path_tail = type_name.to_namespace.path
39
+ raise unless path_head
40
+
39
41
  name_head = TypeName.new(name: path_head, namespace: Namespace.empty)
40
42
 
41
- absolute_head = context.each.find do |namespace|
43
+ absolute_head = context.find do |namespace|
44
+ # @type break: TypeName
42
45
  full_name = name_head.with_prefix(namespace)
43
46
  has_name?(full_name) and break full_name
44
47
  end
45
48
 
46
- if absolute_head
49
+ case absolute_head
50
+ when TypeName
47
51
  has_name?(Namespace.new(path: absolute_head.to_namespace.path.push(*path_tail), absolute: true).to_type_name)
52
+ when Namespace
53
+ # This cannot happen because the `context.find` doesn't return a Namespace.
54
+ raise
48
55
  end
49
56
  end
50
57
  end
@@ -1,3 +1,4 @@
1
+
1
2
  module RBS
2
3
  module Types
3
4
  module NoFreeVariables
@@ -77,7 +78,7 @@ module RBS
77
78
  when Types::Bases::Class
78
79
  'class'
79
80
  else
80
- raise "Unexpected base type: #{type.inspect}"
81
+ raise "Unexpected base type: #{inspect}"
81
82
  end
82
83
  end
83
84
  end
@@ -138,8 +139,6 @@ module RBS
138
139
  new(name: v, location: nil)
139
140
  when Array
140
141
  v.map {|x| new(name: x, location: nil) }
141
- else
142
- raise
143
142
  end
144
143
  end
145
144
 
@@ -227,7 +226,7 @@ module RBS
227
226
  end
228
227
 
229
228
  def each_type(&block)
230
- if block_given?
229
+ if block
231
230
  args.each(&block)
232
231
  else
233
232
  enum_for :each_type
@@ -380,7 +379,7 @@ module RBS
380
379
  end
381
380
 
382
381
  def each_type(&block)
383
- if block_given?
382
+ if block
384
383
  types.each(&block)
385
384
  else
386
385
  enum_for :each_type
@@ -445,7 +444,7 @@ module RBS
445
444
  end
446
445
 
447
446
  def each_type(&block)
448
- if block_given?
447
+ if block
449
448
  fields.each_value(&block)
450
449
  else
451
450
  enum_for :each_type
@@ -492,11 +491,15 @@ module RBS
492
491
  end
493
492
 
494
493
  def to_s(level = 0)
495
- if type.is_a?(RBS::Types::Literal) && type.literal.is_a?(Symbol)
496
- "#{type.to_s(1)} ?"
497
- else
498
- "#{type.to_s(1)}?"
494
+ case t = type
495
+ when RBS::Types::Literal
496
+ case t.literal
497
+ when Symbol
498
+ return "#{type.to_s(1)} ?"
499
+ end
499
500
  end
501
+
502
+ "#{type.to_s(1)}?"
500
503
  end
501
504
 
502
505
  def each_type
@@ -560,7 +563,7 @@ module RBS
560
563
  end
561
564
 
562
565
  def each_type(&block)
563
- if block_given?
566
+ if block
564
567
  types.each(&block)
565
568
  else
566
569
  enum_for :each_type
@@ -568,7 +571,7 @@ module RBS
568
571
  end
569
572
 
570
573
  def map_type(&block)
571
- if block_given?
574
+ if block
572
575
  Union.new(types: types.map(&block), location: location)
573
576
  else
574
577
  enum_for :map_type
@@ -629,7 +632,7 @@ module RBS
629
632
  end
630
633
 
631
634
  def each_type(&block)
632
- if block_given?
635
+ if block
633
636
  types.each(&block)
634
637
  else
635
638
  enum_for :each_type
@@ -637,7 +640,7 @@ module RBS
637
640
  end
638
641
 
639
642
  def map_type(&block)
640
- if block_given?
643
+ if block
641
644
  Intersection.new(types: types.map(&block), location: location)
642
645
  else
643
646
  enum_for :map_type
@@ -672,8 +675,8 @@ module RBS
672
675
  self.class.hash ^ type.hash ^ name.hash
673
676
  end
674
677
 
675
- def map_type
676
- if block_given?
678
+ def map_type(&block)
679
+ if block
677
680
  Param.new(name: name, type: yield(type))
678
681
  else
679
682
  enum_for :map_type
@@ -772,7 +775,7 @@ module RBS
772
775
  end
773
776
 
774
777
  def map_type(&block)
775
- if block_given?
778
+ if block
776
779
  Function.new(
777
780
  required_positionals: required_positionals.map {|param| param.map_type(&block) },
778
781
  optional_positionals: optional_positionals.map {|param| param.map_type(&block) },
@@ -810,7 +813,7 @@ module RBS
810
813
  end
811
814
 
812
815
  def each_param(&block)
813
- if block_given?
816
+ if block
814
817
  required_positionals.each(&block)
815
818
  optional_positionals.each(&block)
816
819
  rest_positionals&.yield_self(&block)
@@ -891,7 +894,9 @@ module RBS
891
894
  end
892
895
 
893
896
  def param_to_s
897
+ # @type var params: Array[String]
894
898
  params = []
899
+
895
900
  params.push(*required_positionals.map(&:to_s))
896
901
  params.push(*optional_positionals.map {|p| "?#{p}"})
897
902
  params.push("*#{rest_positionals}") if rest_positionals
@@ -927,8 +932,9 @@ module RBS
927
932
  def drop_tail
928
933
  case
929
934
  when !trailing_positionals.empty?
935
+ last = trailing_positionals.last or raise
930
936
  [
931
- trailing_positionals.last,
937
+ last,
932
938
  update(trailing_positionals: trailing_positionals.take(trailing_positionals.size - 1))
933
939
  ]
934
940
  else
@@ -960,7 +966,7 @@ module RBS
960
966
  self.class.hash ^ type.hash
961
967
  end
962
968
 
963
- def free_variables(set)
969
+ def free_variables(set = Set[])
964
970
  type.free_variables(set)
965
971
  end
966
972
 
@@ -977,7 +983,7 @@ module RBS
977
983
  end
978
984
 
979
985
  def each_type(&block)
980
- if block_given?
986
+ if block
981
987
  type.each_type(&block)
982
988
  else
983
989
  enum_for :each_type