rbs 0.14.0 → 0.18.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (144) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +8 -4
  3. data/CHANGELOG.md +35 -0
  4. data/Gemfile +1 -0
  5. data/README.md +1 -1
  6. data/Rakefile +19 -1
  7. data/Steepfile +0 -1
  8. data/bin/test_runner.rb +15 -1
  9. data/{stdlib/builtin → core}/array.rbs +124 -120
  10. data/{stdlib/builtin → core}/basic_object.rbs +0 -0
  11. data/{stdlib/builtin → core}/binding.rbs +0 -0
  12. data/core/builtin.rbs +70 -0
  13. data/{stdlib/builtin → core}/class.rbs +0 -0
  14. data/{stdlib/builtin → core}/comparable.rbs +0 -0
  15. data/{stdlib/builtin → core}/complex.rbs +0 -0
  16. data/{stdlib/builtin → core}/constants.rbs +0 -0
  17. data/{stdlib/builtin → core}/data.rbs +0 -0
  18. data/{stdlib/builtin → core}/deprecated.rbs +0 -0
  19. data/{stdlib/builtin → core}/dir.rbs +1 -1
  20. data/{stdlib/builtin → core}/encoding.rbs +0 -0
  21. data/{stdlib/builtin → core}/enumerable.rbs +67 -60
  22. data/{stdlib/builtin → core}/enumerator.rbs +5 -5
  23. data/{stdlib/builtin → core}/errno.rbs +0 -0
  24. data/{stdlib/builtin → core}/errors.rbs +1 -1
  25. data/{stdlib/builtin → core}/exception.rbs +0 -0
  26. data/{stdlib/builtin → core}/false_class.rbs +0 -0
  27. data/{stdlib/builtin → core}/fiber.rbs +0 -0
  28. data/{stdlib/builtin → core}/fiber_error.rbs +0 -0
  29. data/{stdlib/builtin → core}/file.rbs +0 -0
  30. data/{stdlib/builtin → core}/file_test.rbs +0 -0
  31. data/{stdlib/builtin → core}/float.rbs +0 -0
  32. data/{stdlib/builtin → core}/gc.rbs +2 -2
  33. data/{stdlib/builtin → core}/hash.rbs +15 -15
  34. data/{stdlib/builtin → core}/integer.rbs +0 -0
  35. data/{stdlib/builtin → core}/io.rbs +6 -6
  36. data/{stdlib/builtin → core}/kernel.rbs +1 -85
  37. data/{stdlib/builtin → core}/marshal.rbs +0 -0
  38. data/{stdlib/builtin → core}/match_data.rbs +0 -0
  39. data/{stdlib/builtin → core}/math.rbs +0 -0
  40. data/{stdlib/builtin → core}/method.rbs +0 -0
  41. data/{stdlib/builtin → core}/module.rbs +13 -13
  42. data/{stdlib/builtin → core}/nil_class.rbs +0 -0
  43. data/{stdlib/builtin → core}/numeric.rbs +0 -0
  44. data/{stdlib/builtin → core}/object.rbs +1 -1
  45. data/core/object_space.rbs +98 -0
  46. data/{stdlib/builtin → core}/proc.rbs +0 -0
  47. data/{stdlib/builtin → core}/process.rbs +0 -0
  48. data/{stdlib/builtin → core}/random.rbs +1 -1
  49. data/{stdlib/builtin → core}/range.rbs +3 -3
  50. data/{stdlib/builtin → core}/rational.rbs +0 -0
  51. data/{stdlib/builtin → core}/rb_config.rbs +0 -0
  52. data/{stdlib/builtin → core}/regexp.rbs +0 -0
  53. data/{stdlib/builtin → core}/ruby_vm.rbs +0 -0
  54. data/{stdlib/builtin → core}/signal.rbs +0 -0
  55. data/{stdlib/builtin → core}/string.rbs +6 -6
  56. data/{stdlib/builtin → core}/string_io.rbs +7 -7
  57. data/{stdlib/builtin → core}/struct.rbs +2 -2
  58. data/{stdlib/builtin → core}/symbol.rbs +1 -1
  59. data/{stdlib/builtin → core}/thread.rbs +4 -4
  60. data/{stdlib/builtin → core}/thread_group.rbs +0 -0
  61. data/{stdlib/builtin → core}/time.rbs +0 -0
  62. data/{stdlib/builtin → core}/trace_point.rbs +0 -0
  63. data/{stdlib/builtin → core}/true_class.rbs +1 -1
  64. data/{stdlib/builtin → core}/unbound_method.rbs +0 -0
  65. data/{stdlib/builtin → core}/warning.rbs +0 -0
  66. data/docs/repo.md +125 -0
  67. data/docs/syntax.md +50 -6
  68. data/lib/rbs.rb +1 -0
  69. data/lib/rbs/cli.rb +105 -103
  70. data/lib/rbs/definition.rb +9 -4
  71. data/lib/rbs/definition_builder.rb +50 -17
  72. data/lib/rbs/environment_loader.rb +79 -105
  73. data/lib/rbs/environment_walker.rb +70 -35
  74. data/lib/rbs/parser.rb +404 -397
  75. data/lib/rbs/parser.y +18 -9
  76. data/lib/rbs/prototype/rb.rb +186 -25
  77. data/lib/rbs/prototype/runtime.rb +18 -7
  78. data/lib/rbs/repository.rb +121 -0
  79. data/lib/rbs/test/hook.rb +27 -15
  80. data/lib/rbs/test/setup.rb +5 -3
  81. data/lib/rbs/test/tester.rb +4 -1
  82. data/lib/rbs/test/type_check.rb +16 -5
  83. data/lib/rbs/type_name.rb +2 -1
  84. data/lib/rbs/vendorer.rb +38 -16
  85. data/lib/rbs/version.rb +1 -1
  86. data/sig/cli.rbs +58 -0
  87. data/sig/constant_table.rbs +1 -1
  88. data/sig/declarations.rbs +1 -1
  89. data/sig/definition.rbs +9 -4
  90. data/sig/definition_builder.rbs +4 -1
  91. data/sig/environment_loader.rbs +92 -46
  92. data/sig/members.rbs +2 -2
  93. data/sig/method_types.rbs +1 -1
  94. data/sig/namespace.rbs +1 -1
  95. data/sig/polyfill.rbs +42 -0
  96. data/sig/rbs.rbs +8 -0
  97. data/sig/repository.rbs +79 -0
  98. data/sig/vendorer.rbs +44 -0
  99. data/stdlib/abbrev/{abbrev.rbs → 0/abbrev.rbs} +0 -0
  100. data/stdlib/base64/{base64.rbs → 0/base64.rbs} +1 -1
  101. data/stdlib/benchmark/{benchmark.rbs → 0/benchmark.rbs} +0 -0
  102. data/stdlib/{bigdecimal/math → bigdecimal-math/0}/big_math.rbs +0 -0
  103. data/stdlib/bigdecimal/{big_decimal.rbs → 0/big_decimal.rbs} +0 -0
  104. data/stdlib/coverage/{coverage.rbs → 0/coverage.rbs} +2 -2
  105. data/stdlib/csv/{csv.rbs → 0/csv.rbs} +4 -4
  106. data/stdlib/date/{date.rbs → 0/date.rbs} +2 -2
  107. data/stdlib/date/{date_time.rbs → 0/date_time.rbs} +1 -1
  108. data/stdlib/dbm/0/dbm.rbs +277 -0
  109. data/stdlib/erb/{erb.rbs → 0/erb.rbs} +0 -0
  110. data/stdlib/fiber/{fiber.rbs → 0/fiber.rbs} +0 -0
  111. data/stdlib/find/{find.rbs → 0/find.rbs} +2 -2
  112. data/stdlib/forwardable/{forwardable.rbs → 0/forwardable.rbs} +0 -0
  113. data/stdlib/ipaddr/{ipaddr.rbs → 0/ipaddr.rbs} +0 -0
  114. data/stdlib/json/{json.rbs → 0/json.rbs} +0 -0
  115. data/stdlib/logger/{formatter.rbs → 0/formatter.rbs} +0 -0
  116. data/stdlib/logger/{log_device.rbs → 0/log_device.rbs} +1 -1
  117. data/stdlib/logger/{logger.rbs → 0/logger.rbs} +1 -1
  118. data/stdlib/logger/{period.rbs → 0/period.rbs} +0 -0
  119. data/stdlib/logger/{severity.rbs → 0/severity.rbs} +0 -0
  120. data/stdlib/mutex_m/{mutex_m.rbs → 0/mutex_m.rbs} +0 -0
  121. data/stdlib/pathname/{pathname.rbs → 0/pathname.rbs} +39 -39
  122. data/stdlib/prime/{integer-extension.rbs → 0/integer-extension.rbs} +0 -0
  123. data/stdlib/prime/{prime.rbs → 0/prime.rbs} +1 -1
  124. data/stdlib/pstore/0/pstore.rbs +287 -0
  125. data/stdlib/pty/{pty.rbs → 0/pty.rbs} +1 -1
  126. data/stdlib/securerandom/{securerandom.rbs → 0/securerandom.rbs} +0 -0
  127. data/stdlib/set/{set.rbs → 0/set.rbs} +10 -10
  128. data/stdlib/singleton/0/singleton.rbs +111 -0
  129. data/stdlib/tmpdir/{tmpdir.rbs → 0/tmpdir.rbs} +0 -0
  130. data/stdlib/tsort/0/cyclic.rbs +4 -0
  131. data/stdlib/tsort/0/interfaces.rbs +19 -0
  132. data/stdlib/tsort/0/tsort.rbs +363 -0
  133. data/stdlib/uri/{file.rbs → 0/file.rbs} +0 -0
  134. data/stdlib/uri/{generic.rbs → 0/generic.rbs} +1 -1
  135. data/stdlib/uri/{http.rbs → 0/http.rbs} +0 -0
  136. data/stdlib/uri/{https.rbs → 0/https.rbs} +0 -0
  137. data/stdlib/uri/{ldap.rbs → 0/ldap.rbs} +0 -0
  138. data/stdlib/uri/{ldaps.rbs → 0/ldaps.rbs} +0 -0
  139. data/stdlib/yaml/0/dbm.rbs +221 -0
  140. data/stdlib/yaml/0/store.rbs +53 -0
  141. data/stdlib/zlib/{zlib.rbs → 0/zlib.rbs} +0 -0
  142. data/steep/Gemfile.lock +9 -9
  143. metadata +108 -94
  144. data/stdlib/builtin/builtin.rbs +0 -42
