rbs 2.8.4 → 3.0.0.dev.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +0 -3
  3. data/Gemfile +1 -1
  4. data/Gemfile.lock +17 -17
  5. data/README.md +1 -0
  6. data/Rakefile +66 -0
  7. data/core/array.rbs +1 -1
  8. data/core/builtin.rbs +1 -1
  9. data/core/hash.rbs +1 -1
  10. data/core/module.rbs +1 -1
  11. data/ext/rbs_extension/constants.c +18 -2
  12. data/ext/rbs_extension/constants.h +9 -1
  13. data/ext/rbs_extension/lexer.c +834 -777
  14. data/ext/rbs_extension/lexer.h +3 -1
  15. data/ext/rbs_extension/lexer.re +3 -1
  16. data/ext/rbs_extension/lexstate.c +4 -2
  17. data/ext/rbs_extension/parser.c +287 -57
  18. data/ext/rbs_extension/ruby_objs.c +71 -5
  19. data/ext/rbs_extension/ruby_objs.h +9 -2
  20. data/lib/rbs/annotate/rdoc_annotator.rb +1 -1
  21. data/lib/rbs/ast/declarations.rb +49 -2
  22. data/lib/rbs/ast/directives.rb +39 -0
  23. data/lib/rbs/ast/members.rb +49 -15
  24. data/lib/rbs/cli.rb +38 -19
  25. data/lib/rbs/collection/config/lockfile.rb +115 -0
  26. data/lib/rbs/collection/config/lockfile_generator.rb +99 -53
  27. data/lib/rbs/collection/config.rb +12 -40
  28. data/lib/rbs/collection/installer.rb +9 -13
  29. data/lib/rbs/collection/sources/base.rb +2 -2
  30. data/lib/rbs/collection/sources/git.rb +135 -62
  31. data/lib/rbs/collection/sources/rubygems.rb +10 -12
  32. data/lib/rbs/collection/sources/stdlib.rb +10 -13
  33. data/lib/rbs/collection/sources.rb +7 -1
  34. data/lib/rbs/collection.rb +1 -0
  35. data/lib/rbs/definition.rb +1 -1
  36. data/lib/rbs/definition_builder/ancestor_builder.rb +24 -8
  37. data/lib/rbs/definition_builder/method_builder.rb +3 -3
  38. data/lib/rbs/definition_builder.rb +456 -579
  39. data/lib/rbs/environment/use_map.rb +77 -0
  40. data/lib/rbs/environment.rb +356 -85
  41. data/lib/rbs/environment_loader.rb +20 -17
  42. data/lib/rbs/environment_walker.rb +1 -1
  43. data/lib/rbs/errors.rb +34 -37
  44. data/lib/rbs/locator.rb +3 -3
  45. data/lib/rbs/parser_aux.rb +8 -6
  46. data/lib/rbs/prototype/helpers.rb +29 -13
  47. data/lib/rbs/prototype/node_usage.rb +99 -0
  48. data/lib/rbs/prototype/rb.rb +3 -2
  49. data/lib/rbs/prototype/rbi.rb +6 -4
  50. data/lib/rbs/prototype/runtime.rb +25 -12
  51. data/lib/rbs/resolver/constant_resolver.rb +23 -7
  52. data/lib/rbs/resolver/type_name_resolver.rb +2 -1
  53. data/lib/rbs/sorter.rb +3 -3
  54. data/lib/rbs/substitution.rb +19 -0
  55. data/lib/rbs/test/setup.rb +1 -1
  56. data/lib/rbs/type_alias_dependency.rb +1 -1
  57. data/lib/rbs/type_alias_regularity.rb +3 -3
  58. data/lib/rbs/types.rb +1 -5
  59. data/lib/rbs/validator.rb +25 -3
  60. data/lib/rbs/variance_calculator.rb +2 -2
  61. data/lib/rbs/version.rb +1 -1
  62. data/lib/rbs/writer.rb +54 -19
  63. data/lib/rbs.rb +3 -2
  64. data/lib/rdoc_plugin/parser.rb +3 -3
  65. data/schema/members.json +15 -10
  66. data/sig/ancestor_graph.rbs +22 -2
  67. data/sig/collection/config/lockfile.rbs +80 -0
  68. data/sig/collection/config/lockfile_generator.rbs +53 -0
  69. data/sig/collection/config.rbs +5 -48
  70. data/sig/collection/installer.rbs +1 -1
  71. data/sig/collection/sources.rbs +76 -33
  72. data/sig/constant.rbs +1 -1
  73. data/sig/declarations.rbs +36 -3
  74. data/sig/definition.rbs +1 -1
  75. data/sig/definition_builder.rbs +94 -82
  76. data/sig/directives.rbs +61 -0
  77. data/sig/environment.rbs +150 -28
  78. data/sig/environment_loader.rbs +2 -2
  79. data/sig/errors.rbs +42 -0
  80. data/sig/members.rbs +31 -7
  81. data/sig/parser.rbs +8 -15
  82. data/sig/prototype/node_usage.rbs +20 -0
  83. data/sig/resolver/constant_resolver.rbs +1 -2
  84. data/sig/shims/bundler.rbs +31 -0
  85. data/sig/shims/rubygems.rbs +15 -0
  86. data/sig/shims.rbs +0 -22
  87. data/sig/substitution.rbs +6 -0
  88. data/sig/use_map.rbs +35 -0
  89. data/sig/validator.rbs +12 -5
  90. data/sig/writer.rbs +6 -2
  91. metadata +16 -9
  92. data/lib/rbs/constant_table.rb +0 -167
  93. data/lib/rbs/type_name_resolver.rb +0 -67
  94. data/sig/constant_table.rbs +0 -30
  95. data/sig/type_name_resolver.rbs +0 -26
