rbs 0.2.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +7 -1
- data/.gitignore +1 -1
- data/CHANGELOG.md +35 -0
- data/COPYING +1 -1
- data/Gemfile +16 -2
- data/README.md +87 -48
- data/Rakefile +54 -22
- data/bin/rbs-prof +9 -0
- data/bin/run_in_md.rb +49 -0
- data/bin/test_runner.rb +0 -2
- data/docs/sigs.md +6 -6
- data/docs/stdlib.md +3 -5
- data/docs/syntax.md +6 -3
- data/goodcheck.yml +65 -0
- data/lib/rbs.rb +3 -0
- data/lib/rbs/ast/declarations.rb +115 -14
- data/lib/rbs/ast/members.rb +41 -17
- data/lib/rbs/cli.rb +301 -123
- data/lib/rbs/constant.rb +4 -4
- data/lib/rbs/constant_table.rb +64 -53
- data/lib/rbs/definition.rb +175 -59
- data/lib/rbs/definition_builder.rb +646 -603
- data/lib/rbs/environment.rb +352 -210
- data/lib/rbs/environment_walker.rb +14 -23
- data/lib/rbs/errors.rb +159 -3
- data/lib/rbs/factory.rb +14 -0
- data/lib/rbs/namespace.rb +18 -0
- data/lib/rbs/parser.y +75 -21
- data/lib/rbs/prototype/rb.rb +119 -117
- data/lib/rbs/prototype/rbi.rb +5 -3
- data/lib/rbs/prototype/runtime.rb +34 -7
- data/lib/rbs/substitution.rb +8 -1
- data/lib/rbs/test.rb +81 -3
- data/lib/rbs/test/errors.rb +1 -1
- data/lib/rbs/test/hook.rb +133 -259
- data/lib/rbs/test/observer.rb +17 -0
- data/lib/rbs/test/setup.rb +13 -14
- data/lib/rbs/test/spy.rb +0 -321
- data/lib/rbs/test/tester.rb +116 -0
- data/lib/rbs/test/type_check.rb +44 -7
- data/lib/rbs/type_name_resolver.rb +58 -0
- data/lib/rbs/types.rb +94 -2
- data/lib/rbs/validator.rb +51 -0
- data/lib/rbs/variance_calculator.rb +12 -2
- data/lib/rbs/version.rb +1 -1
- data/lib/rbs/writer.rb +127 -91
- data/rbs.gemspec +0 -9
- data/schema/annotation.json +14 -0
- data/schema/comment.json +26 -0
- data/schema/decls.json +353 -0
- data/schema/function.json +87 -0
- data/schema/location.json +56 -0
- data/schema/members.json +248 -0
- data/schema/methodType.json +44 -0
- data/schema/types.json +299 -0
- data/stdlib/benchmark/benchmark.rbs +151 -151
- data/stdlib/builtin/encoding.rbs +2 -0
- data/stdlib/builtin/enumerable.rbs +2 -2
- data/stdlib/builtin/enumerator.rbs +3 -1
- data/stdlib/builtin/fiber.rbs +5 -1
- data/stdlib/builtin/file.rbs +0 -3
- data/stdlib/builtin/io.rbs +4 -4
- data/stdlib/builtin/proc.rbs +1 -2
- data/stdlib/builtin/symbol.rbs +1 -1
- data/stdlib/builtin/thread.rbs +2 -2
- data/stdlib/csv/csv.rbs +4 -6
- data/stdlib/fiber/fiber.rbs +117 -0
- data/stdlib/json/json.rbs +1 -1
- data/stdlib/logger/formatter.rbs +23 -0
- data/stdlib/logger/log_device.rbs +39 -0
- data/stdlib/logger/logger.rbs +507 -0
- data/stdlib/logger/period.rbs +7 -0
- data/stdlib/logger/severity.rbs +8 -0
- data/stdlib/mutex_m/mutex_m.rbs +77 -0
- data/stdlib/pathname/pathname.rbs +6 -6
- data/stdlib/prime/integer-extension.rbs +1 -1
- data/stdlib/prime/prime.rbs +44 -44
- data/stdlib/tmpdir/tmpdir.rbs +1 -1
- metadata +26 -116
- data/lib/rbs/test/test_helper.rb +0 -183
data/lib/rbs/test/type_check.rb
CHANGED
@@ -4,9 +4,34 @@ module RBS
|
|
4
4
|
attr_reader :self_class
|
5
5
|
attr_reader :builder
|
6
6
|
|
7
|
-
def initialize(self_class:, builder:)
|
7
|
+
def initialize(self_class:, builder:, sampling:)
|
8
8
|
@self_class = self_class
|
9
9
|
@builder = builder
|
10
|
+
@sampling = sampling
|
11
|
+
end
|
12
|
+
|
13
|
+
def overloaded_call(method, method_name, call, errors:)
|
14
|
+
es = method.method_types.map do |method_type|
|
15
|
+
es = method_call(method_name, method_type, call, errors: [])
|
16
|
+
|
17
|
+
if es.empty?
|
18
|
+
return errors
|
19
|
+
else
|
20
|
+
es
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
if es.size == 1
|
25
|
+
errors.push(*es[0])
|
26
|
+
else
|
27
|
+
errors << Errors::UnresolvedOverloadingError.new(
|
28
|
+
klass: self_class,
|
29
|
+
method_name: method_name,
|
30
|
+
method_types: method.method_types
|
31
|
+
)
|
32
|
+
end
|
33
|
+
|
34
|
+
errors
|
10
35
|
end
|
11
36
|
|
12
37
|
def method_call(method_name, method_type, call, errors:)
|
@@ -56,7 +81,7 @@ module RBS
|
|
56
81
|
end
|
57
82
|
|
58
83
|
def return(method_name, method_type, fun, call, errors, return_error:)
|
59
|
-
|
84
|
+
if call.return?
|
60
85
|
unless value(call.return_value, fun.return_type)
|
61
86
|
errors << return_error.new(klass: self_class,
|
62
87
|
method_name: method_name,
|
@@ -151,6 +176,14 @@ module RBS
|
|
151
176
|
end
|
152
177
|
end
|
153
178
|
|
179
|
+
def sampling?
|
180
|
+
!!@sampling
|
181
|
+
end
|
182
|
+
|
183
|
+
def sample(array)
|
184
|
+
array.size > 100 && sampling? ? array.sample(100) : array
|
185
|
+
end
|
186
|
+
|
154
187
|
def value(val, type)
|
155
188
|
case type
|
156
189
|
when Types::Bases::Any
|
@@ -175,9 +208,14 @@ module RBS
|
|
175
208
|
klass = Object.const_get(type.name.to_s)
|
176
209
|
case
|
177
210
|
when klass == ::Array
|
178
|
-
Test.call(val, IS_AP, klass) && val.
|
211
|
+
Test.call(val, IS_AP, klass) && sample(val).yield_self do |val|
|
212
|
+
val.all? {|v| value(v, type.args[0]) }
|
213
|
+
end
|
179
214
|
when klass == ::Hash
|
180
|
-
Test.call(val, IS_AP, klass) && val.
|
215
|
+
Test.call(val, IS_AP, klass) && sample(val.keys).yield_self do |keys|
|
216
|
+
values = val.values_at(*keys)
|
217
|
+
keys.all? {|key| value(key, type.args[0]) } && values.all? {|v| value(v, type.args[1]) }
|
218
|
+
end
|
181
219
|
when klass == ::Range
|
182
220
|
Test.call(val, IS_AP, klass) && value(val.begin, type.args[0]) && value(val.end, type.args[0])
|
183
221
|
when klass == ::Enumerator
|
@@ -198,7 +236,7 @@ module RBS
|
|
198
236
|
end
|
199
237
|
end
|
200
238
|
|
201
|
-
values.all? do |v|
|
239
|
+
sample(values).all? do |v|
|
202
240
|
if v.size == 1
|
203
241
|
# Only one block argument.
|
204
242
|
value(v[0], type.args[0]) || value(v, type.args[0])
|
@@ -220,8 +258,7 @@ module RBS
|
|
220
258
|
val == klass
|
221
259
|
when Types::Interface
|
222
260
|
methods = Set.new(Test.call(val, METHODS))
|
223
|
-
|
224
|
-
if (definition = builder.build_interface(type.name, decl))
|
261
|
+
if (definition = builder.build_interface(type.name))
|
225
262
|
definition.methods.each_key.all? do |method_name|
|
226
263
|
methods.member?(method_name)
|
227
264
|
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module RBS
|
2
|
+
class TypeNameResolver
|
3
|
+
Query = Struct.new(:type_name, :context, keyword_init: true)
|
4
|
+
|
5
|
+
attr_reader :all_names
|
6
|
+
attr_reader :cache
|
7
|
+
|
8
|
+
def initialize()
|
9
|
+
@all_names = Set[]
|
10
|
+
@cache = {}
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.from_env(env)
|
14
|
+
new.add_names(env.class_decls.keys)
|
15
|
+
.add_names(env.interface_decls.keys)
|
16
|
+
.add_names(env.alias_decls.keys)
|
17
|
+
end
|
18
|
+
|
19
|
+
def add_names(names)
|
20
|
+
all_names.merge(names)
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
def try_cache(query)
|
25
|
+
cache.fetch(query) do
|
26
|
+
result = yield
|
27
|
+
cache[query] = result
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def resolve(type_name, context:)
|
32
|
+
if type_name.absolute?
|
33
|
+
return type_name
|
34
|
+
end
|
35
|
+
|
36
|
+
query = Query.new(type_name: type_name, context: context)
|
37
|
+
try_cache(query) do
|
38
|
+
path_head, *path_tail = type_name.to_namespace.path
|
39
|
+
name_head = TypeName.new(name: path_head, namespace: Namespace.empty)
|
40
|
+
|
41
|
+
absolute_head = context.each.find do |namespace|
|
42
|
+
full_name = name_head.with_prefix(namespace)
|
43
|
+
has_name?(full_name) and break full_name
|
44
|
+
end
|
45
|
+
|
46
|
+
if absolute_head
|
47
|
+
has_name?(Namespace.new(path: absolute_head.to_namespace.path.push(*path_tail), absolute: true).to_type_name)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def has_name?(full_name)
|
53
|
+
if all_names.include?(full_name)
|
54
|
+
full_name
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/lib/rbs/types.rb
CHANGED
@@ -12,6 +12,12 @@ module RBS
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
+
module NoTypeName
|
16
|
+
def map_type_name
|
17
|
+
self
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
15
21
|
module EmptyEachType
|
16
22
|
def each_type
|
17
23
|
if block_given?
|
@@ -43,6 +49,7 @@ module RBS
|
|
43
49
|
include NoFreeVariables
|
44
50
|
include NoSubst
|
45
51
|
include EmptyEachType
|
52
|
+
include NoTypeName
|
46
53
|
|
47
54
|
def to_json(*a)
|
48
55
|
klass = to_s.to_sym
|
@@ -82,7 +89,11 @@ module RBS
|
|
82
89
|
class Top < Base; end
|
83
90
|
class Bottom < Base; end
|
84
91
|
class Self < Base; end
|
85
|
-
class Instance < Base
|
92
|
+
class Instance < Base
|
93
|
+
def sub(s)
|
94
|
+
s.apply(self)
|
95
|
+
end
|
96
|
+
end
|
86
97
|
class Class < Base; end
|
87
98
|
end
|
88
99
|
|
@@ -90,6 +101,8 @@ module RBS
|
|
90
101
|
attr_reader :name
|
91
102
|
attr_reader :location
|
92
103
|
|
104
|
+
include NoTypeName
|
105
|
+
|
93
106
|
def initialize(name:, location:)
|
94
107
|
@name = name
|
95
108
|
@location = location
|
@@ -174,6 +187,13 @@ module RBS
|
|
174
187
|
end
|
175
188
|
|
176
189
|
include EmptyEachType
|
190
|
+
|
191
|
+
def map_type_name
|
192
|
+
ClassSingleton.new(
|
193
|
+
name: yield(name, location, self),
|
194
|
+
location: location
|
195
|
+
)
|
196
|
+
end
|
177
197
|
end
|
178
198
|
|
179
199
|
module Application
|
@@ -235,6 +255,14 @@ module RBS
|
|
235
255
|
args: args.map {|ty| ty.sub(s) },
|
236
256
|
location: location)
|
237
257
|
end
|
258
|
+
|
259
|
+
def map_type_name(&block)
|
260
|
+
Interface.new(
|
261
|
+
name: yield(name, location, self),
|
262
|
+
args: args.map {|type| type.map_type_name(&block) },
|
263
|
+
location: location
|
264
|
+
)
|
265
|
+
end
|
238
266
|
end
|
239
267
|
|
240
268
|
class ClassInstance
|
@@ -257,6 +285,14 @@ module RBS
|
|
257
285
|
args: args.map {|ty| ty.sub(s) },
|
258
286
|
location: location)
|
259
287
|
end
|
288
|
+
|
289
|
+
def map_type_name(&block)
|
290
|
+
ClassInstance.new(
|
291
|
+
name: yield(name, location, self),
|
292
|
+
args: args.map {|type| type.map_type_name(&block) },
|
293
|
+
location: location
|
294
|
+
)
|
295
|
+
end
|
260
296
|
end
|
261
297
|
|
262
298
|
class Alias
|
@@ -290,6 +326,13 @@ module RBS
|
|
290
326
|
end
|
291
327
|
|
292
328
|
include EmptyEachType
|
329
|
+
|
330
|
+
def map_type_name
|
331
|
+
Alias.new(
|
332
|
+
name: yield(name, location, self),
|
333
|
+
location: location
|
334
|
+
)
|
335
|
+
end
|
293
336
|
end
|
294
337
|
|
295
338
|
class Tuple
|
@@ -343,6 +386,13 @@ module RBS
|
|
343
386
|
enum_for :each_type
|
344
387
|
end
|
345
388
|
end
|
389
|
+
|
390
|
+
def map_type_name(&block)
|
391
|
+
Tuple.new(
|
392
|
+
types: types.map {|type| type.map_type_name(&block) },
|
393
|
+
location: location
|
394
|
+
)
|
395
|
+
end
|
346
396
|
end
|
347
397
|
|
348
398
|
class Record
|
@@ -401,6 +451,13 @@ module RBS
|
|
401
451
|
enum_for :each_type
|
402
452
|
end
|
403
453
|
end
|
454
|
+
|
455
|
+
def map_type_name(&block)
|
456
|
+
Record.new(
|
457
|
+
fields: fields.transform_values {|ty| ty.map_type_name(&block) },
|
458
|
+
location: location
|
459
|
+
)
|
460
|
+
end
|
404
461
|
end
|
405
462
|
|
406
463
|
class Optional
|
@@ -449,6 +506,13 @@ module RBS
|
|
449
506
|
enum_for :each_type
|
450
507
|
end
|
451
508
|
end
|
509
|
+
|
510
|
+
def map_type_name(&block)
|
511
|
+
Optional.new(
|
512
|
+
type: type.map_type_name(&block),
|
513
|
+
location: location
|
514
|
+
)
|
515
|
+
end
|
452
516
|
end
|
453
517
|
|
454
518
|
class Union
|
@@ -510,6 +574,13 @@ module RBS
|
|
510
574
|
enum_for :map_type
|
511
575
|
end
|
512
576
|
end
|
577
|
+
|
578
|
+
def map_type_name(&block)
|
579
|
+
Union.new(
|
580
|
+
types: types.map {|type| type.map_type_name(&block) },
|
581
|
+
location: location
|
582
|
+
)
|
583
|
+
end
|
513
584
|
end
|
514
585
|
|
515
586
|
class Intersection
|
@@ -572,6 +643,13 @@ module RBS
|
|
572
643
|
enum_for :map_type
|
573
644
|
end
|
574
645
|
end
|
646
|
+
|
647
|
+
def map_type_name(&block)
|
648
|
+
Intersection.new(
|
649
|
+
types: types.map {|type| type.map_type_name(&block) },
|
650
|
+
location: location
|
651
|
+
)
|
652
|
+
end
|
575
653
|
end
|
576
654
|
|
577
655
|
class Function
|
@@ -648,7 +726,7 @@ module RBS
|
|
648
726
|
other.required_keywords == required_keywords &&
|
649
727
|
other.optional_keywords == optional_keywords &&
|
650
728
|
other.rest_keywords == rest_keywords &&
|
651
|
-
return_type == return_type
|
729
|
+
other.return_type == return_type
|
652
730
|
end
|
653
731
|
|
654
732
|
alias eql? ==
|
@@ -710,6 +788,12 @@ module RBS
|
|
710
788
|
end
|
711
789
|
end
|
712
790
|
|
791
|
+
def map_type_name(&block)
|
792
|
+
map_type do |type|
|
793
|
+
type.map_type_name(&block)
|
794
|
+
end
|
795
|
+
end
|
796
|
+
|
713
797
|
def each_type
|
714
798
|
if block_given?
|
715
799
|
required_positionals.each {|param| yield param.type }
|
@@ -899,6 +983,13 @@ module RBS
|
|
899
983
|
enum_for :each_type
|
900
984
|
end
|
901
985
|
end
|
986
|
+
|
987
|
+
def map_type_name(&block)
|
988
|
+
Proc.new(
|
989
|
+
type: type.map_type_name(&block),
|
990
|
+
location: location
|
991
|
+
)
|
992
|
+
end
|
902
993
|
end
|
903
994
|
|
904
995
|
class Literal
|
@@ -923,6 +1014,7 @@ module RBS
|
|
923
1014
|
include NoFreeVariables
|
924
1015
|
include NoSubst
|
925
1016
|
include EmptyEachType
|
1017
|
+
include NoTypeName
|
926
1018
|
|
927
1019
|
def to_json(*a)
|
928
1020
|
{ class: :literal, literal: literal.inspect, location: location }.to_json(*a)
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module RBS
|
2
|
+
class Validator
|
3
|
+
attr_reader :env
|
4
|
+
attr_reader :resolver
|
5
|
+
|
6
|
+
def initialize(env:, resolver:)
|
7
|
+
@env = env
|
8
|
+
@resolver = resolver
|
9
|
+
end
|
10
|
+
|
11
|
+
def absolute_type(type, context:)
|
12
|
+
type.map_type_name do |type_name, _, type|
|
13
|
+
resolver.resolve(type_name, context: context) || yield(type)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# Validates presence of the relative type, and application arity match.
|
18
|
+
def validate_type(type, context:)
|
19
|
+
case type
|
20
|
+
when Types::ClassInstance, Types::Interface
|
21
|
+
if type.name.namespace.relative?
|
22
|
+
type = absolute_type(type, context: context) do |type|
|
23
|
+
NoTypeFoundError.check!(type.name.absolute!, env: env, location: type.location)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
type_params = case type
|
28
|
+
when Types::ClassInstance
|
29
|
+
env.class_decls[type.name]&.type_params
|
30
|
+
when Types::Interface
|
31
|
+
env.interface_decls[type.name]&.decl&.type_params
|
32
|
+
end
|
33
|
+
|
34
|
+
unless type_params
|
35
|
+
raise NoTypeFoundError.new(type_name: type.name, location: type.location)
|
36
|
+
end
|
37
|
+
|
38
|
+
InvalidTypeApplicationError.check!(
|
39
|
+
type_name: type.name,
|
40
|
+
args: type.args,
|
41
|
+
params: type_params.each.map(&:name),
|
42
|
+
location: type.location
|
43
|
+
)
|
44
|
+
end
|
45
|
+
|
46
|
+
type.each_type do |type|
|
47
|
+
validate_type(type, context: context)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -107,9 +107,19 @@ module RBS
|
|
107
107
|
end
|
108
108
|
end
|
109
109
|
when Types::ClassInstance, Types::Interface
|
110
|
-
|
110
|
+
NoTypeFoundError.check!(type.name,
|
111
|
+
env: env,
|
112
|
+
location: type.location)
|
113
|
+
|
114
|
+
type_params = case type
|
115
|
+
when Types::ClassInstance
|
116
|
+
env.class_decls[type.name]&.type_params
|
117
|
+
when Types::Interface
|
118
|
+
env.interface_decls[type.name]&.decl&.type_params
|
119
|
+
end
|
120
|
+
|
111
121
|
type.args.each.with_index do |ty, i|
|
112
|
-
var =
|
122
|
+
var = type_params.params[i]
|
113
123
|
case var.variance
|
114
124
|
when :invariant
|
115
125
|
type(ty, result: result, context: :invariant)
|
data/lib/rbs/version.rb
CHANGED
data/lib/rbs/writer.rb
CHANGED
@@ -1,37 +1,59 @@
|
|
1
1
|
module RBS
|
2
2
|
class Writer
|
3
3
|
attr_reader :out
|
4
|
+
attr_reader :indentation
|
4
5
|
|
5
6
|
def initialize(out:)
|
6
7
|
@out = out
|
8
|
+
@indentation = []
|
7
9
|
end
|
8
10
|
|
9
|
-
def
|
10
|
-
|
11
|
+
def indent(size = 2)
|
12
|
+
indentation.push(" " * size)
|
13
|
+
yield
|
14
|
+
ensure
|
15
|
+
indentation.pop
|
16
|
+
end
|
17
|
+
|
18
|
+
def prefix
|
19
|
+
indentation.join()
|
20
|
+
end
|
21
|
+
|
22
|
+
def puts(string = "")
|
23
|
+
if string.size > 0
|
24
|
+
@out.puts("#{prefix}#{string}")
|
25
|
+
else
|
26
|
+
@out.puts
|
27
|
+
end
|
28
|
+
end
|
11
29
|
|
30
|
+
def write_annotation(annotations)
|
12
31
|
annotations.each do |annotation|
|
13
32
|
string = annotation.string
|
14
33
|
case
|
15
34
|
when string !~ /\}/
|
16
|
-
|
35
|
+
puts "%a{#{string}}"
|
17
36
|
when string !~ /\)/
|
18
|
-
|
37
|
+
puts "%a(#{string})"
|
19
38
|
when string !~ /\]/
|
20
|
-
|
39
|
+
puts "%a[#{string}]"
|
21
40
|
when string !~ /\>/
|
22
|
-
|
41
|
+
puts "%a<#{string}>"
|
23
42
|
when string !~ /\|/
|
24
|
-
|
43
|
+
puts "%a|#{string}|"
|
25
44
|
end
|
26
45
|
end
|
27
46
|
end
|
28
47
|
|
29
|
-
def write_comment(comment
|
48
|
+
def write_comment(comment)
|
30
49
|
if comment
|
31
|
-
prefix = " " * level
|
32
50
|
comment.string.lines.each do |line|
|
33
|
-
line =
|
34
|
-
|
51
|
+
line = line.chomp
|
52
|
+
unless line.empty?
|
53
|
+
puts "# #{line}"
|
54
|
+
else
|
55
|
+
puts "#"
|
56
|
+
end
|
35
57
|
end
|
36
58
|
end
|
37
59
|
end
|
@@ -49,68 +71,72 @@ module RBS
|
|
49
71
|
super_class = if decl.super_class
|
50
72
|
" < #{name_and_args(decl.super_class.name, decl.super_class.args)}"
|
51
73
|
end
|
52
|
-
write_comment decl.comment
|
53
|
-
write_annotation decl.annotations
|
54
|
-
|
74
|
+
write_comment decl.comment
|
75
|
+
write_annotation decl.annotations
|
76
|
+
puts "class #{name_and_params(decl.name, decl.type_params)}#{super_class}"
|
55
77
|
|
56
|
-
|
57
|
-
|
58
|
-
|
78
|
+
indent do
|
79
|
+
[nil, *decl.members].each_cons(2) do |prev, member|
|
80
|
+
preserve_empty_line prev, member
|
81
|
+
write_member member
|
82
|
+
end
|
59
83
|
end
|
60
84
|
|
61
|
-
|
85
|
+
puts "end"
|
62
86
|
|
63
87
|
when AST::Declarations::Module
|
64
|
-
self_type =
|
65
|
-
" : #{decl.
|
88
|
+
self_type = unless decl.self_types.empty?
|
89
|
+
" : #{decl.self_types.join(", ")}"
|
66
90
|
end
|
67
91
|
|
68
|
-
write_comment decl.comment
|
69
|
-
write_annotation decl.annotations
|
70
|
-
|
71
|
-
decl.
|
72
|
-
|
73
|
-
|
92
|
+
write_comment decl.comment
|
93
|
+
write_annotation decl.annotations
|
94
|
+
|
95
|
+
puts "module #{name_and_params(decl.name, decl.type_params)}#{self_type}"
|
96
|
+
|
97
|
+
indent do
|
98
|
+
decl.members.each.with_index do |member, index|
|
99
|
+
if index > 0
|
100
|
+
puts
|
101
|
+
end
|
102
|
+
write_member member
|
74
103
|
end
|
75
|
-
write_member member
|
76
104
|
end
|
77
|
-
|
105
|
+
|
106
|
+
puts "end"
|
78
107
|
when AST::Declarations::Constant
|
79
|
-
write_comment decl.comment
|
80
|
-
|
108
|
+
write_comment decl.comment
|
109
|
+
puts "#{decl.name}: #{decl.type}"
|
81
110
|
|
82
111
|
when AST::Declarations::Global
|
83
|
-
write_comment decl.comment
|
84
|
-
|
112
|
+
write_comment decl.comment
|
113
|
+
puts "#{decl.name}: #{decl.type}"
|
85
114
|
|
86
115
|
when AST::Declarations::Alias
|
87
|
-
write_comment decl.comment
|
88
|
-
write_annotation decl.annotations
|
89
|
-
|
116
|
+
write_comment decl.comment
|
117
|
+
write_annotation decl.annotations
|
118
|
+
puts "type #{decl.name} = #{decl.type}"
|
90
119
|
|
91
120
|
when AST::Declarations::Interface
|
92
|
-
write_comment decl.comment
|
93
|
-
write_annotation decl.annotations
|
94
|
-
|
95
|
-
decl.
|
96
|
-
|
97
|
-
|
121
|
+
write_comment decl.comment
|
122
|
+
write_annotation decl.annotations
|
123
|
+
|
124
|
+
puts "interface #{name_and_params(decl.name, decl.type_params)}"
|
125
|
+
|
126
|
+
indent do
|
127
|
+
decl.members.each.with_index do |member, index|
|
128
|
+
if index > 0
|
129
|
+
puts
|
130
|
+
end
|
131
|
+
write_member member
|
98
132
|
end
|
99
|
-
write_member member
|
100
133
|
end
|
101
|
-
|
134
|
+
|
135
|
+
puts "end"
|
102
136
|
|
103
137
|
when AST::Declarations::Extension
|
104
|
-
|
105
|
-
|
106
|
-
out.puts "extension #{name_and_args(decl.name, decl.type_params)} (#{decl.extension_name})"
|
107
|
-
decl.members.each.with_index do |member, index|
|
108
|
-
if index > 0
|
109
|
-
out.puts
|
110
|
-
end
|
111
|
-
write_member member
|
112
|
-
end
|
113
|
-
out.puts "end"
|
138
|
+
RBS.logger.warn "Extension is ignored: #{decl.name}"
|
139
|
+
|
114
140
|
end
|
115
141
|
end
|
116
142
|
|
@@ -151,52 +177,54 @@ module RBS
|
|
151
177
|
def write_member(member)
|
152
178
|
case member
|
153
179
|
when AST::Members::Include
|
154
|
-
write_comment member.comment
|
155
|
-
write_annotation member.annotations
|
156
|
-
|
180
|
+
write_comment member.comment
|
181
|
+
write_annotation member.annotations
|
182
|
+
puts "include #{name_and_args(member.name, member.args)}"
|
157
183
|
when AST::Members::Extend
|
158
|
-
write_comment member.comment
|
159
|
-
write_annotation member.annotations
|
160
|
-
|
184
|
+
write_comment member.comment
|
185
|
+
write_annotation member.annotations
|
186
|
+
puts "extend #{name_and_args(member.name, member.args)}"
|
161
187
|
when AST::Members::Prepend
|
162
|
-
write_comment member.comment
|
163
|
-
write_annotation member.annotations
|
164
|
-
|
188
|
+
write_comment member.comment
|
189
|
+
write_annotation member.annotations
|
190
|
+
puts "prepend #{name_and_args(member.name, member.args)}"
|
165
191
|
when AST::Members::AttrAccessor
|
166
|
-
write_comment member.comment
|
167
|
-
write_annotation member.annotations
|
168
|
-
|
192
|
+
write_comment member.comment
|
193
|
+
write_annotation member.annotations
|
194
|
+
puts "#{attribute(:accessor, member)}"
|
169
195
|
when AST::Members::AttrReader
|
170
|
-
write_comment member.comment
|
171
|
-
write_annotation member.annotations
|
172
|
-
|
196
|
+
write_comment member.comment
|
197
|
+
write_annotation member.annotations
|
198
|
+
puts "#{attribute(:reader, member)}"
|
173
199
|
when AST::Members::AttrWriter
|
174
|
-
write_comment member.comment
|
175
|
-
write_annotation member.annotations
|
176
|
-
|
200
|
+
write_comment member.comment
|
201
|
+
write_annotation member.annotations
|
202
|
+
puts "#{attribute(:writer, member)}"
|
177
203
|
when AST::Members::Public
|
178
|
-
|
204
|
+
puts "public"
|
179
205
|
when AST::Members::Private
|
180
|
-
|
206
|
+
puts "private"
|
181
207
|
when AST::Members::Alias
|
182
|
-
write_comment member.comment
|
183
|
-
write_annotation member.annotations
|
208
|
+
write_comment member.comment
|
209
|
+
write_annotation member.annotations
|
184
210
|
new_name = member.singleton? ? "self.#{member.new_name}" : member.new_name
|
185
211
|
old_name = member.singleton? ? "self.#{member.old_name}" : member.old_name
|
186
|
-
|
212
|
+
puts "alias #{new_name} #{old_name}"
|
187
213
|
when AST::Members::InstanceVariable
|
188
|
-
write_comment member.comment
|
189
|
-
|
214
|
+
write_comment member.comment
|
215
|
+
puts "#{member.name}: #{member.type}"
|
190
216
|
when AST::Members::ClassInstanceVariable
|
191
|
-
write_comment member.comment
|
192
|
-
|
217
|
+
write_comment member.comment
|
218
|
+
puts "self.#{member.name}: #{member.type}"
|
193
219
|
when AST::Members::ClassVariable
|
194
|
-
write_comment member.comment
|
195
|
-
|
220
|
+
write_comment member.comment
|
221
|
+
puts "#{member.name}: #{member.type}"
|
196
222
|
when AST::Members::MethodDefinition
|
197
|
-
write_comment member.comment
|
198
|
-
write_annotation member.annotations
|
223
|
+
write_comment member.comment
|
224
|
+
write_annotation member.annotations
|
199
225
|
write_def member
|
226
|
+
else
|
227
|
+
write_decl member
|
200
228
|
end
|
201
229
|
end
|
202
230
|
|
@@ -220,18 +248,26 @@ module RBS
|
|
220
248
|
"self.#{method_name(member.name)}"
|
221
249
|
end
|
222
250
|
|
251
|
+
string = ""
|
252
|
+
|
223
253
|
attrs = member.attributes.empty? ? "" : member.attributes.join(" ") + " "
|
224
|
-
|
254
|
+
overload = member.overload? ? "overload " : ""
|
255
|
+
prefix = "#{overload}#{attrs}def #{name}:"
|
225
256
|
padding = " " * (prefix.size-1)
|
226
257
|
|
227
|
-
|
258
|
+
string << prefix
|
228
259
|
|
229
260
|
member.types.each.with_index do |type, index|
|
230
261
|
if index > 0
|
231
|
-
|
232
|
-
|
262
|
+
string << padding
|
263
|
+
string << "|"
|
233
264
|
end
|
234
|
-
|
265
|
+
|
266
|
+
string << " #{type}\n"
|
267
|
+
end
|
268
|
+
|
269
|
+
string.each_line do |line|
|
270
|
+
puts line.chomp
|
235
271
|
end
|
236
272
|
end
|
237
273
|
|
@@ -255,14 +291,14 @@ module RBS
|
|
255
291
|
# When the signature is not constructed by the parser,
|
256
292
|
# it always inserts an empty line.
|
257
293
|
if !prev.location || !decl.location
|
258
|
-
|
294
|
+
puts
|
259
295
|
return
|
260
296
|
end
|
261
297
|
|
262
298
|
prev_end_line = prev.location.end_line
|
263
299
|
start_line = decl.location.start_line
|
264
300
|
if start_line - prev_end_line > 1
|
265
|
-
|
301
|
+
puts
|
266
302
|
end
|
267
303
|
end
|
268
304
|
end
|