@@ -60,12 +60,14 @@ module RBS
60
60
  attr_reader :defs
61
61
  attr_reader :accessibility
62
62
  attr_reader :extra_annotations
63
+ attr_reader :alias_of
63
64
 
64
- def initialize(super_method:, defs:, accessibility:, annotations: [])
65
+ def initialize(super_method:, defs:, accessibility:, annotations: [], alias_of:)
65
66
  @super_method = super_method
66
67
  @defs = defs
67
68
  @accessibility = accessibility
68
69
  @extra_annotations = annotations
70
+ @alias_of = alias_of
69
71
  end
70
72
 
71
73
  def defined_in
@@ -110,7 +112,8 @@ module RBS
110
112
  self.class.new(
111
113
  super_method: super_method&.sub(s),
112
114
  defs: defs.map {|defn| defn.update(type: defn.type.sub(s)) },
113
- accessibility: @accessibility
115
+ accessibility: @accessibility,
116
+ alias_of: alias_of
114
117
  )
115
118
  end
116
119
 
@@ -118,7 +121,8 @@ module RBS
118
121
  self.class.new(
119
122
  super_method: super_method&.map_type(&block),
120
123
  defs: defs.map {|defn| defn.update(type: defn.type.map_type(&block)) },
121
- accessibility: @accessibility
124
+ accessibility: @accessibility,
125
+ alias_of: alias_of
122
126
  )