@@ -60,25 +60,26 @@ module RBS
60
60
 
61
61
  def resolve_dependencies(library:, version:)
62
62
  [Collection::Sources::Rubygems.instance, Collection::Sources::Stdlib.instance].each do |source|
63
- # @type var gem: { 'name' => String, 'version' => String? }
64
- gem = { 'name' => library, 'version' => version }
65
- next unless source.has?(gem)
63
+ next unless source.has?(library, version)
66
64
 
67
- gem['version'] ||= source.versions(gem).last
68
- source.dependencies_of(gem)&.each do |dep|
65
+ unless version
66
+ version = source.versions(library).last or raise
67
+ end
68
+
69
+ source.dependencies_of(library, version)&.each do |dep|
69
70
  add(library: dep['name'], version: nil)
70
71
  end
71
72
  return
72
73
  end
73
74
  end
74
75
 
75
- def add_collection(collection_config)
76
- collection_config.check_rbs_availability!
76
+ def add_collection(lockfile)
77
+ lockfile.check_rbs_availability!
77
78
 
78
- repository.add(collection_config.repo_path)
79
+ repository.add(lockfile.fullpath)
79
80
 
80
- collection_config.gems.each do |gem|
81
- add(library: gem['name'], version: gem['version'], resolve_dependencies: false)
81
+ lockfile.gems.each_value do |gem|
82
+ add(library: gem[:name], version: gem[:version], resolve_dependencies: false)
82
83
  end
83
84
  end
84
85
 
@@ -94,9 +95,11 @@ module RBS
94
95
  # @type var loaded: Array[[AST::Declarations::t, Pathname, source]]
95
96
  loaded = []
96
97
 
97
- each_decl do |decl, buf, source, path|
98
- env << decl
99
- loaded << [decl, path, source]
98
+ each_signature do |source, path, buffer, decls, dirs|
99
+ decls.each do |decl|
100
+ loaded << [decl, path, source]
101
+ end
102
+ env.add_signature(buffer: buffer, directives: dirs, decls: decls)
100
103
  end
101
104
 
102
105
  loaded
@@ -147,7 +150,7 @@ module RBS
147
150
  end
148
151
  end
149
152
 
150
- def each_decl
153
+ def each_signature
151
154
  files = Set[]
152
155
 
153
156
  each_dir do |source, dir|
@@ -159,9 +162,9 @@ module RBS
159
162
  files << path
160
163
  buffer = Buffer.new(name: path.to_s, content: path.read(encoding: "UTF-8"))
161
164
 
162
- Parser.parse_signature(buffer).each do |decl|
163
- yield decl, buffer, source, path
164
- end
165
+ _, dirs, decls = Parser.parse_signature(buffer)
166
+
167
+ yield source, path, buffer, decls, dirs
165
168
  end
166
169
  end
167
170
  end
@@ -36,7 +36,7 @@ module RBS
36
36
  env.interface_decls.each_key do |type_name|
37
37
  yield TypeNameNode.new(type_name: type_name)
