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,148 @@
|
|
1
|
+
# Writing Signatures Guide
|
2
|
+
|
3
|
+
You can write the signature of your applications and libraries.
|
4
|
+
Signature of your Ruby program would help:
|
5
|
+
|
6
|
+
1. Understanding the code structure
|
7
|
+
2. Finding APIs
|
8
|
+
|
9
|
+
And if you ship your gem with signature, the gem users can type check their applications!
|
10
|
+
|
11
|
+
## Writing signatures
|
12
|
+
|
13
|
+
You first need to write your program's signature.
|
14
|
+
See [syntax guide](syntax.md).
|
15
|
+
|
16
|
+
## Testing signatures
|
17
|
+
|
18
|
+
When you finish writing signature, you may want to test the signature.
|
19
|
+
ruby-signature provides a feature to test your signature.
|
20
|
+
|
21
|
+
```
|
22
|
+
$ RBS_TEST_TARGET='Foo::*' bundle exec ruby -r ruby/signature/test/setup test/foo_test.rb
|
23
|
+
```
|
24
|
+
|
25
|
+
The test installs instrumentations to spy the method calls and check if arguments/return values are correct with respect to the type of the method in signature.
|
26
|
+
If errors are reported by the test, you will fix the signature.
|
27
|
+
You will be sure that you ship a correct signature finally.
|
28
|
+
|
29
|
+
The instrumentations are implemneted using `Module#prepend`.
|
30
|
+
It defines a module with same name of methods, which asserts the type of arguments/return values and calls `super`.
|
31
|
+
|
32
|
+
## Type errors
|
33
|
+
|
34
|
+
If the test detects type errors, it will print error messages.
|
35
|
+
|
36
|
+
### ArgumentTypeError, BlockArgumentTypeError
|
37
|
+
|
38
|
+
The message means there is an unexpected type of argument or block argument.
|
39
|
+
|
40
|
+
```
|
41
|
+
ERROR -- : [Kaigi::Speaker.new] ArgumentTypeError: expected `::String` (email) but given `:"matsumoto@soutaro.com"`
|
42
|
+
```
|
43
|
+
|
44
|
+
### ArgumentError, BlockArgumentError
|
45
|
+
|
46
|
+
The message means there is an unexpected argument or missing argument.
|
47
|
+
|
48
|
+
```
|
49
|
+
[Kaigi::Speaker.new] ArgumentError: expected method type (size: ::Symbol, email: ::String, name: ::String) -> ::Kaigi::Speaker
|
50
|
+
```
|
51
|
+
|
52
|
+
### ReturnTypeError, BlockReturnTypeError
|
53
|
+
|
54
|
+
The message means the return value from method or block is incorrect.
|
55
|
+
|
56
|
+
```
|
57
|
+
ERROR -- : [Kaigi::Conference#each_speaker] ReturnTypeError: expected `self` but returns `[#<Kaigi::Speaker:0x00007fb2b249e5a0 @name="Soutaro Matsumoto", @email=:"matsumoto@soutaro.com">]`
|
58
|
+
```
|
59
|
+
|
60
|
+
### UnexpectedBlockError, MissingBlockError
|
61
|
+
|
62
|
+
The errors are reported when required block is not given or unused block is given.
|
63
|
+
|
64
|
+
```
|
65
|
+
ERROR -- : [Kaigi::Conference#speakers] UnexpectedBlockError: unexpected block is given for `() -> ::Array[::Kaigi::Speaker]`
|
66
|
+
```
|
67
|
+
|
68
|
+
### UnresolvedOverloadingError
|
69
|
+
|
70
|
+
The error means there is a type error on overloaded methods.
|
71
|
+
The `ruby-signature` test framework tries to the best error message for overloaded methods too, but it reports the `UnresolvedOverloadingError` when it fails.
|
72
|
+
|
73
|
+
## Setting up the test
|
74
|
+
|
75
|
+
The design of the signature testing aims to be non-intrusive. The setup is done in two steps:
|
76
|
+
|
77
|
+
1. Loading the testing library
|
78
|
+
2. Setting up the test through environment variables
|
79
|
+
|
80
|
+
### Loading the library
|
81
|
+
|
82
|
+
You need to require `ruby/signature/test/setup` for signature testing.
|
83
|
+
You can do it using `-r` option through command line argument or the `RUBYOPT` environment variable.
|
84
|
+
|
85
|
+
```
|
86
|
+
$ ruby -r ruby/signature/test/setup run_tests.rb
|
87
|
+
$ RUBYOPT='-rruby/signature/test/setup' rake test
|
88
|
+
```
|
89
|
+
|
90
|
+
When you are using Bundler, you may need to require `bundler/setup` explicitly.
|
91
|
+
|
92
|
+
```
|
93
|
+
$ RUBYOPT='-rbundler/setup -rruby/signature/test/setup' bundle exec rake test
|
94
|
+
```
|
95
|
+
|
96
|
+
### Environment variables
|
97
|
+
|
98
|
+
You need to specify `RBS_TEST_TARGET` to run the test, and you can customize the test with the following environment variables.
|
99
|
+
|
100
|
+
- `RBS_TEST_SKIP` (optional)
|
101
|
+
- `RBS_TEST_OPT` (optional)
|
102
|
+
- `RBS_TEST_LOGLEVEL` (optional)
|
103
|
+
- `RBS_TEST_RAISE` (optional)
|
104
|
+
|
105
|
+
`RBS_TEST_TARGET` is to specify the classes you want to test. `RBS_TEST_TARGET` can contain comma-separated class name pattern, which is one of an exact class name or with wildcard `*`.
|
106
|
+
|
107
|
+
- `RBS_TEST_TARGET=Foo::Bar,Foo::Baz` comma separated exact class names
|
108
|
+
- `RBS_TEST_TARGET=Foo::*` using wildcard
|
109
|
+
|
110
|
+
`RBS_TEST_SKIP` is to skip some of the classes which matches with `RBS_TEST_TARGET`.
|
111
|
+
|
112
|
+
`RBS_TEST_OPT` is to pass the options for ruby signature handling.
|
113
|
+
You may need to specify `-r` or `-I` to load signatures.
|
114
|
+
The default is `-I sig`.
|
115
|
+
|
116
|
+
```
|
117
|
+
RBS_TEST_OPT='-r set -r pathname -I sig'
|
118
|
+
```
|
119
|
+
|
120
|
+
`RBS_TEST_LOGLEVEL` can be used to configure log level. Defaults to `info`.
|
121
|
+
|
122
|
+
`RBS_TEST_RAISE` may help to debug the type signatures.
|
123
|
+
If the environment variable is set, it raises an exception when a type error is detected.
|
124
|
+
You can see the backtrace how the type error is caused and debug your program or signature.
|
125
|
+
|
126
|
+
So, a typical command line to start the test would look like the following:
|
127
|
+
|
128
|
+
```
|
129
|
+
$ RBS_TEST_LOGLEVEL=error \
|
130
|
+
RBS_TEST_TARGET='Kaigi::*' \
|
131
|
+
RBS_TEST_SKIP='Kaigi::MonkeyPatch' \
|
132
|
+
RBS_TEST_OPT='-rset -rpathname -Isig -Iprivate' \
|
133
|
+
RBS_TEST_RAISE=true \
|
134
|
+
RUBYOPT='-rbundler/setup -rruby/signature/test/setup' \
|
135
|
+
bundle exec rake test
|
136
|
+
```
|
137
|
+
|
138
|
+
## Testing tips
|
139
|
+
|
140
|
+
### Skipping a method
|
141
|
+
|
142
|
+
You can skip installing the instrumentation per-method basis using `rbs:test:skip` annotation.
|
143
|
+
|
144
|
+
```
|
145
|
+
class String
|
146
|
+
%a{rbs:test:skip} def =~: (Regexp) -> Integer?
|
147
|
+
end
|
148
|
+
```
|
@@ -0,0 +1,152 @@
|
|
1
|
+
# Stdlib Signatures Guide
|
2
|
+
|
3
|
+
This is a guide for contributing to `ruby-signature` by writing/revising stdlib signatures.
|
4
|
+
|
5
|
+
The typical steps of writing signatures will be like the following:
|
6
|
+
|
7
|
+
1. Generate a prototype
|
8
|
+
2. Import RDoc document
|
9
|
+
3. Give correct types to the prototype
|
10
|
+
4. Add tests
|
11
|
+
|
12
|
+
## Signatures
|
13
|
+
|
14
|
+
Signatures for standard libraries are located in `stdlib` directory. `stdlib/builtin` is for builtin libraries. Other libraries have directories like `stdlib/set` or `stdlib/pathname`.
|
15
|
+
|
16
|
+
To write signatures see [syntax guide](syntax.md).
|
17
|
+
|
18
|
+
## Generating prototypes
|
19
|
+
|
20
|
+
`ruby-signature` provides a tool to generate a prototype of signatures, `rbs prototype`.
|
21
|
+
It provides several options, `rbi` from Sorbet RBI files, `rb` from Ruby code, and `runtime` from runtime API.
|
22
|
+
`runtime` should be the best option for standard libraries because they may be implemented in C, no Ruby source code.
|
23
|
+
|
24
|
+
The tool `require`s all of the libraries specified with `-r` option, and then use introspection APIs like `instance_methods` to know the structure of the class.
|
25
|
+
The commandline receives the name of classes you want to prototype, exact class name (like `Pathname`) or pattern with `*` (like `IO::*`).
|
26
|
+
|
27
|
+
```
|
28
|
+
$ bundle exec rbs prototype runtime --require pathname Pathname
|
29
|
+
class Pathname
|
30
|
+
def self.getwd: () -> untyped
|
31
|
+
|
32
|
+
def self.glob: (*untyped) -> untyped
|
33
|
+
|
34
|
+
def self.pwd: () -> untyped
|
35
|
+
|
36
|
+
def +: (untyped other) -> untyped
|
37
|
+
|
38
|
+
alias / +
|
39
|
+
|
40
|
+
def <=>: (untyped) -> untyped
|
41
|
+
|
42
|
+
# snip
|
43
|
+
end
|
44
|
+
|
45
|
+
# snip
|
46
|
+
```
|
47
|
+
|
48
|
+
The prototype includes:
|
49
|
+
|
50
|
+
* Instance method definitions,
|
51
|
+
* Singleton method definitions,
|
52
|
+
* Includes, and
|
53
|
+
* Constants
|
54
|
+
|
55
|
+
It generate a simple prototype in the sense that all of the types included are `untyped`.
|
56
|
+
But it will help you to have an overview of the signatures you are trying to write.
|
57
|
+
|
58
|
+
### What to do with existing RBS files
|
59
|
+
|
60
|
+
Generating prototypes will override everything, so the problem is if there is a RBS files already.
|
61
|
+
You can try to find missing parts, or you can start from the scratch.
|
62
|
+
|
63
|
+
One non-trivial but absolutely better solution is to make a tool:
|
64
|
+
|
65
|
+
1. To load type definitions from existing RBS file, and
|
66
|
+
2. Generate prototypes only for missing methods.
|
67
|
+
|
68
|
+
## Import RDoc document
|
69
|
+
|
70
|
+
The next step should be importing RDoc documents.
|
71
|
+
|
72
|
+
```
|
73
|
+
$ bundle exec bin/annotate-with-rdoc stdlib/pathname/pathname.rbs
|
74
|
+
Loading store from /Users/soutaro/.rbenv/versions/2.7.0-dev/share/ri/2.7.0/system...
|
75
|
+
Loading store from /Users/soutaro/.rbenv/versions/2.7.0-dev/share/ri/2.7.0/site...
|
76
|
+
Opening stdlib/pathname/pathname.rbs...
|
77
|
+
Importing documentation for Pathname...
|
78
|
+
Processing glob...
|
79
|
+
Processing +...
|
80
|
+
# snip
|
81
|
+
Writing stdlib/pathname/pathname.rbs...
|
82
|
+
```
|
83
|
+
|
84
|
+
The `annotate-with-rdoc` command adds annotations to RBS files.
|
85
|
+
|
86
|
+
1. Query RDoc documents to annotate classes, modules, methods, and constants,
|
87
|
+
2. Put annotations on RBS AST, and
|
88
|
+
3. Update the given .RBS files
|
89
|
+
|
90
|
+
We recommend using the command to annotate the RBS files.
|
91
|
+
|
92
|
+
## Writing types
|
93
|
+
|
94
|
+
The next step is to replace `untyped` types in the prototype.
|
95
|
+
See [syntax guide](syntax.md) for the detail of the syntax.
|
96
|
+
|
97
|
+
We can show some of the guides for writing types.
|
98
|
+
|
99
|
+
1. Use `bool` for truth values, truthy or falsey. More specific types like `TrueClass | FalseClass` may be too strict.
|
100
|
+
2. Use `void` if the return value is useless.
|
101
|
+
3. Use `nil` instead of `NilClass`.
|
102
|
+
4. The most strict types may not be the best types. Use `untyped` if you cannot find the best one.
|
103
|
+
|
104
|
+
## Add Tests
|
105
|
+
|
106
|
+
We support writing tests for stdlib signatures.
|
107
|
+
|
108
|
+
### Writing tests
|
109
|
+
|
110
|
+
First, execute `generate:stdlib_test` rake task with a class name that you want to test.
|
111
|
+
|
112
|
+
```bash
|
113
|
+
$ bundle exec rake 'generate:stdlib_test[String]'
|
114
|
+
Created: test/stdlib/String_test.rb
|
115
|
+
```
|
116
|
+
|
117
|
+
It generates `test/stdlib/[class_name]_test.rb`.
|
118
|
+
The test scripts would look like the following:
|
119
|
+
|
120
|
+
```rb
|
121
|
+
class StringTest < StdlibTest
|
122
|
+
target String
|
123
|
+
using hook.refinement
|
124
|
+
|
125
|
+
def test_gsub
|
126
|
+
s = "string"
|
127
|
+
s.gsub(/./, "")
|
128
|
+
s.gsub("a", "b")
|
129
|
+
s.gsub(/./) {|x| "" }
|
130
|
+
s.gsub(/./, {"foo" => "bar"})
|
131
|
+
s.gsub(/./)
|
132
|
+
s.gsub("")
|
133
|
+
end
|
134
|
+
end
|
135
|
+
```
|
136
|
+
|
137
|
+
You need two method calls, `target` and `using`.
|
138
|
+
`target` method call tells which class is the subject of the class.
|
139
|
+
`using hook.refinement` installs a special instrumentation for stdlib, based on refinements.
|
140
|
+
And you write the sample programs which calls all of the patterns of overloads.
|
141
|
+
|
142
|
+
Note that the instrumentation is based on refinements and you need to write all method calls in the unit class definitions.
|
143
|
+
If the execution of the program escape from the class definition, the instrumentation is disabled and no check will be done.
|
144
|
+
|
145
|
+
### Running tests
|
146
|
+
|
147
|
+
You can run the test with:
|
148
|
+
|
149
|
+
```
|
150
|
+
$ bundle exec ruby bin/test_runner.rb # Run all tests
|
151
|
+
$ bundle exec ruby test/stdlib/String_test.rb # Run specific tests
|
152
|
+
```
|
@@ -0,0 +1,528 @@
|
|
1
|
+
# Syntax
|
2
|
+
|
3
|
+
## Types
|
4
|
+
|
5
|
+
```markdown
|
6
|
+
_type_ ::= _class-name_ _type-arguments_ (Class instance type)
|
7
|
+
| _interface-name_ _type-arguments_ (Interface type)
|
8
|
+
| `singleton(` _class-name_ `)` (Class singleton type)
|
9
|
+
| _alias-name_ (Alias type)
|
10
|
+
| _literal_ (Literal type)
|
11
|
+
| _type_ `|` _type_ (Union type)
|
12
|
+
| _type_ `&` _type_ (Intersection type)
|
13
|
+
| _type_ `?` (Optional type)
|
14
|
+
| `{` _record-name_ `:` _type_ `,` ... `}` (Record type)
|
15
|
+
| `[]` | `[` _type_ `,` ... `]` (Tuples)
|
16
|
+
| _type-variable_ (Type variables)
|
17
|
+
| `^(` _parameters_ `) ->` _type_ (Proc type)
|
18
|
+
| `self`
|
19
|
+
| `instance`
|
20
|
+
| `class`
|
21
|
+
| `bool`
|
22
|
+
| `untyped`
|
23
|
+
| `nil`
|
24
|
+
| `top`
|
25
|
+
| `bot`
|
26
|
+
| `void`
|
27
|
+
|
28
|
+
_class-name_ ::= _namespace_ /[A-Z]\w*/
|
29
|
+
_interface-name_ ::= _namespace_ /_[A-Z]\w*/
|
30
|
+
_alias-name_ ::= _namespace_ /[a-z]\w*/
|
31
|
+
|
32
|
+
_type-variable_ ::= /[A-Z]\w*/
|
33
|
+
|
34
|
+
_namespace_ ::= (Empty namespace)
|
35
|
+
| `::` (Root)
|
36
|
+
| _namespace_ /[A-Z]\w*/ `::` (Namespace)
|
37
|
+
|
38
|
+
_type-arguments_ ::= (No application)
|
39
|
+
| `[` _type_ `,` ... `]` (Type application)
|
40
|
+
|
41
|
+
_literal_ ::= _string-literal_
|
42
|
+
| _symbol-literal_
|
43
|
+
| _integer-literal_
|
44
|
+
| `true`
|
45
|
+
| `false`
|
46
|
+
```
|
47
|
+
|
48
|
+
### Class instance type
|
49
|
+
|
50
|
+
Class instance type denotes _an instance of a class_.
|
51
|
+
|
52
|
+
```
|
53
|
+
Integer # Instance of Integer class
|
54
|
+
::Integer # Instance of ::Integer class
|
55
|
+
Hash[Symbol, String] # Instance of Hash class with type application of Symbol and String
|
56
|
+
```
|
57
|
+
|
58
|
+
### Interface type
|
59
|
+
|
60
|
+
Interface type denotes _type of a value which can be a subtype of the interface_.
|
61
|
+
|
62
|
+
```
|
63
|
+
_ToS # _ToS interface
|
64
|
+
::MyApp::_Each[String] # Interface name with namespace and type application
|
65
|
+
```
|
66
|
+
|
67
|
+
### Class singleton type
|
68
|
+
|
69
|
+
Class singleton type denotes _the type of a singleton object of a class_.
|
70
|
+
|
71
|
+
```
|
72
|
+
singleton(String)
|
73
|
+
singleton(::Hash) # Class singleton type cannot be parametrized.
|
74
|
+
```
|
75
|
+
|
76
|
+
### Alias type
|
77
|
+
|
78
|
+
Alias type denotes an alias declared with _alias declaration_.
|
79
|
+
|
80
|
+
The name of type aliases starts with lowercase `[a-z]`.
|
81
|
+
|
82
|
+
|
83
|
+
```
|
84
|
+
name
|
85
|
+
::JSON::t # Alias name with namespace
|
86
|
+
```
|
87
|
+
|
88
|
+
### Literal type
|
89
|
+
|
90
|
+
Literal type denotes _a type with only one value of the literal_.
|
91
|
+
|
92
|
+
```
|
93
|
+
123 # Integer
|
94
|
+
"hello world" # A string
|
95
|
+
:to_s # A symbol
|
96
|
+
true # true or false
|
97
|
+
```
|
98
|
+
|
99
|
+
### Union type
|
100
|
+
|
101
|
+
Union type denotes _a type of one of the given types_.
|
102
|
+
|
103
|
+
```
|
104
|
+
Integer | String # Integer or String
|
105
|
+
Array[Integer | String] # Array of Integer or String
|
106
|
+
```
|
107
|
+
|
108
|
+
### Intersection type
|
109
|
+
|
110
|
+
Intersection type denotes _a type of all of the given types_.
|
111
|
+
|
112
|
+
```
|
113
|
+
Integer & String # Integer and String
|
114
|
+
```
|
115
|
+
|
116
|
+
Note that `&` has higher precedence than `|` that `Integer & String | Symbol` is `(Integer & String) | Symbol`.
|
117
|
+
|
118
|
+
### Optional type
|
119
|
+
|
120
|
+
Optional type denotes _a type of value or nil_.
|
121
|
+
|
122
|
+
```
|
123
|
+
Integer?
|
124
|
+
Array[Integer?]
|
125
|
+
```
|
126
|
+
|
127
|
+
### Record type
|
128
|
+
|
129
|
+
Records are `Hash` objects, fixed set of keys, and heterogeneous.
|
130
|
+
|
131
|
+
```
|
132
|
+
{ id: Integer, name: String } # Hash object like `{ id: 31, name: String }`
|
133
|
+
```
|
134
|
+
|
135
|
+
### Tuple type
|
136
|
+
|
137
|
+
Tuples are `Array` objects, fixed size and heterogeneous.
|
138
|
+
|
139
|
+
```
|
140
|
+
[ ] # Empty like `[]`
|
141
|
+
[String] # Single string like `["hi"]`
|
142
|
+
[Integer, Integer] # Pair of integers like `[1, 2]`
|
143
|
+
[Symbol, Integer, Integer] # Tuple of Symbol, Integer, and Integer like `[:pair, 30, 22]`
|
144
|
+
```
|
145
|
+
|
146
|
+
*Empty tuple* or *1-tuple* sound strange, but RBS allows these types.
|
147
|
+
|
148
|
+
### Type variable
|
149
|
+
|
150
|
+
```
|
151
|
+
U
|
152
|
+
T
|
153
|
+
S
|
154
|
+
Elem
|
155
|
+
```
|
156
|
+
|
157
|
+
Type variables cannot be distinguished from _class instance types_.
|
158
|
+
They are scoped in _class/module/interface declaration_ or _generic method types_.
|
159
|
+
|
160
|
+
```
|
161
|
+
class Ref[T] # Object is scoped in the class declaration.
|
162
|
+
@value: T # Type variable `T`
|
163
|
+
def map: [X] { (T) -> X } -> Ref[X] # X is a type variable scoped in the method type.
|
164
|
+
end
|
165
|
+
```
|
166
|
+
|
167
|
+
### Proc type
|
168
|
+
|
169
|
+
Proc type denots type of procedures, `Proc` instances.
|
170
|
+
|
171
|
+
```
|
172
|
+
^(Integer) -> String # A procedure with an `Integer` parameter and returns `String`
|
173
|
+
^(?String, size: Integer) -> bool # A procedure with `String` optional parameter, `size` keyword of `Integer`, and returns `bool`
|
174
|
+
```
|
175
|
+
|
176
|
+
### Base types
|
177
|
+
|
178
|
+
`self` denotes the type of receiver. The type is used to model the open recursion via `self`.
|
179
|
+
|
180
|
+
`instance` denotes the type of instance of the class. `class` is the singleton of the class.
|
181
|
+
|
182
|
+
`bool` is an abstract type for truth value.
|
183
|
+
|
184
|
+
`untyped` is for _a type without type checking_. It is `?` in gradual typing, _dynamic_ in some languages like C#, and _any_ in TypeScript. It is both subtype _and_ supertype of all of the types. (The type was `any` but renamed to `untyped`.)
|
185
|
+
|
186
|
+
`nil` is for _nil_.
|
187
|
+
|
188
|
+
`top` is a supertype of all of the types. `bot` is a subtype of all of the types.
|
189
|
+
|
190
|
+
`void` is a supertype of all of the types.
|
191
|
+
|
192
|
+
#### `nil` or `NilClass`?
|
193
|
+
|
194
|
+
We recommend using `nil`.
|
195
|
+
|
196
|
+
#### `bool` or `TrueClass | FalseClass`
|
197
|
+
|
198
|
+
We recommend using `bool` because it is more close to Ruby's semantics. If the type of a parameter of a method is `bool`, we usually pass `true` and `false`, and also `nil` or any other values. `TrueClass | FalseClass` rejects other values than `true` and `false`.
|
199
|
+
|
200
|
+
#### `void`, `bool`, or `top`?
|
201
|
+
|
202
|
+
They are all equivalent for the type system; they are all _top type_.
|
203
|
+
|
204
|
+
`void` tells developers a hint that _the value should not be used_. `bool` implies the value is used as a truth value. `top` is anything else.
|
205
|
+
|
206
|
+
## Method Types
|
207
|
+
|
208
|
+
```markdown
|
209
|
+
_method-type_ ::= `(` _parameters_ `) ->` _type_ # Method without block
|
210
|
+
| `(` _parameters_ `) { (` _parameters_ `) -> ` _type_ `} ->` _type_ # Method with required block
|
211
|
+
| `(` _parameters_ `) ?{ (` _parameters_ `) -> ` _type_ `} ->` _type_ # Method with optional block
|
212
|
+
|
213
|
+
_parameters_ ::= _required-positionals_ _optional-positionals_ _rest-positional_ _trailing-positionals_ _keywords_
|
214
|
+
|
215
|
+
_paramater_ ::= _type_ _var-name_ # Parameter with var name
|
216
|
+
| _type_ # Parameter without var name
|
217
|
+
_required-positionals_ ::= _parameter_ `,` ...
|
218
|
+
_optional-positionals_ ::= `?` _parameter_ `,` ...
|
219
|
+
_rest-positional_ ::= # Empty
|
220
|
+
| `*` _parameter_
|
221
|
+
_trailing-positionals_ ::= _parameter_ `,` ...
|
222
|
+
_keywords_ ::= # Empty
|
223
|
+
| `**` _parameter_ # Rest keyword
|
224
|
+
| _keyword_ `:` _parameter_ `,` _keywords_ # Required keyword
|
225
|
+
| `?` _keyword_ `:` _parameter_ `,` _keywords_ # Optional keyword
|
226
|
+
|
227
|
+
_var-name_ ::= /[a-z]\w*/
|
228
|
+
```
|
229
|
+
|
230
|
+
### Parameters
|
231
|
+
|
232
|
+
A parameter can be a type or a pair of type and variable name.
|
233
|
+
Variable name can be used for documentation.
|
234
|
+
|
235
|
+
### Examples
|
236
|
+
|
237
|
+
```
|
238
|
+
# Two required positional `Integer` parameters, and returns `String`
|
239
|
+
(Integer, Integer) -> String
|
240
|
+
|
241
|
+
# Two optional parameters `size` and `name`.
|
242
|
+
# `name` is a optional parameter with optional type so that developer can omit, pass a string, or pass `nil`.
|
243
|
+
(?Integer size, ?String? name) -> String
|
244
|
+
|
245
|
+
# Method type with a rest parameter
|
246
|
+
(*Integer, Integer) -> void
|
247
|
+
|
248
|
+
# `size` is a required keyword, with variable name of `sz`.
|
249
|
+
# `name` is a optional keyword.
|
250
|
+
# `created_at` is a optional keyword, and the value can be `nil`.
|
251
|
+
(size: Integer sz, ?name: String, ?created_at: Time?) -> void
|
252
|
+
```
|
253
|
+
|
254
|
+
## Members
|
255
|
+
|
256
|
+
```markdown
|
257
|
+
_member_ ::= _ivar-member_ # Ivar definition
|
258
|
+
| _method-member_ # Method definition
|
259
|
+
| _attribute-member_ # Attribute definition
|
260
|
+
| _include-member_ # Mixin (include)
|
261
|
+
| _extend-member_ # Mixin (extend)
|
262
|
+
| _prepend-member_ # Mixin (prepend)
|
263
|
+
| _alias-member_ # Alias
|
264
|
+
| `public` # Public
|
265
|
+
| `private` # Private
|
266
|
+
|
267
|
+
_ivar-member_ ::= _ivar-name_ `:` _type_
|
268
|
+
|
269
|
+
_method-member_ ::= `def` _method-name_ `:` _method-types_ # Instance method
|
270
|
+
| `def self.` _method-name_ `:` _method-types_ # Singleton method
|
271
|
+
| `def self?.` _method-name_ `:` _method-types_ # Singleton and instance method
|
272
|
+
|
273
|
+
_method-types_ ::= # Empty
|
274
|
+
| `super` # `super` overloading
|
275
|
+
| _type-parameters_ _method-type_ `|` _method-types_ # Overloading types
|
276
|
+
|
277
|
+
_type-parameters_ ::= # Empty
|
278
|
+
| `[` _type-variable_ `,` ... `]`
|
279
|
+
|
280
|
+
_attribute-member_ ::= _attribute-type_ _method-name_ `:` _type_ # Attribute
|
281
|
+
| _attribute-type_ _method-name_ `(` _ivar-name_ `) :` _type_ # Attribute with variable name specification
|
282
|
+
| _attribute-type_ _method-name_ `() :` _type_ # Attribute without variable
|
283
|
+
|
284
|
+
_attribute-type_ ::= `attr_reader` | `attr_writer` | `attr_accessor`
|
285
|
+
|
286
|
+
_include-member_ ::= `include` _class-name_ _type-arguments_
|
287
|
+
| `include` _interface-name_ _type-arguments_
|
288
|
+
_extend-member_ ::= `extend` _class-name_ _type-arguments_
|
289
|
+
| `extend` _interface-name_ _type-arguments_
|
290
|
+
_prepend-member_ ::= `prepend` _class-name_ _type-arguments_
|
291
|
+
|
292
|
+
_alias-member_ ::= `alias` _method-name_ _method-name_
|
293
|
+
| `alias self.` _method-name_ `self.` _method-name_
|
294
|
+
|
295
|
+
_ivar-name_ ::= /@\w+/
|
296
|
+
_method-name_ ::= ...
|
297
|
+
| /`[^`]+`/
|
298
|
+
```
|
299
|
+
|
300
|
+
### Ivar definition
|
301
|
+
|
302
|
+
An instance variable definition consists of the name of an instance variable and its type.
|
303
|
+
|
304
|
+
```
|
305
|
+
@name: String
|
306
|
+
@value: Hash[Symbol, Key]
|
307
|
+
```
|
308
|
+
|
309
|
+
### Method definition
|
310
|
+
|
311
|
+
Method definition has several syntax variations.
|
312
|
+
|
313
|
+
You can write `self.` or `self?.` before the name of the method to specify the kind of method: instance, singleton, or both instance and singleton.
|
314
|
+
|
315
|
+
```
|
316
|
+
def to_s: () -> String # Defines a instance method
|
317
|
+
def self.new: () -> AnObject # Defines singleton method
|
318
|
+
def self?.sqrt: (Numeric) -> Numeric # self? is for `module_function`s
|
319
|
+
```
|
320
|
+
|
321
|
+
The method type can be connected with `|`s to define an overloaded method.
|
322
|
+
|
323
|
+
```
|
324
|
+
def +: (Float) -> Float
|
325
|
+
| (Integer) -> Integer
|
326
|
+
| (Numeric) -> Numeric
|
327
|
+
```
|
328
|
+
|
329
|
+
You need extra parentheses on return type to avoid ambiguity.
|
330
|
+
|
331
|
+
```
|
332
|
+
def +: (Float | Integer) -> (Float | Integer)
|
333
|
+
| (Numeric) -> Numeric
|
334
|
+
```
|
335
|
+
|
336
|
+
Method types can end with `super` which means the methods from existing definitions.
|
337
|
+
This is useful to define an _extension_, which adds a new variation to the existing method preserving the original behavior.
|
338
|
+
|
339
|
+
### Attribute definition
|
340
|
+
|
341
|
+
Attribute definitions help to define methods and instance variables based on the convention of `attr_reader`, `attr_writer` and `attr_accessor` methods in Ruby.
|
342
|
+
|
343
|
+
You can specify the name of instance variable using `(@some_name)` syntax and also omit the instance variable definition by specifying `()`.
|
344
|
+
|
345
|
+
```
|
346
|
+
# Defines `id` method and `@id` instance variable.
|
347
|
+
attr_reader id: Integer
|
348
|
+
# @id: Integer
|
349
|
+
# def id: () -> Integer
|
350
|
+
|
351
|
+
# Defines `name=` method and `raw_name` instance variable.
|
352
|
+
attr_writer name (@raw_name) : String
|
353
|
+
# @raw_name: String
|
354
|
+
# def name=: (String) -> String
|
355
|
+
|
356
|
+
# Defines `people` and `people=` methods, but no instance variable.
|
357
|
+
attr_accessor people (): Array[Person]
|
358
|
+
# def people: () -> Array[Person]
|
359
|
+
# def people=: (Array[Person]) -> Array[Person]
|
360
|
+
```
|
361
|
+
|
362
|
+
### Mixin (include), Mixin (extend), Mixin (prepend)
|
363
|
+
|
364
|
+
You can define mixins between class and modules.
|
365
|
+
|
366
|
+
```
|
367
|
+
include Kernel
|
368
|
+
include Enumerable[String, void]
|
369
|
+
extend ActiveSupport::Concern
|
370
|
+
```
|
371
|
+
|
372
|
+
You can also `include` or `extend` an interface.
|
373
|
+
|
374
|
+
```
|
375
|
+
include _Hashing
|
376
|
+
extend _LikeString
|
377
|
+
```
|
378
|
+
|
379
|
+
This allows importing `def`s from the interface to help developer implementing a set of methods.
|
380
|
+
|
381
|
+
### Alias
|
382
|
+
|
383
|
+
You can define an alias between methods.
|
384
|
+
|
385
|
+
```
|
386
|
+
def map: [X] () { (String) -> X } -> Array[X]
|
387
|
+
alias collect map # `#collect` has the same type with `map`
|
388
|
+
```
|
389
|
+
|
390
|
+
### `public`, `private`
|
391
|
+
|
392
|
+
`public` and `private` allows specifying the visibility of methods.
|
393
|
+
|
394
|
+
These work only as _statements_, not per-method specifier.
|
395
|
+
|
396
|
+
## Declarations
|
397
|
+
|
398
|
+
```markdown
|
399
|
+
_decl_ ::= _class-decl_ # Class declaration
|
400
|
+
| _module-decl_ # Module declaration
|
401
|
+
| _interface-decl_ # Interface declaration
|
402
|
+
| _extension-decl_ # Extension declaration
|
403
|
+
| _type-alias-decl_ # Type alias declaration
|
404
|
+
| _const-decl_ # Constant declaration
|
405
|
+
| _global-decl_ # Global declaration
|
406
|
+
|
407
|
+
_class-decl_ ::= `class` _class-name_ _module-type-parameters_ _members_ `end`
|
408
|
+
| `class` _class-name_ _module-type-parameters_ `<` _class-name_ _type-arguments_ _members_ `end`
|
409
|
+
|
410
|
+
_module-decl_ ::= `module` _module-name_ _module-type-parameters_ _members_ `end`
|
411
|
+
| `module` _module-name_ _module-type-parameters_ `:` _class-name_ _type-arguments_ _members_ `end`
|
412
|
+
|
413
|
+
_interface-decl_ ::= `interface` _interface-name_ _module-type-parameters_ _interface-members_ `end`
|
414
|
+
|
415
|
+
_interface-members_ ::= _method-member_ # Method
|
416
|
+
| _include-member_ # Mixin (include)
|
417
|
+
| _alias-member_ # Alias
|
418
|
+
|
419
|
+
_extension-decl_ ::= `extension` _class-name_ _type-parameters_ `(` _extension-name_ `)` _members_ `end`
|
420
|
+
|
421
|
+
_type-alias-decl_ ::= `type` _alias-name_ `=` _type_
|
422
|
+
|
423
|
+
_const-decl_ ::= _const-name_ `:` _type_
|
424
|
+
|
425
|
+
_global-decl_ ::= _global-name_ `:` _type_
|
426
|
+
|
427
|
+
_const-name_ ::= _namespace_ /[A-Z]\w*/
|
428
|
+
_global-name_ ::= /$[a-zA-Z]\w+/ | ...
|
429
|
+
|
430
|
+
_module-type-parameters_ ::= # Empty
|
431
|
+
| `[` _module-type-parameter_ `,` ... `]`
|
432
|
+
|
433
|
+
_module-type-parameter_ ::= _variance_ _type-variable_
|
434
|
+
_variance_ ::= `out` | `in`
|
435
|
+
```
|
436
|
+
|
437
|
+
### Class declaration
|
438
|
+
|
439
|
+
Class declaration can have type parameters and superclass. When you omit superclass, `::Object` is assumed.
|
440
|
+
|
441
|
+
```
|
442
|
+
class Ref[A] < Object
|
443
|
+
attr_reader value: A
|
444
|
+
def initialize: (value: A) -> void
|
445
|
+
end
|
446
|
+
```
|
447
|
+
|
448
|
+
### Module declaration
|
449
|
+
|
450
|
+
Module declaration takes optional _self type_ parameter, which defines a constraint about a class when the module is mixed.
|
451
|
+
|
452
|
+
```
|
453
|
+
interface _Each[A, B]
|
454
|
+
def each: { (A) -> void } -> B
|
455
|
+
end
|
456
|
+
|
457
|
+
module Enumerable[A, B] : _Each[A, B]
|
458
|
+
def count: () -> Integer
|
459
|
+
end
|
460
|
+
```
|
461
|
+
|
462
|
+
The `Enumerable` method above requires `each` method for enumerating objects.
|
463
|
+
|
464
|
+
### Interface declaration
|
465
|
+
|
466
|
+
Interface declaration can have parameters but allows only a few of the members.
|
467
|
+
|
468
|
+
```
|
469
|
+
interface _Hashing
|
470
|
+
def hash: () -> Integer
|
471
|
+
def eql?: (any) -> bool
|
472
|
+
end
|
473
|
+
```
|
474
|
+
|
475
|
+
There are several limitations which are not described in the grammar.
|
476
|
+
|
477
|
+
1. Interface cannot `include` modules
|
478
|
+
2. Interface cannot have singleton method definitions
|
479
|
+
|
480
|
+
```
|
481
|
+
interface _Foo
|
482
|
+
include Bar # Error: cannot include modules
|
483
|
+
def self.new: () -> Foo # Error: cannot include singleton method definitions
|
484
|
+
end
|
485
|
+
```
|
486
|
+
|
487
|
+
### Extension declaration
|
488
|
+
|
489
|
+
Extension is to model _open class_.
|
490
|
+
|
491
|
+
```
|
492
|
+
extension Kernel (Pathname)
|
493
|
+
def Pathname: (String) -> Pathname
|
494
|
+
end
|
495
|
+
|
496
|
+
extension Array[A] (ActiveSupport)
|
497
|
+
def to: (Integer) -> Array[A]
|
498
|
+
def from: (Integer) -> Array[A]
|
499
|
+
def second: () -> A?
|
500
|
+
def third: () -> A?
|
501
|
+
end
|
502
|
+
```
|
503
|
+
|
504
|
+
### Type alias declaration
|
505
|
+
|
506
|
+
You can declare an alias of types.
|
507
|
+
|
508
|
+
```
|
509
|
+
type subject = Attendee | Speaker
|
510
|
+
type JSON::t = Integer | TrueClass | FalseClass | String | Hash[Symbol, t] | Array[t]
|
511
|
+
```
|
512
|
+
|
513
|
+
### Constant type declaration
|
514
|
+
|
515
|
+
You can declare a constant.
|
516
|
+
|
517
|
+
```
|
518
|
+
Person::DefaultEmailAddress: String
|
519
|
+
```
|
520
|
+
|
521
|
+
### Global type declaration
|
522
|
+
|
523
|
+
You can declare a global variable.
|
524
|
+
|
525
|
+
```
|
526
|
+
$LOAD_PATH: Array[String]
|
527
|
+
```
|
528
|
+
|