123
127
  end
124
128
 
@@ -126,7 +130,8 @@ module RBS
126
130
  self.class.new(
127
131
  super_method: super_method,
128
132
  defs: defs.map {|defn| defn.update(type: yield(defn.type)) },
129
- accessibility: @accessibility
133
+ accessibility: @accessibility,
134
+ alias_of: alias_of
130
135
  )
131
136
  end
132
137
  end
@@ -599,10 +599,15 @@ module RBS
599
599
  try_cache(type_name, cache: one_instance_cache) do
600
600
  entry = env.class_decls[type_name]
601
601
 
602
+ param_names = entry.type_params.each.map(&:name)
602
603
  self_type = Types::ClassInstance.new(name: type_name,
603
- args: Types::Variable.build(entry.type_params.each.map(&:name)),
604
+ args: Types::Variable.build(param_names),
604
605
  location: nil)
605
- ancestors = [Definition::Ancestor::Instance.new(name: type_name, args: self_type.args)]
606
+ ancestors = Definition::InstanceAncestors.new(
607
+ type_name: type_name,
608
+ params: param_names,
609
+ ancestors: [Definition::Ancestor::Instance.new(name: type_name, args: self_type.args)]
610
+ )
606
611
 
607
612
  Definition.new(type_name: type_name, entry: entry, self_type: self_type, ancestors: ancestors).tap do |definition|