38
38
  end
39
- env.alias_decls.each_key do |type_name|
39
+ env.type_alias_decls.each_key do |type_name|
40
40
  yield TypeNameNode.new(type_name: type_name)
41
41
  end
42
42
  end
data/lib/rbs/errors.rb CHANGED
@@ -119,19 +119,7 @@ module RBS
119
119
  end
120
120
 
121
121
  def self.check!(type_name, env:, location:)
122
- dic = case
123
- when type_name.class?
124
- env.class_decls
125
- when type_name.alias?
126
- env.alias_decls
127
- when type_name.interface?
128
- env.interface_decls
129
- else
130
- raise
131
- end
132
-
133
- dic.key?(type_name) or raise new(type_name: type_name, location: location)
134
-
122
+ env.type_name?(type_name) or raise new(type_name: type_name, location: location)
135
123
  type_name
136
124
  end
137
125
  end
@@ -148,7 +136,7 @@ module RBS
148
136
  end
149
137
 
150
138
  def self.check!(type_name, env:, location:)
151
- if decl = env.class_decls[type_name]
139
+ if env.module_name?(type_name)
152
140
  return
153
141
  end
154
142
 
@@ -166,7 +154,7 @@ module RBS
166
154
  end
167
155
 
168
156
  def self.check!(super_decl, env:)
169
- return if env.class_decls[super_decl.name].is_a?(Environment::ClassEntry)
157
+ return if env.class_decl?(super_decl.name) || env.class_alias?(super_decl.name)
170
158
 
171
159
  raise new(super_decl)
172
160
  end
@@ -186,16 +174,7 @@ module RBS
186
174
  def self.check!(self_type, env:)
187
175
  type_name = self_type.name
188
176
 
189
- dic = case
190
- when type_name.class?
191
- env.class_decls
192
- when type_name.interface?
193
- env.interface_decls
194
- else
195
- raise
196
- end
197
-
198
- dic.key?(type_name) or raise new(type_name: type_name, location: self_type.location)
177
+ (env.module_name?(type_name) || env.interface_name?(type_name)) or raise new(type_name: type_name, location: self_type.location)
199
178
  end
200
179
  end
201
180
 
@@ -215,16 +194,7 @@ module RBS
215
194
  end
216
195
 
217
196
  def self.check!(type_name, env:, member:)
218
- dic = case
219
- when type_name.class?
220
- env.class_decls
221
- when type_name.interface?
222
- env.interface_decls
223
- else
224
- raise
225
- end
226
-
227
- dic.key?(type_name) or raise new(type_name: type_name, member: member)
197
+ (env.module_name?(type_name) || env.interface_name?(type_name)) or raise new(type_name: type_name, member: member)
228
198
  end
229
199
  end
230
200
 
@@ -416,8 +386,7 @@ module RBS
416
386
  end
417
387
 
418
388
  def self.check!(type_name:, env:, member:)
419
- case env.class_decls[member.name]
420
- when Environment::ClassEntry
389
+ if env.class_decl?(member.name)
421
390
  raise new(type_name: type_name, member: member)
422
391
  end
423
392
  end
@@ -478,4 +447,32 @@ module RBS
478
447
  super "#{Location.to_string(location)}: Cyclic type parameter bound is prohibited"
479
448
  end
480
449
  end
450
+
451
+ class InconsistentClassModuleAliasError < BaseError
452
+ attr_reader :alias_entry
453
+
454
+ def initialize(entry)
455
+ @alias_entry = entry
456
+
457
+ expected_kind, actual_kind =
458
+ case entry
459
+ when Environment::ModuleAliasEntry
460
+ ["module", "class"]
461
+ when Environment::ClassAliasEntry
462
+ ["class", "module"]
463
+ end
464
+
465
+ super "#{Location.to_string(entry.decl.location&.[](:old_name))}: A #{expected_kind} `#{entry.decl.new_name}` cannot be an alias of a #{actual_kind} `#{entry.decl.old_name}`"
466
+ end
467
+ end
468
+
469
+ class CyclicClassAliasDefinitionError < BaseError
470
+ attr_reader :alias_entry
471
+
472
+ def initialize(entry)
473
+ @alias_entry = entry
474
+
475
+ super "#{Location.to_string(entry.decl.location&.[](:old_name))}: A #{alias_entry.decl.new_name} is a cyclic definition"
476
+ end
477
+ end
481
478
  end
