steep 0.11.1 → 0.12.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 +27 -0
- data/.gitmodules +3 -0
- data/CHANGELOG.md +5 -0
- data/README.md +48 -90
- data/Rakefile +10 -6
- data/Steepfile +1 -0
- data/bin/setup +1 -0
- data/bin/smoke_runner.rb +9 -14
- data/exe/rbs +3 -0
- data/exe/ruby-signature +3 -0
- data/exe/steep +1 -0
- data/lib/steep.rb +32 -26
- data/lib/steep/annotation_parser.rb +167 -0
- data/lib/steep/ast/annotation/collection.rb +7 -7
- data/lib/steep/ast/types.rb +60 -0
- data/lib/steep/ast/types/any.rb +1 -1
- data/lib/steep/ast/types/factory.rb +535 -0
- data/lib/steep/ast/types/name.rb +3 -3
- data/lib/steep/ast/types/var.rb +1 -1
- data/lib/steep/cli.rb +56 -240
- data/lib/steep/drivers/annotations.rb +36 -19
- data/lib/steep/drivers/check.rb +55 -91
- data/lib/steep/drivers/init.rb +54 -0
- data/lib/steep/drivers/langserver.rb +241 -150
- data/lib/steep/drivers/print_project.rb +56 -0
- data/lib/steep/drivers/signature_error_printer.rb +25 -0
- data/lib/steep/drivers/trace_printer.rb +25 -0
- data/lib/steep/drivers/utils/driver_helper.rb +26 -0
- data/lib/steep/drivers/validate.rb +18 -38
- data/lib/steep/drivers/vendor.rb +46 -0
- data/lib/steep/drivers/watch.rb +78 -140
- data/lib/steep/errors.rb +22 -13
- data/lib/steep/interface/interface.rb +91 -0
- data/lib/steep/interface/method.rb +0 -4
- data/lib/steep/interface/method_type.rb +362 -2
- data/lib/steep/interface/substitution.rb +22 -0
- data/lib/steep/project.rb +25 -233
- data/lib/steep/project/dsl.rb +132 -0
- data/lib/steep/project/file.rb +93 -76
- data/lib/steep/project/file_loader.rb +63 -0
- data/lib/steep/project/options.rb +7 -0
- data/lib/steep/project/target.rb +190 -0
- data/lib/steep/signature/errors.rb +25 -77
- data/lib/steep/signature/validator.rb +122 -0
- data/lib/steep/source.rb +12 -7
- data/lib/steep/subtyping/check.rb +357 -633
- data/lib/steep/subtyping/constraints.rb +2 -2
- data/lib/steep/subtyping/trace.rb +23 -0
- data/lib/steep/type_construction.rb +509 -455
- data/lib/steep/type_inference/constant_env.rb +16 -24
- data/lib/steep/type_inference/type_env.rb +26 -18
- data/lib/steep/version.rb +1 -1
- data/sample/Steepfile +6 -0
- data/sample/lib/conference.rb +12 -0
- data/sample/sig/conference.rbs +6 -0
- data/smoke/alias/Steepfile +4 -0
- data/smoke/alias/a.rb +2 -2
- data/smoke/alias/{a.rbi → a.rbs} +1 -1
- data/smoke/and/Steepfile +4 -0
- data/smoke/array/Steepfile +4 -0
- data/smoke/array/a.rb +2 -2
- data/smoke/array/b.rb +4 -4
- data/smoke/array/c.rb +2 -2
- data/smoke/block/Steepfile +5 -0
- data/smoke/block/{a.rbi → a.rbs} +1 -1
- data/smoke/block/{c.rbi → c.rbs} +0 -0
- data/smoke/block/d.rb +6 -6
- data/smoke/case/Steepfile +4 -0
- data/smoke/case/a.rb +4 -3
- data/smoke/class/Steepfile +4 -0
- data/smoke/class/a.rb +1 -4
- data/smoke/class/a.rbs +24 -0
- data/smoke/class/h.rb +6 -2
- data/smoke/class/{h.rbi → h.rbs} +1 -2
- data/smoke/class/i.rb +1 -2
- data/smoke/class/i.rbs +9 -0
- data/smoke/const/Steepfile +4 -0
- data/smoke/dstr/Steepfile +4 -0
- data/smoke/ensure/Steepfile +4 -0
- data/smoke/ensure/a.rb +1 -1
- data/smoke/enumerator/Steepfile +4 -0
- data/smoke/enumerator/a.rb +7 -7
- data/smoke/enumerator/b.rb +6 -6
- data/smoke/extension/Steepfile +4 -0
- data/smoke/extension/{a.rbi → a.rbs} +2 -2
- data/smoke/extension/{e.rbi → e.rbs} +2 -2
- data/smoke/hash/Steepfile +4 -0
- data/smoke/hash/{a.rbi → a.rbs} +0 -0
- data/smoke/hash/b.rb +2 -2
- data/smoke/hash/c.rb +1 -1
- data/smoke/hash/e.rbs +3 -0
- data/smoke/hash/f.rb +1 -1
- data/smoke/hello/Steepfile +4 -0
- data/smoke/hello/hello.rbs +7 -0
- data/smoke/if/Steepfile +4 -0
- data/smoke/implements/Steepfile +4 -0
- data/smoke/implements/a.rbs +6 -0
- data/smoke/initialize/Steepfile +4 -0
- data/smoke/initialize/a.rbs +3 -0
- data/smoke/integer/Steepfile +4 -0
- data/smoke/integer/a.rb +5 -3
- data/smoke/interface/Steepfile +4 -0
- data/smoke/interface/{a.rbi → a.rbs} +0 -0
- data/smoke/kwbegin/Steepfile +4 -0
- data/smoke/lambda/Steepfile +4 -0
- data/smoke/lambda/a.rb +9 -2
- data/smoke/literal/Steepfile +4 -0
- data/smoke/literal/{literal_methods.rbi → literal_methods.rbs} +0 -0
- data/smoke/map/Steepfile +4 -0
- data/smoke/map/a.rb +1 -1
- data/smoke/method/Steepfile +4 -0
- data/smoke/method/{a.rbi → a.rbs} +0 -0
- data/smoke/method/b.rb +1 -4
- data/smoke/method/d.rb +1 -0
- data/smoke/method/d.rbs +3 -0
- data/smoke/module/Steepfile +4 -0
- data/smoke/module/a.rb +1 -1
- data/smoke/module/a.rbs +16 -0
- data/smoke/module/c.rb +1 -1
- data/smoke/regexp/Steepfile +4 -0
- data/smoke/regexp/a.rb +2 -2
- data/smoke/regexp/b.rb +16 -16
- data/smoke/regression/Steepfile +5 -0
- data/smoke/regression/array.rb +2 -2
- data/smoke/regression/hash.rb +2 -2
- data/smoke/regression/poly_new.rb +2 -0
- data/smoke/regression/poly_new.rbs +4 -0
- data/smoke/regression/set_divide.rb +2 -2
- data/smoke/rescue/Steepfile +4 -0
- data/smoke/rescue/a.rb +1 -1
- data/smoke/self/Steepfile +4 -0
- data/smoke/self/a.rbs +4 -0
- data/smoke/skip/Steepfile +4 -0
- data/smoke/stdout/Steepfile +4 -0
- data/smoke/stdout/{a.rbi → a.rbs} +1 -1
- data/smoke/super/Steepfile +4 -0
- data/smoke/super/a.rbs +10 -0
- data/smoke/type_case/Steepfile +4 -0
- data/smoke/type_case/a.rb +1 -1
- data/smoke/yield/Steepfile +4 -0
- data/smoke/yield/a.rb +2 -2
- data/steep.gemspec +14 -7
- data/vendor/ruby-signature/.github/workflows/ruby.yml +27 -0
- data/vendor/ruby-signature/.gitignore +12 -0
- data/vendor/ruby-signature/.rubocop.yml +15 -0
- data/vendor/ruby-signature/BSDL +22 -0
- data/vendor/ruby-signature/COPYING +56 -0
- data/vendor/ruby-signature/Gemfile +6 -0
- data/vendor/ruby-signature/README.md +93 -0
- data/vendor/ruby-signature/Rakefile +66 -0
- data/vendor/ruby-signature/bin/annotate-with-rdoc +156 -0
- data/vendor/ruby-signature/bin/console +14 -0
- data/vendor/ruby-signature/bin/query-rdoc +103 -0
- data/vendor/ruby-signature/bin/setup +10 -0
- data/vendor/ruby-signature/bin/sort +88 -0
- data/vendor/ruby-signature/bin/test_runner.rb +17 -0
- data/vendor/ruby-signature/docs/CONTRIBUTING.md +97 -0
- data/vendor/ruby-signature/docs/sigs.md +148 -0
- data/vendor/ruby-signature/docs/stdlib.md +152 -0
- data/vendor/ruby-signature/docs/syntax.md +528 -0
- data/vendor/ruby-signature/exe/rbs +3 -0
- data/vendor/ruby-signature/exe/ruby-signature +7 -0
- data/vendor/ruby-signature/lib/ruby/signature.rb +64 -0
- data/vendor/ruby-signature/lib/ruby/signature/ast/annotation.rb +29 -0
- data/vendor/ruby-signature/lib/ruby/signature/ast/comment.rb +29 -0
- data/vendor/ruby-signature/lib/ruby/signature/ast/declarations.rb +391 -0
- data/vendor/ruby-signature/lib/ruby/signature/ast/members.rb +364 -0
- data/vendor/ruby-signature/lib/ruby/signature/buffer.rb +52 -0
- data/vendor/ruby-signature/lib/ruby/signature/builtin_names.rb +54 -0
- data/vendor/ruby-signature/lib/ruby/signature/cli.rb +534 -0
- data/vendor/ruby-signature/lib/ruby/signature/constant.rb +28 -0
- data/vendor/ruby-signature/lib/ruby/signature/constant_table.rb +152 -0
- data/vendor/ruby-signature/lib/ruby/signature/definition.rb +172 -0
- data/vendor/ruby-signature/lib/ruby/signature/definition_builder.rb +921 -0
- data/vendor/ruby-signature/lib/ruby/signature/environment.rb +283 -0
- data/vendor/ruby-signature/lib/ruby/signature/environment_loader.rb +138 -0
- data/vendor/ruby-signature/lib/ruby/signature/environment_walker.rb +126 -0
- data/vendor/ruby-signature/lib/ruby/signature/errors.rb +189 -0
- data/vendor/ruby-signature/lib/ruby/signature/location.rb +104 -0
- data/vendor/ruby-signature/lib/ruby/signature/method_type.rb +125 -0
- data/vendor/ruby-signature/lib/ruby/signature/namespace.rb +93 -0
- data/vendor/ruby-signature/lib/ruby/signature/parser.y +1343 -0
- data/vendor/ruby-signature/lib/ruby/signature/prototype/rb.rb +441 -0
- data/vendor/ruby-signature/lib/ruby/signature/prototype/rbi.rb +579 -0
- data/vendor/ruby-signature/lib/ruby/signature/prototype/runtime.rb +383 -0
- data/vendor/ruby-signature/lib/ruby/signature/substitution.rb +48 -0
- data/vendor/ruby-signature/lib/ruby/signature/test.rb +28 -0
- data/vendor/ruby-signature/lib/ruby/signature/test/errors.rb +63 -0
- data/vendor/ruby-signature/lib/ruby/signature/test/hook.rb +290 -0
- data/vendor/ruby-signature/lib/ruby/signature/test/setup.rb +58 -0
- data/vendor/ruby-signature/lib/ruby/signature/test/spy.rb +324 -0
- data/vendor/ruby-signature/lib/ruby/signature/test/test_helper.rb +185 -0
- data/vendor/ruby-signature/lib/ruby/signature/test/type_check.rb +256 -0
- data/vendor/ruby-signature/lib/ruby/signature/type_name.rb +72 -0
- data/vendor/ruby-signature/lib/ruby/signature/types.rb +932 -0
- data/vendor/ruby-signature/lib/ruby/signature/variance_calculator.rb +140 -0
- data/vendor/ruby-signature/lib/ruby/signature/vendorer.rb +49 -0
- data/vendor/ruby-signature/lib/ruby/signature/version.rb +5 -0
- data/vendor/ruby-signature/lib/ruby/signature/writer.rb +271 -0
- data/vendor/ruby-signature/ruby-signature.gemspec +45 -0
- data/vendor/ruby-signature/stdlib/abbrev/abbrev.rbs +3 -0
- data/vendor/ruby-signature/stdlib/base64/base64.rbs +15 -0
- data/vendor/ruby-signature/stdlib/builtin/array.rbs +1997 -0
- data/vendor/ruby-signature/stdlib/builtin/basic_object.rbs +280 -0
- data/vendor/ruby-signature/stdlib/builtin/binding.rbs +177 -0
- data/vendor/ruby-signature/stdlib/builtin/builtin.rbs +35 -0
- data/vendor/ruby-signature/stdlib/builtin/class.rbs +145 -0
- data/vendor/ruby-signature/stdlib/builtin/comparable.rbs +116 -0
- data/vendor/ruby-signature/stdlib/builtin/complex.rbs +400 -0
- data/vendor/ruby-signature/stdlib/builtin/constants.rbs +37 -0
- data/vendor/ruby-signature/stdlib/builtin/data.rbs +5 -0
- data/vendor/ruby-signature/stdlib/builtin/deprecated.rbs +2 -0
- data/vendor/ruby-signature/stdlib/builtin/dir.rbs +419 -0
- data/vendor/ruby-signature/stdlib/builtin/encoding.rbs +606 -0
- data/vendor/ruby-signature/stdlib/builtin/enumerable.rbs +404 -0
- data/vendor/ruby-signature/stdlib/builtin/enumerator.rbs +260 -0
- data/vendor/ruby-signature/stdlib/builtin/errno.rbs +781 -0
- data/vendor/ruby-signature/stdlib/builtin/errors.rbs +582 -0
- data/vendor/ruby-signature/stdlib/builtin/exception.rbs +193 -0
- data/vendor/ruby-signature/stdlib/builtin/false_class.rbs +40 -0
- data/vendor/ruby-signature/stdlib/builtin/fiber.rbs +68 -0
- data/vendor/ruby-signature/stdlib/builtin/fiber_error.rbs +12 -0
- data/vendor/ruby-signature/stdlib/builtin/file.rbs +476 -0
- data/vendor/ruby-signature/stdlib/builtin/file_test.rbs +59 -0
- data/vendor/ruby-signature/stdlib/builtin/float.rbs +696 -0
- data/vendor/ruby-signature/stdlib/builtin/gc.rbs +121 -0
- data/vendor/ruby-signature/stdlib/builtin/hash.rbs +1029 -0
- data/vendor/ruby-signature/stdlib/builtin/integer.rbs +710 -0
- data/vendor/ruby-signature/stdlib/builtin/io.rbs +683 -0
- data/vendor/ruby-signature/stdlib/builtin/kernel.rbs +574 -0
- data/vendor/ruby-signature/stdlib/builtin/marshal.rbs +135 -0
- data/vendor/ruby-signature/stdlib/builtin/match_data.rbs +141 -0
- data/vendor/ruby-signature/stdlib/builtin/math.rbs +66 -0
- data/vendor/ruby-signature/stdlib/builtin/method.rbs +182 -0
- data/vendor/ruby-signature/stdlib/builtin/module.rbs +248 -0
- data/vendor/ruby-signature/stdlib/builtin/nil_class.rbs +82 -0
- data/vendor/ruby-signature/stdlib/builtin/numeric.rbs +409 -0
- data/vendor/ruby-signature/stdlib/builtin/object.rbs +824 -0
- data/vendor/ruby-signature/stdlib/builtin/proc.rbs +426 -0
- data/vendor/ruby-signature/stdlib/builtin/process.rbs +354 -0
- data/vendor/ruby-signature/stdlib/builtin/random.rbs +93 -0
- data/vendor/ruby-signature/stdlib/builtin/range.rbs +226 -0
- data/vendor/ruby-signature/stdlib/builtin/rational.rbs +424 -0
- data/vendor/ruby-signature/stdlib/builtin/rb_config.rbs +10 -0
- data/vendor/ruby-signature/stdlib/builtin/regexp.rbs +131 -0
- data/vendor/ruby-signature/stdlib/builtin/ruby_vm.rbs +14 -0
- data/vendor/ruby-signature/stdlib/builtin/signal.rbs +55 -0
- data/vendor/ruby-signature/stdlib/builtin/string.rbs +770 -0
- data/vendor/ruby-signature/stdlib/builtin/string_io.rbs +13 -0
- data/vendor/ruby-signature/stdlib/builtin/struct.rbs +40 -0
- data/vendor/ruby-signature/stdlib/builtin/symbol.rbs +230 -0
- data/vendor/ruby-signature/stdlib/builtin/thread.rbs +1112 -0
- data/vendor/ruby-signature/stdlib/builtin/thread_group.rbs +23 -0
- data/vendor/ruby-signature/stdlib/builtin/time.rbs +739 -0
- data/vendor/ruby-signature/stdlib/builtin/trace_point.rbs +91 -0
- data/vendor/ruby-signature/stdlib/builtin/true_class.rbs +46 -0
- data/vendor/ruby-signature/stdlib/builtin/unbound_method.rbs +159 -0
- data/vendor/ruby-signature/stdlib/builtin/warning.rbs +17 -0
- data/vendor/ruby-signature/stdlib/erb/erb.rbs +18 -0
- data/vendor/ruby-signature/stdlib/find/find.rbs +44 -0
- data/vendor/ruby-signature/stdlib/pathname/pathname.rbs +21 -0
- data/vendor/ruby-signature/stdlib/prime/integer-extension.rbs +23 -0
- data/vendor/ruby-signature/stdlib/prime/prime.rbs +188 -0
- data/vendor/ruby-signature/stdlib/securerandom/securerandom.rbs +9 -0
- data/vendor/ruby-signature/stdlib/set/set.rbs +77 -0
- data/vendor/ruby-signature/stdlib/tmpdir/tmpdir.rbs +53 -0
- metadata +244 -54
- data/.travis.yml +0 -7
- data/lib/steep/ast/signature/alias.rb +0 -19
- data/lib/steep/ast/signature/class.rb +0 -33
- data/lib/steep/ast/signature/const.rb +0 -17
- data/lib/steep/ast/signature/env.rb +0 -138
- data/lib/steep/ast/signature/extension.rb +0 -21
- data/lib/steep/ast/signature/gvar.rb +0 -17
- data/lib/steep/ast/signature/interface.rb +0 -31
- data/lib/steep/ast/signature/members.rb +0 -115
- data/lib/steep/ast/signature/module.rb +0 -21
- data/lib/steep/drivers/print_interface.rb +0 -94
- data/lib/steep/drivers/scaffold.rb +0 -321
- data/lib/steep/drivers/utils/each_signature.rb +0 -31
- data/lib/steep/interface/abstract.rb +0 -68
- data/lib/steep/interface/builder.rb +0 -637
- data/lib/steep/interface/instantiated.rb +0 -163
- data/lib/steep/interface/ivar_chain.rb +0 -26
- data/lib/steep/parser.y +0 -1278
- data/lib/steep/project/listener.rb +0 -53
- data/smoke/class/a.rbi +0 -24
- data/smoke/class/d.rb +0 -9
- data/smoke/class/e.rb +0 -12
- data/smoke/class/i.rbi +0 -9
- data/smoke/hash/e.rbi +0 -3
- data/smoke/hello/hello.rbi +0 -7
- data/smoke/implements/a.rbi +0 -6
- data/smoke/initialize/a.rbi +0 -3
- data/smoke/module/a.rbi +0 -16
- data/smoke/self/a.rbi +0 -4
- data/smoke/super/a.rbi +0 -10
- data/stdlib/builtin.rbi +0 -787
@@ -0,0 +1,140 @@
|
|
1
|
+
module Ruby
|
2
|
+
module Signature
|
3
|
+
class VarianceCalculator
|
4
|
+
class Result
|
5
|
+
attr_reader :result
|
6
|
+
|
7
|
+
def initialize(variables:)
|
8
|
+
@result = {}
|
9
|
+
variables.each do |x|
|
10
|
+
result[x] = :unused
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def covariant(x)
|
15
|
+
case result[x]
|
16
|
+
when :unused
|
17
|
+
result[x] = :covariant
|
18
|
+
when :contravariant
|
19
|
+
result[x] = :invariant
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def contravariant(x)
|
24
|
+
case result[x]
|
25
|
+
when :unused
|
26
|
+
result[x] = :contravariant
|
27
|
+
when :covariant
|
28
|
+
result[x] = :invariant
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def invariant(x)
|
33
|
+
result[x] = :invariant
|
34
|
+
end
|
35
|
+
|
36
|
+
def each(&block)
|
37
|
+
result.each(&block)
|
38
|
+
end
|
39
|
+
|
40
|
+
def include?(name)
|
41
|
+
result.key?(name)
|
42
|
+
end
|
43
|
+
|
44
|
+
def compatible?(var, with_annotation:)
|
45
|
+
variance = result[var]
|
46
|
+
|
47
|
+
case
|
48
|
+
when variance == :unused
|
49
|
+
true
|
50
|
+
when with_annotation == :invariant
|
51
|
+
true
|
52
|
+
when variance == with_annotation
|
53
|
+
true
|
54
|
+
else
|
55
|
+
false
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
attr_reader :builder
|
61
|
+
|
62
|
+
def initialize(builder:)
|
63
|
+
@builder = builder
|
64
|
+
end
|
65
|
+
|
66
|
+
def env
|
67
|
+
builder.env
|
68
|
+
end
|
69
|
+
|
70
|
+
def in_method_type(method_type:, variables:)
|
71
|
+
result = Result.new(variables: variables)
|
72
|
+
|
73
|
+
method_type.type.each_param do |param|
|
74
|
+
type(param.type, result: result, context: :contravariant)
|
75
|
+
end
|
76
|
+
|
77
|
+
if method_type.block
|
78
|
+
method_type.block.type.each_param do |param|
|
79
|
+
type(param.type, result: result, context: :covariant)
|
80
|
+
end
|
81
|
+
type(method_type.block.type.return_type, result: result, context: :contravariant)
|
82
|
+
end
|
83
|
+
|
84
|
+
type(method_type.type.return_type, result: result, context: :covariant)
|
85
|
+
|
86
|
+
result
|
87
|
+
end
|
88
|
+
|
89
|
+
def in_inherit(name:, args:, variables:)
|
90
|
+
type = Types::ClassInstance.new(name: name, args: args, location: nil)
|
91
|
+
|
92
|
+
Result.new(variables: variables).tap do |result|
|
93
|
+
type(type, result: result, context: :covariant)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def type(type, result:, context:)
|
98
|
+
case type
|
99
|
+
when Types::Variable
|
100
|
+
if result.include?(type.name)
|
101
|
+
case context
|
102
|
+
when :covariant
|
103
|
+
result.covariant(type.name)
|
104
|
+
when :contravariant
|
105
|
+
result.contravariant(type.name)
|
106
|
+
when :invariant
|
107
|
+
result.invariant(type.name)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
when Types::ClassInstance, Types::Interface
|
111
|
+
decl = env.find_class(type.name)
|
112
|
+
type.args.each.with_index do |ty, i|
|
113
|
+
var = decl.type_params.params[i]
|
114
|
+
case var.variance
|
115
|
+
when :invariant
|
116
|
+
type(ty, result: result, context: :invariant)
|
117
|
+
when :covariant
|
118
|
+
type(ty, result: result, context: context)
|
119
|
+
when :contravariant
|
120
|
+
con = case context
|
121
|
+
when :invariant
|
122
|
+
:invariant
|
123
|
+
when :covariant
|
124
|
+
:contravariant
|
125
|
+
when :contravariant
|
126
|
+
:covariant
|
127
|
+
end
|
128
|
+
type(ty, result: result, context: con)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
when Types::Tuple, Types::Record, Types::Union, Types::Intersection
|
132
|
+
# Covariant types
|
133
|
+
type.each_type do |ty|
|
134
|
+
type(ty, result: result, context: context)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Ruby
|
2
|
+
module Signature
|
3
|
+
class Vendorer
|
4
|
+
attr_reader :vendor_dir
|
5
|
+
|
6
|
+
def initialize(vendor_dir:)
|
7
|
+
@vendor_dir = vendor_dir
|
8
|
+
end
|
9
|
+
|
10
|
+
def ensure_dir
|
11
|
+
unless vendor_dir.directory?
|
12
|
+
vendor_dir.mkpath
|
13
|
+
end
|
14
|
+
|
15
|
+
yield
|
16
|
+
end
|
17
|
+
|
18
|
+
def clean!
|
19
|
+
ensure_dir do
|
20
|
+
Signature.logger.info "Cleaning vendor root: #{vendor_dir}..."
|
21
|
+
vendor_dir.rmtree
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def stdlib!()
|
26
|
+
ensure_dir do
|
27
|
+
Signature.logger.info "Vendoring stdlib: #{EnvironmentLoader::STDLIB_ROOT} => #{vendor_dir + "stdlib"}..."
|
28
|
+
FileUtils.copy_entry EnvironmentLoader::STDLIB_ROOT, vendor_dir + "stdlib"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def gem!(name, version)
|
33
|
+
ensure_dir do
|
34
|
+
sig_path = EnvironmentLoader.gem_sig_path(name, version)
|
35
|
+
Signature.logger.debug "Checking gem signature path: name=#{name}, version=#{version}, path=#{sig_path}"
|
36
|
+
|
37
|
+
if sig_path&.directory?
|
38
|
+
gems_dir = vendor_dir + "gems"
|
39
|
+
gems_dir.mkpath unless gems_dir.directory?
|
40
|
+
|
41
|
+
gem_dir = gems_dir + name
|
42
|
+
Signature.logger.info "Vendoring gem(#{name}:#{version}): #{sig_path} => #{gem_dir}..."
|
43
|
+
FileUtils.copy_entry sig_path, gem_dir
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,271 @@
|
|
1
|
+
module Ruby
|
2
|
+
module Signature
|
3
|
+
class Writer
|
4
|
+
attr_reader :out
|
5
|
+
|
6
|
+
def initialize(out:)
|
7
|
+
@out = out
|
8
|
+
end
|
9
|
+
|
10
|
+
def write_annotation(annotations, level:)
|
11
|
+
prefix = " " * level
|
12
|
+
|
13
|
+
annotations.each do |annotation|
|
14
|
+
string = annotation.string
|
15
|
+
case
|
16
|
+
when string !~ /\}/
|
17
|
+
out.puts "#{prefix}%a{#{string}}"
|
18
|
+
when string !~ /\)/
|
19
|
+
out.puts "#{prefix}%a(#{string})"
|
20
|
+
when string !~ /\]/
|
21
|
+
out.puts "#{prefix}%a[#{string}]"
|
22
|
+
when string !~ /\>/
|
23
|
+
out.puts "#{prefix}%a<#{string}>"
|
24
|
+
when string !~ /\|/
|
25
|
+
out.puts "#{prefix}%a|#{string}|"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def write_comment(comment, level:)
|
31
|
+
if comment
|
32
|
+
prefix = " " * level
|
33
|
+
comment.string.lines.each do |line|
|
34
|
+
line = " #{line}" unless line.chomp.empty?
|
35
|
+
out.puts "#{prefix}##{line}"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def write(decls)
|
41
|
+
[nil, *decls].each_cons(2) do |prev, decl|
|
42
|
+
preserve_empty_line(prev, decl)
|
43
|
+
write_decl decl
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def write_decl(decl)
|
48
|
+
case decl
|
49
|
+
when AST::Declarations::Class
|
50
|
+
super_class = if decl.super_class
|
51
|
+
" < #{name_and_args(decl.super_class.name, decl.super_class.args)}"
|
52
|
+
end
|
53
|
+
write_comment decl.comment, level: 0
|
54
|
+
write_annotation decl.annotations, level: 0
|
55
|
+
out.puts "class #{name_and_params(decl.name, decl.type_params)}#{super_class}"
|
56
|
+
|
57
|
+
[nil, *decl.members].each_cons(2) do |prev, member|
|
58
|
+
preserve_empty_line prev, member
|
59
|
+
write_member member
|
60
|
+
end
|
61
|
+
|
62
|
+
out.puts "end"
|
63
|
+
|
64
|
+
when AST::Declarations::Module
|
65
|
+
self_type = if decl.self_type
|
66
|
+
" : #{decl.self_type}"
|
67
|
+
end
|
68
|
+
|
69
|
+
write_comment decl.comment, level: 0
|
70
|
+
write_annotation decl.annotations, level: 0
|
71
|
+
out.puts "module #{name_and_params(decl.name, decl.type_params)}#{self_type}"
|
72
|
+
decl.members.each.with_index do |member, index|
|
73
|
+
if index > 0
|
74
|
+
out.puts
|
75
|
+
end
|
76
|
+
write_member member
|
77
|
+
end
|
78
|
+
out.puts "end"
|
79
|
+
when AST::Declarations::Constant
|
80
|
+
write_comment decl.comment, level: 0
|
81
|
+
out.puts "#{decl.name}: #{decl.type}"
|
82
|
+
|
83
|
+
when AST::Declarations::Global
|
84
|
+
write_comment decl.comment, level: 0
|
85
|
+
out.puts "#{decl.name}: #{decl.type}"
|
86
|
+
|
87
|
+
when AST::Declarations::Alias
|
88
|
+
write_comment decl.comment, level: 0
|
89
|
+
write_annotation decl.annotations, level: 0
|
90
|
+
out.puts "type #{decl.name} = #{decl.type}"
|
91
|
+
|
92
|
+
when AST::Declarations::Interface
|
93
|
+
write_comment decl.comment, level: 0
|
94
|
+
write_annotation decl.annotations, level: 0
|
95
|
+
out.puts "interface #{name_and_params(decl.name, decl.type_params)}"
|
96
|
+
decl.members.each.with_index do |member, index|
|
97
|
+
if index > 0
|
98
|
+
out.puts
|
99
|
+
end
|
100
|
+
write_member member
|
101
|
+
end
|
102
|
+
out.puts "end"
|
103
|
+
|
104
|
+
when AST::Declarations::Extension
|
105
|
+
write_comment decl.comment, level: 0
|
106
|
+
write_annotation decl.annotations, level: 0
|
107
|
+
out.puts "extension #{name_and_args(decl.name, decl.type_params)} (#{decl.extension_name})"
|
108
|
+
decl.members.each.with_index do |member, index|
|
109
|
+
if index > 0
|
110
|
+
out.puts
|
111
|
+
end
|
112
|
+
write_member member
|
113
|
+
end
|
114
|
+
out.puts "end"
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def name_and_params(name, params)
|
119
|
+
if params.empty?
|
120
|
+
"#{name}"
|
121
|
+
else
|
122
|
+
ps = params.each.map do |param|
|
123
|
+
s = ""
|
124
|
+
if param.skip_validation
|
125
|
+
s << "unchecked "
|
126
|
+
end
|
127
|
+
case param.variance
|
128
|
+
when :invariant
|
129
|
+
# nop
|
130
|
+
when :covariant
|
131
|
+
s << "out "
|
132
|
+
when :contravariant
|
133
|
+
s << "in "
|
134
|
+
end
|
135
|
+
s + param.name.to_s
|
136
|
+
end
|
137
|
+
|
138
|
+
"#{name}[#{ps.join(", ")}]"
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
def name_and_args(name, args)
|
143
|
+
if name && args
|
144
|
+
if args.empty?
|
145
|
+
"#{name}"
|
146
|
+
else
|
147
|
+
"#{name}[#{args.join(", ")}]"
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
def write_member(member)
|
153
|
+
case member
|
154
|
+
when AST::Members::Include
|
155
|
+
write_comment member.comment, level: 2
|
156
|
+
write_annotation member.annotations, level: 2
|
157
|
+
out.puts " include #{name_and_args(member.name, member.args)}"
|
158
|
+
when AST::Members::Extend
|
159
|
+
write_comment member.comment, level: 2
|
160
|
+
write_annotation member.annotations, level: 2
|
161
|
+
out.puts " extend #{name_and_args(member.name, member.args)}"
|
162
|
+
when AST::Members::Prepend
|
163
|
+
write_comment member.comment, level: 2
|
164
|
+
write_annotation member.annotations, level: 2
|
165
|
+
out.puts " prepend #{name_and_args(member.name, member.args)}"
|
166
|
+
when AST::Members::AttrAccessor
|
167
|
+
write_comment member.comment, level: 2
|
168
|
+
write_annotation member.annotations, level: 2
|
169
|
+
out.puts " #{attribute(:accessor, member)}"
|
170
|
+
when AST::Members::AttrReader
|
171
|
+
write_comment member.comment, level: 2
|
172
|
+
write_annotation member.annotations, level: 2
|
173
|
+
out.puts " #{attribute(:reader, member)}"
|
174
|
+
when AST::Members::AttrWriter
|
175
|
+
write_comment member.comment, level: 2
|
176
|
+
write_annotation member.annotations, level: 2
|
177
|
+
out.puts " #{attribute(:writer, member)}"
|
178
|
+
when AST::Members::Public
|
179
|
+
out.puts " public"
|
180
|
+
when AST::Members::Private
|
181
|
+
out.puts " private"
|
182
|
+
when AST::Members::Alias
|
183
|
+
write_comment member.comment, level: 2
|
184
|
+
write_annotation member.annotations, level: 2
|
185
|
+
new_name = member.singleton? ? "self.#{member.new_name}" : member.new_name
|
186
|
+
old_name = member.singleton? ? "self.#{member.old_name}" : member.old_name
|
187
|
+
out.puts " alias #{new_name} #{old_name}"
|
188
|
+
when AST::Members::InstanceVariable
|
189
|
+
write_comment member.comment, level: 2
|
190
|
+
out.puts " #{member.name}: #{member.type}"
|
191
|
+
when AST::Members::ClassInstanceVariable
|
192
|
+
write_comment member.comment, level: 2
|
193
|
+
out.puts " self.#{member.name}: #{member.type}"
|
194
|
+
when AST::Members::ClassVariable
|
195
|
+
write_comment member.comment, level: 2
|
196
|
+
out.puts " #{member.name}: #{member.type}"
|
197
|
+
when AST::Members::MethodDefinition
|
198
|
+
write_comment member.comment, level: 2
|
199
|
+
write_annotation member.annotations, level: 2
|
200
|
+
write_def member
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
def method_name(name)
|
205
|
+
s = name.to_s
|
206
|
+
|
207
|
+
if /\A#{Parser::KEYWORDS_RE}\z/.match?(s)
|
208
|
+
"`#{s}`"
|
209
|
+
else
|
210
|
+
s
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
def write_def(member)
|
215
|
+
name = case member.kind
|
216
|
+
when :instance
|
217
|
+
"#{method_name(member.name)}"
|
218
|
+
when :singleton_instance
|
219
|
+
"self?.#{method_name(member.name)}"
|
220
|
+
when :singleton
|
221
|
+
"self.#{method_name(member.name)}"
|
222
|
+
end
|
223
|
+
|
224
|
+
attrs = member.attributes.empty? ? "" : member.attributes.join(" ") + " "
|
225
|
+
prefix = " #{attrs}def #{name}:"
|
226
|
+
padding = " " * (prefix.size-1)
|
227
|
+
|
228
|
+
out.print prefix
|
229
|
+
|
230
|
+
member.types.each.with_index do |type, index|
|
231
|
+
if index > 0
|
232
|
+
out.print padding
|
233
|
+
out.print "|"
|
234
|
+
end
|
235
|
+
out.puts " #{type}"
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
def attribute(kind, attr)
|
240
|
+
var = case attr.ivar_name
|
241
|
+
when nil
|
242
|
+
""
|
243
|
+
when false
|
244
|
+
"()"
|
245
|
+
else
|
246
|
+
"(#{attr.ivar_name})"
|
247
|
+
end
|
248
|
+
"attr_#{kind} #{attr.name}#{var}: #{attr.type}"
|
249
|
+
end
|
250
|
+
|
251
|
+
def preserve_empty_line(prev, decl)
|
252
|
+
return unless prev
|
253
|
+
|
254
|
+
decl = decl.comment if decl.respond_to?(:comment) && decl.comment
|
255
|
+
|
256
|
+
# When the signature is not constructed by the parser,
|
257
|
+
# it always inserts an empty line.
|
258
|
+
if !prev.location || !decl.location
|
259
|
+
out.puts
|
260
|
+
return
|
261
|
+
end
|
262
|
+
|
263
|
+
prev_end_line = prev.location.end_line
|
264
|
+
start_line = decl.location.start_line
|
265
|
+
if start_line - prev_end_line > 1
|
266
|
+
out.puts
|
267
|
+
end
|
268
|
+
end
|
269
|
+
end
|
270
|
+
end
|
271
|
+
end
|