608
613
  method_definition_members(type_name, entry, kind: :instance).each do |method_name, array|
@@ -618,13 +623,15 @@ module RBS
618
623
  Definition::Method.new(
619
624
  super_method: nil,
620
625
  accessibility: visibility,
621
- defs: method_def.defs.map {|defn| defn.update(implemented_in: type_name) }
626
+ defs: method_def.defs.map {|defn| defn.update(implemented_in: type_name) },
627
+ alias_of: nil
622
628
  )
623
629
  else
624
630
  Definition::Method.new(
625
631
  super_method: nil,
626
632
  accessibility: visibility,
627
- defs: []
633
+ defs: [],
634
+ alias_of: nil
628
635
  )
629
636
  end
630
637
 
@@ -641,7 +648,8 @@ module RBS
641
648
  Definition::Method.new(
642
649
  super_method: nil,
643
650
  defs: defs + original.defs,
644
- accessibility: original.accessibility
651
+ accessibility: original.accessibility,
652
+ alias_of: nil
645
653
  )
646
654
  end
647
655
  end
@@ -676,7 +684,8 @@ module RBS
676
684
  implemented_in: type_name
677
685
  )
678
686
  ],
679
- accessibility: accessibility
687
+ accessibility: accessibility,
688
+ alias_of: nil
680
689
  )
681
690
  end
682
691
 
@@ -698,7 +707,8 @@ module RBS
698
707
  implemented_in: type_name
699
708
  ),
700
709
  ],
701
- accessibility: accessibility
710
+ accessibility: accessibility,
711
+ alias_of: nil
702
712
  )
703
713
  end
704
714
 
@@ -747,7 +757,15 @@ module RBS
747
757
  location: member.location
748
758
  )
749
759
 
750
- definition.methods[member.new_name] = definition.methods[member.old_name]
760
+ original_method = definition.methods[member.old_name]
761
+
762
+ definition.methods[member.new_name] = Definition::Method.new(
763
+ super_method: original_method.super_method,
764
+ defs: original_method.defs,
765
+ accessibility: original_method.accessibility,
766
+ alias_of: original_method,
767
+ annotations: original_method.annotations
768
+ )
751
769
  end
752
770
  end
753
771
  end
@@ -838,7 +856,10 @@ module RBS
838
856
  entry = env.class_decls[type_name]
839
857
 
840
858
  self_type = Types::ClassSingleton.new(name: type_name, location: nil)
841
- ancestors = [Definition::Ancestor::Singleton.new(name: type_name)]
859
+ ancestors = Definition::SingletonAncestors.new(
860
+ type_name: type_name,
861
+ ancestors: [Definition::Ancestor::Singleton.new(name: type_name)]
862
+ )
842
863
 
843
864
  Definition.new(type_name: type_name, entry: entry, self_type: self_type, ancestors: ancestors).tap do |definition|
844
865
  method_definition_members(type_name, entry, kind: :singleton).each do |method_name, array|
@@ -855,7 +876,8 @@ module RBS
855
876
  defs: method_def&.yield_self do |method_def|
856
877
  method_def.defs.map {|defn| defn.update(implemented_in: type_name) }