data/lib/rbs/locator.rb CHANGED
@@ -95,7 +95,7 @@ module RBS
95
95
  when AST::Declarations::Constant, AST::Declarations::Global
96
96
  find_in_type(pos, array: array, type: decl.type) and return true
97
97
 
98
- when AST::Declarations::Alias
98
+ when AST::Declarations::TypeAlias
99
99
  find_in_type(pos, array: array, type: decl.type) and return true
100
100
  end
101
101
 
@@ -113,8 +113,8 @@ module RBS
113
113
 
114
114
  case member
115
115
  when AST::Members::MethodDefinition
116
- member.types.each do |method_type|
117
- find_in_method_type(pos, array: array, method_type: method_type) and return true
116
+ member.overloads.each do |overload|
117
+ find_in_method_type(pos, array: array, method_type: overload.method_type) and return true
118
118
  end
119
119
  when AST::Members::InstanceVariable, AST::Members::ClassInstanceVariable, AST::Members::ClassVariable
120
120
  find_in_type(pos, array: array, type: member.type) and return true
@@ -2,19 +2,21 @@
2
2
 
3
3
  module RBS
4
4
  class Parser
5
- def self.parse_type(source, line: nil, column: nil, range: nil, variables: [])
5
+ def self.parse_type(source, range: 0..., variables: [])
6
6
  buf = buffer(source)
7
- _parse_type(buf, range&.begin || 0, range&.end || buf.last_position, variables, range.nil?)
7
+ _parse_type(buf, range.begin || 0, range.end || buf.last_position, variables)
8
8
  end
9
9
 
10
- def self.parse_method_type(source, line: nil, column: nil, range: nil, variables: [])
10
+ def self.parse_method_type(source, range: 0..., variables: [])
11
11
  buf = buffer(source)
12
- _parse_method_type(buf, range&.begin || 0, range&.end || buf.last_position, variables, range.nil?)
12
+ _parse_method_type(buf, range.begin || 0, range.end || buf.last_position, variables)
13
13
  end
14
14
 
15
- def self.parse_signature(source, line: nil, column: nil)
15
+ def self.parse_signature(source)
16
16
  buf = buffer(source)
17
- _parse_signature(buf, buf.last_position)
17
+ dirs, decls = _parse_signature(buf, buf.last_position)
18
+
19
+ [buf, dirs, decls]
18
20
  end
19
21
 
20
22
  def self.buffer(source)
@@ -7,27 +7,43 @@ module RBS
7
7
 
8
8
  def block_from_body(node)
9
9
  _, args_node, body_node = node.children
10
+ _pre_num, _pre_init, _opt, _first_post, _post_num, _post_init, _rest, _kw, _kwrest, block_var = args_from_node(args_node)
10
11
 
11
- _pre_num, _pre_init, _opt, _first_post, _post_num, _post_init, _rest, _kw, _kwrest, block = args_from_node(args_node)
12
+ # @type var body_node: node?
13
+ if body_node
14
+ yields = any_node?(body_node) {|n| n.type == :YIELD }
15
+ end
16
+
17
+ if yields || block_var
18
+ required = true
19
+
20
+ if body_node
21
+ if any_node?(body_node) {|n| n.type == :FCALL && n.children[0] == :block_given? && !n.children[1] }
22
+ required = false
23
+ end
24
+ end
25
+
26
+ if _rest == :* && block_var == :&
27
+ # ... is given
28
+ required = false
29
+ end
12
30
 
13
- method_block = nil
31
+ if block_var
32
+ if body_node
33
+ usage = NodeUsage.new(body_node)
34
+ if usage.each_conditional_node.any? {|n| n.type == :LVAR && n.children[0] == block_var }
35
+ required = false
36
+ end
37
+ end
38
+ end
14
39
 
15
- if block
16
40
  method_block = Types::Block.new(
17
- required: false,
41
+ required: required,
18
42
  type: Types::Function.empty(untyped),
19
43
  self_type: nil
20
44
  )
21
- end
22
-
23
- if body_node
24
- if (yields = any_node?(body_node) {|n| n.type == :YIELD })
25
- method_block = Types::Block.new(
26
- required: true,
27
- type: Types::Function.empty(untyped),
28
- self_type: nil
29
- )
30
45
 
46
+ if yields
31
47
  yields.each do |yield_node|