857
878
  end || [],
858
- accessibility: visibility
879
+ accessibility: visibility,
880
+ alias_of: nil
859
881
  )
860
882
  definition.methods[method_name] = members.inject(m) do |original, new|
861
883
  defs = new.types.map do |type|
@@ -869,7 +891,8 @@ module RBS
869
891
  Definition::Method.new(
870
892
  super_method: nil,
871
893
  defs: defs + original.defs,
872
- accessibility: original.accessibility
894
+ accessibility: original.accessibility,
895
+ alias_of: nil
873
896
  )
874
897
  end
875
898
  end
@@ -893,7 +916,14 @@ module RBS
893
916
  location: member.location
894
917
  )
895
918
 
896
- definition.methods[member.new_name] = definition.methods[member.old_name]
919
+ original_method = definition.methods[member.old_name]
920
+ definition.methods[member.new_name] = Definition::Method.new(
921
+ super_method: original_method.super_method,
922
+ defs: original_method.defs,
923
+ accessibility: original_method.accessibility,
924
+ alias_of: original_method,
925
+ annotations: original_method.annotations
926
+ )
897
927
  end
898
928
  end
899
929
  end
@@ -940,12 +970,13 @@ module RBS
940
970
  Definition::Method::TypeDef.new(
941
971
  type: method_type,
942
972
  member: initialize_def.member,
943
- defined_in: nil,
944
- implemented_in: nil
973
+ defined_in: initialize_def.defined_in,
974
+ implemented_in: initialize_def.implemented_in
945
975
  )
946
976
  end,
947
977
  accessibility: :public,
948
- annotations: [AST::Annotation.new(location: nil, string: "rbs:test:target")]
978
+ annotations: [AST::Annotation.new(location: nil, string: "rbs:test:target")],
979
+ alias_of: nil
949
980
  )
950
981
  end
951
982
  end
@@ -1029,7 +1060,8 @@ module RBS
1029
1060
  methods[name] = Definition::Method.new(
1030
1061
  super_method: super_method,
1031
1062
  accessibility: method.accessibility,
1032
- defs: sub.mapping.empty? ? defs : defs.map {|defn| defn.update(type: defn.type.sub(sub)) }
1063
+ defs: sub.mapping.empty? ? defs : defs.map {|defn| defn.update(type: defn.type.sub(sub)) },
1064
+ alias_of: nil
1033
1065
  )
1034
1066
  end
1035
1067
 
@@ -1123,7 +1155,8 @@ module RBS
1123
1155
  implemented_in: nil
1124
1156
  )
1125
1157
  end,
1126
- accessibility: :public
1158
+ accessibility: :public,
1159
+ alias_of: nil
1127
1160
  )
1128
1161
  definition.methods[member.name] = method
1129
1162
  end
@@ -1,156 +1,130 @@
1
1
  module RBS
2
2
  class EnvironmentLoader
3
- class UnknownLibraryNameError < StandardError
4
- attr_reader :name
3
+ class UnknownLibraryError < StandardError
4
+ attr_reader :library
5
5
 
6
- def initialize(name:)
7
- @name = name
8
- super "Cannot find a library or gem: `#{name}`"
6
+ def initialize(lib:)
7
+ @library = lib
8
+
9
+ super("Cannot find type definitions for library: #{lib.name} (#{lib.version || "[nil]"})")
9
10
  end
10
11
  end
11
12
 
12
- LibraryPath = _ = Struct.new(:name, :path, keyword_init: true)
13
- GemPath = _ = Struct.new(:name, :version, :path, keyword_init: true)
13
+ Library = _ = Struct.new(:name, :version, keyword_init: true)
14
+
15
+ attr_reader :core_root
16
+ attr_reader :repository
14
17
 
15
- attr_reader :paths
16
- attr_reader :stdlib_root
17
- attr_reader :gem_vendor_path
18
+ attr_reader :libs
19
+ attr_reader :dirs
18
20
 
19
- STDLIB_ROOT = Pathname(_ = __dir__) + "../../stdlib"
21
+ DEFAULT_CORE_ROOT = Pathname(_ = __dir__) + "../../core"
20
22
 