32
48
  array_content = yield_node.children[0]&.children&.compact || []
33
49
 
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RBS
4
+ module Prototype
5
+ class NodeUsage
6
+ include Helpers
7
+
8
+ attr_reader :conditional_nodes
9
+
10
+ def initialize(node)
11
+ @node = node
12
+ @conditional_nodes = Set[].compare_by_identity
13
+
14
+ calculate(node, conditional: false)
15
+ end
16
+
17
+ def each_conditional_node(&block)
18
+ if block
19
+ conditional_nodes.each(&block)
20
+ else
21
+ conditional_nodes.each
22
+ end
23
+ end
24
+
25
+ def calculate(node, conditional:)
26
+ if conditional
27
+ conditional_nodes << node
28
+ end
29
+
30
+ case node.type
31
+ when :IF, :UNLESS
32
+ cond_node, true_node, false_node = node.children
33
+ calculate(cond_node, conditional: true)
34
+ calculate(true_node, conditional: conditional) if true_node
35
+ calculate(false_node, conditional: conditional) if false_node
36
+ when :AND, :OR
37
+ left, right = node.children
38
+ calculate(left, conditional: true)
39
+ calculate(right, conditional: conditional)
40
+ when :QCALL
41
+ receiver, _, args = node.children
42
+ calculate(receiver, conditional: true)
43
+ calculate(args, conditional: false) if args
44
+ when :WHILE
45
+ cond, body = node.children
46
+ calculate(cond, conditional: true)
47
+ calculate(body, conditional: false) if body
48
+ when :OP_ASGN_OR, :OP_ASGN_AND
49
+ var, _, asgn = node.children
50
+ calculate(var, conditional: true)
51
+ calculate(asgn, conditional: conditional)
52
+ when :LASGN, :IASGN, :GASGN
53
+ _, lhs = node.children
54
+ calculate(lhs, conditional: conditional) if lhs
55
+ when :MASGN
56
+ lhs, _ = node.children
57
+ calculate(lhs, conditional: conditional)
58
+ when :CDECL
59
+ if node.children.size == 2
60
+ _, lhs = node.children
61
+ calculate(lhs, conditional: conditional)
62
+ else
63
+ const, _, lhs = node.children
64
+ calculate(const, conditional: false)
65
+ calculate(lhs, conditional: conditional)
66
+ end
67
+ when :SCOPE
68
+ _, _, body = node.children
69
+ calculate(body, conditional: conditional)
70
+ when :CASE2
71
+ _, *branches = node.children
72
+ branches.each do |branch|
73
+ if branch.type == :WHEN
74
+ list, body = branch.children
75
+ list.children.each do |child|
76
+ if child
77
+ calculate(child, conditional: true)
78
+ end
79
+ end
80
+ calculate(body, conditional: conditional)
81
+ else
82
+ calculate(branch, conditional: conditional)
83
+ end
84
+ end
85
+ when :BLOCK
86
+ *nodes, last = node.children
87
+ nodes.each do |no|
88
+ calculate(no, conditional: false)
89
+ end
90
+ calculate(last, conditional: conditional) if last
91
+ else
92
+ each_child(node) do |child|
93
+ calculate(child, conditional: false)
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
@@ -186,10 +186,11 @@ module RBS
186
186
  name: def_name,
187
187
  location: nil,
188
188
  annotations: [],
189
- types: types,
189
+ overloads: types.map {|type| AST::Members::MethodDefinition::Overload.new(annotations: [], method_type: type )},
190
190
  kind: kind,
191
191
  comment: comments[node.first_lineno - 1],
192
- overload: false
192
+ overloading: false,
193
+ visibility: nil
193
194
  )
194
195
 
195
196
  decls.push member unless decls.include?(member)
@@ -185,10 +185,11 @@ module RBS
185
185
  name: node.children[1],
186
186
  location: nil,
187
187
  annotations: [],
188
- types: types,
188
+ overloads: types.map {|type| AST::Members::MethodDefinition::Overload.new(annotations: [], method_type: type) },
189
189
  kind: :singleton,
190
190
  comment: comment,
191
- overload: false
191
+ overloading: false,
192
+ visibility: nil
192
193
  )
193
194
  end
194
195
 
@@ -205,10 +206,11 @@ module RBS
205
206
  name: node.children[0],
206
207
  location: nil,
207
208
  annotations: [],