21
23
  def self.gem_sig_path(name, version)
22
- Pathname(Gem::Specification.find_by_name(name, version).gem_dir) + "sig"
24
+ spec = Gem::Specification.find_by_name(name, version)
25
+ path = Pathname(spec.gem_dir) + "sig"
26
+ if path.directory?
27
+ [spec, path]
28
+ end
23
29
  rescue Gem::MissingSpecError
24
30
  nil
25
31
  end
26
32
 
27
- def initialize(stdlib_root: STDLIB_ROOT, gem_vendor_path: nil)
28
- @stdlib_root = stdlib_root
29
- @gem_vendor_path = gem_vendor_path
30
- @paths = []
31
- @no_builtin = false
33
+ def initialize(core_root: DEFAULT_CORE_ROOT, repository: Repository.new)
34
+ @core_root = core_root
35
+ @repository = repository
36
+
37
+ @libs = []
38
+ @dirs = []
32
39
  end
33
40
 
34
- def add(path: nil, library: nil)
41
+ def add(path: nil, library: nil, version: nil)
35
42
  case
36
43
  when path
37
- @paths << path
44
+ dirs << path
38
45
  when library
39
- name, version = self.class.parse_library(library)
40
-
41
- case
42
- when !version && path = stdlib?(name)
43
- @paths << LibraryPath.new(name: name, path: path)
44
- when (path = gem?(name, version))
45
- @paths << GemPath.new(name: name, version: version, path: path)
46
- else
47
- raise UnknownLibraryNameError.new(name: library)
48
- end
46
+ libs << Library.new(name: library, version: version)
49
47
  end
50
48
  end
51
49
 
52
- def self.parse_library(lib)
53
- _ = lib.split(/:/)
50
+ def has_library?(library:, version:)
51
+ self.class.gem_sig_path(library, version) || repository.lookup(library, version)
54
52
  end
55
53
 
56
- def stdlib?(name)
57
- path = stdlib_root + name
58
- if path.directory?
59
- path
54
+ def load(env:)
55
+ # @type var loaded: Array[[AST::Declarations::t, Pathname, source]]
56
+ loaded = []
57
+
58
+ each_decl do |decl, buf, source, path|
59
+ env << decl
60
+ loaded << [decl, path, source]
60
61
  end
62
+
63
+ loaded
61
64
  end
62
65
 
63
- def gem?(name, version)
64
- if path = gem_vendor_path
65
- # Try vendored RBS first
66
- gem_dir = path + name
67
- if gem_dir.directory?
68
- return gem_dir
69
- end
66
+ def each_dir
67
+ if root = core_root
68
+ yield :core, root
70
69
  end
71
70
 
72
- # Try ruby gem library
73
- self.class.gem_sig_path(name, version)
74
- end
71
+ libs.each do |lib|
72
+ unless has_library?(version: lib.version, library: lib.name)
73
+ raise UnknownLibraryError.new(lib: lib)
74
+ end
75
75
 
76
- def each_signature(path, immediate: true, &block)
77
- if block
78
76
  case
79
- when path.file?
80
- if path.extname == ".rbs" || immediate
81
- yield path
82
- end
83
- when path.directory?
84
- path.children.each do |child|
85
- each_signature child, immediate: false, &block
86
- end
77
+ when from_gem = self.class.gem_sig_path(lib.name, lib.version)
78
+ yield lib, from_gem[1]
79
+ when from_repo = repository.lookup(lib.name, lib.version)
80
+ yield lib, from_repo
87
81
  end
88
- else
89
- enum_for :each_signature, path, immediate: immediate
90
82
  end
91
- end
92
83
 
93
- def each_library_path
94
- paths.each do |path|
95
- case path
96
- when Pathname
97
- yield path, path
98
- when LibraryPath
99
- yield path, path.path
100
- when GemPath
101
- yield path, path.path
102
- end
84
+ dirs.each do |dir|
85
+ yield dir, dir
103
86
  end
104
87
  end
105
88
 