208
- types: types,
209
+ overloads: types.map {|type| AST::Members::MethodDefinition::Overload.new(annotations: [], method_type: type) },
209
210
  kind: :instance,
210
211
  comment: comment,
211
- overload: false
212
+ overloading: false,
213
+ visibility: nil
212
214
  )
213
215
  end
214
216
 
@@ -187,18 +187,22 @@ module RBS
187
187
  if method
188
188
  members << AST::Members::MethodDefinition.new(
189
189
  name: method_name,
190
- types: method.method_types.map {|type|
191
- type.update.tap do |ty|
192
- def ty.to_s
193
- location.source
190
+ overloads: method.method_types.map {|type|
191
+ AST::Members::MethodDefinition::Overload.new(
192
+ annotations: [],
193
+ method_type: type.update.tap do |ty|
194
+ def ty.to_s
195
+ location.source
196
+ end
194
197
  end
195
- end
198
+ )
196
199
  },
197
200
  kind: kind,
198
201
  location: nil,
199
202
  comment: method.comments[0],
200
203
  annotations: method.annotations,
201
- overload: false
204
+ overloading: false,
205
+ visibility: nil
202
206
  )
203
207
  return
204
208
  end
@@ -231,12 +235,15 @@ module RBS
231
235
 
232
236
  members << AST::Members::MethodDefinition.new(
233
237
  name: method.name,
234
- types: [method_type(method)],
238
+ overloads: [
239
+ AST::Members::MethodDefinition::Overload.new(annotations: [], method_type: method_type(method))
240
+ ],
235
241
  kind: :singleton,
236
242
  location: nil,
237
243
  comment: nil,
238
244
  annotations: [],
239
- overload: false
245
+ overloading: false,
246
+ visibility: nil
240
247
  )
241
248
  end
242
249
  else
@@ -264,12 +271,15 @@ module RBS
264
271
 
265
272
  members << AST::Members::MethodDefinition.new(
266
273
  name: method.name,
267
- types: [method_type(method)],
274
+ overloads: [
275
+ AST::Members::MethodDefinition::Overload.new(annotations: [], method_type: method_type(method))
276
+ ],
268
277
  kind: :instance,
269
278
  location: nil,
270
279
  comment: nil,
271
280
  annotations: [],
272
- overload: false
281
+ overloading: false,
282
+ visibility: nil
273
283
  )
274
284
  end
275
285
  else
@@ -298,12 +308,15 @@ module RBS
298
308
 
299
309
  members << AST::Members::MethodDefinition.new(
300
310
  name: method.name,
301
- types: [method_type(method)],
311
+ overloads: [
312
+ AST::Members::MethodDefinition::Overload.new(annotations: [], method_type: method_type(method))
313
+ ],
302
314
  kind: :instance,
303
315
  location: nil,
304
316
  comment: nil,
305
317
  annotations: [],
306
- overload: false
318
+ overloading: false,
319
+ visibility: nil
307
320
  )
308
321
  end
309
322
  else
@@ -18,20 +18,33 @@ module RBS
18
18
  end
19
19
 
20
20
  environment.class_decls.each do |name, entry|
21
+ constant = constant_of_module(name, entry)
22
+
21
23
  unless name.namespace.empty?
22
24
  parent = name.namespace.to_type_name
23
-
24
25
  table = children_table[parent] or raise
25
- constant = constant_of_module(name, entry)
26
26
  else
27
27
  table = toplevel
28
- constant = constant_of_module(name, entry)
29
28
  end
30
29
 
31
30
  table[name.name] = constant
32
31
  constants_table[name] = constant
33
32
  end
34
33
 
34
+ environment.class_alias_decls.each do |name, entry|
35
+ normalized_entry = environment.normalized_module_class_entry(name) or next
36
+ constant = constant_of_module(name, normalized_entry)
37
+
38
+ # Insert class/module aliases into `children_table` and `toplevel` table
39
+ unless name.namespace.empty?
40
+ normalized_parent = environment.normalize_module_name?(name.namespace.to_type_name) or raise
41
+ table = children_table[normalized_parent] or raise
42
+ table[name.name] = constant
43
+ else
44
+ toplevel[name.name] = constant
45
+ end
46
+ end
47
+
35
48
  environment.constant_decls.each do |name, entry|
36
49
  unless name.namespace.empty?
37
50
  parent = name.namespace.to_type_name
@@ -97,6 +110,8 @@ module RBS
97
110
  end
98
111
 
99
112
  def children(module_name)
113
+ module_name = builder.env.normalize_module_name(module_name)
114
+
100
115
  unless child_constants_cache.key?(module_name)
101
116
  load_child_constants(module_name)
102
117
  end
@@ -113,6 +128,7 @@ module RBS
113
128
  else
114
129
  constants_from_ancestors(BuiltinNames::Object.name, constants: consts)
115
130
  end
131
+
116
132
  constants_from_context(context, constants: consts) or return
117
133
  constants_itself(context, constants: consts)
118
134
 
@@ -151,7 +167,7 @@ module RBS
151
167
  constants_from_context(parent, constants: constants) or return false
152
168
 
153
169
  if last
154
- consts = table.children(last) or return false
170
+ consts = table.children(builder.env.normalize_module_name(last)) or return false
155
171
  constants.merge!(consts)
156
172
  end
157
173
  end
@@ -160,14 +176,14 @@ module RBS
160
176
  end
161
177
 
162
178
  def constants_from_ancestors(module_name, constants:)
163
- entry = builder.env.class_decls[module_name]
179
+ entry = builder.env.normalized_module_class_entry(module_name) or raise
164
180
 
165
- if entry.is_a?(Environment::ModuleEntry)
181
+ if entry.is_a?(Environment::ClassEntry) || entry.is_a?(Environment::ModuleEntry)
166
182
  constants.merge!(table.children(BuiltinNames::Object.name) || raise)
167
183
  constants.merge!(table.toplevel)
168
184
  end
169
185
 
170
- builder.ancestor_builder.instance_ancestors(module_name).ancestors.reverse_each do |ancestor|
186
+ builder.ancestor_builder.instance_ancestors(entry.name).ancestors.reverse_each do |ancestor|
171
187
  if ancestor.is_a?(Definition::Ancestor::Instance)
172
188
  case ancestor.source
173
189
  when AST::Members::Include, :super, nil
@@ -12,7 +12,8 @@ module RBS
12
12
 
13
13
  all_names.merge(env.class_decls.keys)
14
14
  all_names.merge(env.interface_decls.keys)
15
- all_names.merge(env.alias_decls.keys)
15
+ all_names.merge(env.type_alias_decls.keys)
16
+ all_names.merge(env.class_alias_decls.keys)
16
17
  end
17
18
 
18
19
  def try_cache(query)
data/lib/rbs/sorter.rb CHANGED
@@ -15,7 +15,7 @@ module RBS
15
15
  stdout.puts "Opening #{path}..."
16
16
 
17
17
  buffer = Buffer.new(name: path, content: path.read)
18
- sigs = Parser.parse_signature(buffer)
18
+ _, _, sigs = Parser.parse_signature(buffer)
19
19
 
20
20
  sigs.each do |m|
21
21
  sort_decl! m
@@ -30,7 +30,7 @@ module RBS
30
30
 
31
31
  def group(member)
32
32
  case member
33
- when Declarations::Alias
33
+ when Declarations::TypeAlias
34
34
  -3
35
35
  when Declarations::Constant
36
36
  -2
@@ -100,7 +100,7 @@ module RBS
100
100
  member.new_name.to_s
101
101
  when Declarations::Constant
102
102
  member.name.to_s
103
- when Declarations::Alias
103
+ when Declarations::TypeAlias
104
104
  member.name.to_s
105
105
  when Declarations::Class, Declarations::Module
106
106
  member.name.to_s
@@ -50,6 +50,8 @@ module RBS
50
50
  end
51
51
  end
52
52
 
53
+ alias [] apply
54
+
53
55
  def without(*vars)
54
56
  Substitution.new.tap do |subst|
55
57
  subst.mapping.merge!(mapping)
@@ -60,5 +62,22 @@ module RBS
60
62
  subst.instance_type = self.instance_type
61
63
  end
62
64
  end
65
+
66
+ def +(other)
67
+ return self if other.empty?
68
+ return other if self.empty?
69
+
70
+ Substitution.new.tap do |subst|
71
+ subst.mapping.merge!(mapping)
72
+
73
+ other.mapping.each do |var, type|
74
+ if mapping.key?(var)
75
+ subst.add(from: var, to: self[type])
76
+ else
77
+ subst.add(from: var, to: type)
78
+ end
79
+ end
80
+ end
81
+ end
63
82
  end
64
83
  end