106
- def no_builtin!(skip = true)
107
- @no_builtin = skip
108
- self
109
- end
110
-
111
- def no_builtin?
112
- @no_builtin
113
- end
114
-
115
- def each_decl
116
- if block_given?
117
- # @type var signature_files: Array[[path | :stdlib, Pathname]]
118
- signature_files = []
119
-
120
- unless no_builtin?
121
- each_signature(stdlib_root + "builtin") do |path|
122
- signature_files << [:stdlib, path]
123
- end
89
+ def each_file(path, immediate:, skip_hidden:, &block)
90
+ case
91
+ when path.file?
92
+ if path.extname == ".rbs" || immediate
93
+ yield path
124
94
  end
125
95
 
126
- each_library_path do |library_path, pathname|
127
- each_signature(pathname) do |path|
128
- signature_files << [library_path, path]
96
+ when path.directory?
97
+ if path.basename.to_s.start_with?("_")
98
+ if skip_hidden
99
+ unless immediate
100
+ return
101
+ end
129
102
  end
130
103
  end
131
104
 
132
- signature_files.each do |lib_path, file_path|
133
- buffer = Buffer.new(name: file_path.to_s, content: file_path.read)
134
- Parser.parse_signature(buffer).each do |decl|
135
- yield decl, buffer, file_path, lib_path
136
- end
105
+ path.each_child do |child|
106
+ each_file(child, immediate: false, skip_hidden: skip_hidden, &block)
137
107
  end
138
- else
139
- enum_for :each_decl
140
108
  end
141
109
  end
142
110
 
143
- def load(env:)
144
- # @type var loadeds: Array[[AST::Declarations::t, Pathname, path | :stdlib]]
145
- loadeds = []
111
+ def each_decl
112
+ files = Set[]
146
113
 
147
- each_decl do |decl, buffer, file_path, lib_path|
148
- env.buffers.push(buffer)
149
- env << decl
150
- loadeds << [decl, file_path, lib_path]
151
- end
114
+ each_dir do |source, dir|
115
+ skip_hidden = !source.is_a?(Pathname)
116
+
117
+ each_file(dir, skip_hidden: skip_hidden, immediate: true) do |path|
118
+ next if files.include?(path)
152
119
 
153
- loadeds
120
+ files << path
121
+ buffer = Buffer.new(name: path.to_s, content: path.read(encoding: "UTF-8"))
122
+
123
+ Parser.parse_signature(buffer).each do |decl|
124
+ yield decl, buffer, source, path
125
+ end
126
+ end
127
+ end
154
128
  end
155
129
  end
156
130
  end
@@ -1,5 +1,9 @@
1
1
  module RBS
2
2
  class EnvironmentWalker
3
+ InstanceNode = Struct.new(:type_name, keyword_init: true)
4
+ SingletonNode = Struct.new(:type_name, keyword_init: true)
5
+ TypeNameNode = Struct.new(:type_name, keyword_init: true)
6
+
3
7
  attr_reader :env
4
8
 
5
9
  def initialize(env:)
@@ -23,54 +27,80 @@ module RBS
23
27
  include TSort
24
28
 
25
29
  def tsort_each_node(&block)
26
- env.class_decls.each_key(&block)
27
- env.interface_decls.each_key(&block)
28
- env.alias_decls.each_key(&block)
30
+ env.class_decls.each_key do |type_name|
31
+ yield InstanceNode.new(type_name: type_name)
32
+ yield SingletonNode.new(type_name: type_name)
33
+ end
34
+ env.interface_decls.each_key do |type_name|
35
+ yield TypeNameNode.new(type_name: type_name)
36
+ end
37
+ env.alias_decls.each_key do |type_name|
38
+ yield TypeNameNode.new(type_name: type_name)
39
+ end
29
40
  end
30
41
 
31
- def tsort_each_child(name, &block)
42
+ def tsort_each_child(node, &block)
43
+ name = node.type_name
44
+
32
45
  unless name.namespace.empty?
33
- yield name.namespace.to_type_name
46
+ yield SingletonNode.new(type_name: name.namespace.to_type_name)
34
47
  end
35
48
 
36
- case
37
- when name.class?, name.interface?
38
- definitions = []
39
-
49
+ case node
50
+ when TypeNameNode
40
51
  case
41
- when name.class?
42
- definitions << builder.build_instance(name)
43
- definitions << builder.build_singleton(name)
44
52
  when name.interface?
45
- definitions << builder.build_interface(name)
53
+ definition = builder.build_interface(name)
54
+ unless only_ancestors?
55
+ definition.each_type do |type|
56
+ each_type_node type, &block
57
+ end
58
+ end
59
+ when name.alias?
60
+ each_type_node builder.expand_alias(name), &block
61
+ else
62
+ raise "Unexpected TypeNameNode with type_name=#{name}"
46
63
  end
47
64
 
48
- definitions.each do |definition|
49
- if ancestors = definition.ancestors
50
- ancestors.ancestors.each do |ancestor|
51
- yield ancestor.name
65
+ when InstanceNode, SingletonNode
66
+ definition = if node.is_a?(InstanceNode)
67
+ builder.build_instance(name)
68
+ else
69
+ builder.build_singleton(name)
70
+ end
52
71
 
53
- case ancestor
54
- when Definition::Ancestor::Instance
72
+ if ancestors = definition.ancestors
73
+ ancestors.ancestors.each do |ancestor|
74
+ case ancestor
75
+ when Definition::Ancestor::Instance
76
+ yield InstanceNode.new(type_name: ancestor.name)
77
+
78
+ unless only_ancestors?
55
79
  ancestor.args.each do |type|
56
- each_type_name type, &block
80
+ each_type_node type, &block
57
81
  end
58
82
  end
83
+ when Definition::Ancestor::Singleton
84
+ yield SingletonNode.new(type_name: ancestor.name)
59
85
  end
60
86
  end
87
+ end
61
88
 
62
- unless only_ancestors?
63
- definition.each_type do |type|
64
- each_type_name type, &block
65
- end
89
+ unless only_ancestors?
90
+ definition.each_type do |type|
91
+ each_type_node type, &block
66
92
  end
67
93
  end
68
- when name.alias?
69
- each_type_name builder.expand_alias(name), &block
70
94
  end
71
95
  end
72
96
 
73
97
  def each_type_name(type, &block)
98
+ each_type_node(type) do |node|
99
+ yield node.type_name
100
+ end
101
+ end
102
+
103
+ def each_type_node(type, &block)
74
104
  case type
75
105
  when RBS::Types::Bases::Any
76
106
  when RBS::Types::Bases::Class
@@ -83,29 +113,34 @@ module RBS
83
113
  when RBS::Types::Bases::Nil
84
114
  when RBS::Types::Variable
85
115
  when RBS::Types::ClassSingleton
86
- yield type.name
87
- when RBS::Types::ClassInstance, RBS::Types::Interface
88
- yield type.name
116
+ yield SingletonNode.new(type_name: type.name)
117
+ when RBS::Types::ClassInstance
118
+ yield InstanceNode.new(type_name: type.name)
119
+ type.args.each do |ty|
120
+ each_type_node(ty, &block)
121
+ end
122
+ when RBS::Types::Interface
123
+ yield TypeNameNode.new(type_name: type.name)
89
124
  type.args.each do |ty|
90
- each_type_name(ty, &block)
125
+ each_type_node(ty, &block)
91
126
  end
92
127
  when RBS::Types::Alias
93
- yield type.name
128
+ yield TypeNameNode.new(type_name: type.name)
94
129
  when RBS::Types::Union, RBS::Types::Intersection, RBS::Types::Tuple
95
130
  type.types.each do |ty|
96
- each_type_name ty, &block
131
+ each_type_node ty, &block
97
132
  end
98
133
  when RBS::Types::Optional
99
- each_type_name type.type, &block
134
+ each_type_node type.type, &block
100
135
  when RBS::Types::Literal
101
136
  # nop
102
137
  when RBS::Types::Record
103
138
  type.fields.each_value do |ty|
104
- each_type_name ty, &block
139
+ each_type_node ty, &block
105
140
  end
106
141
  when RBS::Types::Proc
107
142
  type.each_type do |ty|
108
- each_type_name ty, &block
143
+ each_type_node ty, &block
109
144
  end
110
145
  else
111
146
  raise "Unexpected type given: #{type}"