rbs 0.3.1 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +7 -1
  3. data/.gitignore +1 -1
  4. data/CHANGELOG.md +39 -0
  5. data/COPYING +1 -1
  6. data/Gemfile +16 -2
  7. data/README.md +87 -48
  8. data/Rakefile +54 -22
  9. data/bin/rbs-prof +9 -0
  10. data/bin/run_in_md.rb +49 -0
  11. data/bin/test_runner.rb +0 -2
  12. data/docs/sigs.md +6 -6
  13. data/docs/stdlib.md +3 -5
  14. data/docs/syntax.md +6 -3
  15. data/goodcheck.yml +65 -0
  16. data/lib/rbs.rb +3 -0
  17. data/lib/rbs/ast/declarations.rb +115 -14
  18. data/lib/rbs/ast/members.rb +41 -17
  19. data/lib/rbs/cli.rb +315 -122
  20. data/lib/rbs/constant.rb +4 -4
  21. data/lib/rbs/constant_table.rb +51 -45
  22. data/lib/rbs/definition.rb +175 -59
  23. data/lib/rbs/definition_builder.rb +802 -604
  24. data/lib/rbs/environment.rb +352 -210
  25. data/lib/rbs/environment_walker.rb +14 -23
  26. data/lib/rbs/errors.rb +184 -3
  27. data/lib/rbs/factory.rb +14 -0
  28. data/lib/rbs/parser.y +95 -27
  29. data/lib/rbs/prototype/rb.rb +119 -117
  30. data/lib/rbs/prototype/rbi.rb +5 -3
  31. data/lib/rbs/prototype/runtime.rb +34 -7
  32. data/lib/rbs/substitution.rb +12 -1
  33. data/lib/rbs/test.rb +82 -3
  34. data/lib/rbs/test/errors.rb +5 -1
  35. data/lib/rbs/test/hook.rb +133 -259
  36. data/lib/rbs/test/observer.rb +17 -0
  37. data/lib/rbs/test/setup.rb +35 -19
  38. data/lib/rbs/test/setup_helper.rb +29 -0
  39. data/lib/rbs/test/spy.rb +0 -321
  40. data/lib/rbs/test/tester.rb +116 -0
  41. data/lib/rbs/test/type_check.rb +43 -7
  42. data/lib/rbs/type_name_resolver.rb +58 -0
  43. data/lib/rbs/types.rb +94 -2
  44. data/lib/rbs/validator.rb +51 -0
  45. data/lib/rbs/variance_calculator.rb +12 -2
  46. data/lib/rbs/version.rb +1 -1
  47. data/lib/rbs/writer.rb +127 -91
  48. data/rbs.gemspec +0 -9
  49. data/schema/annotation.json +14 -0
  50. data/schema/comment.json +26 -0
  51. data/schema/decls.json +353 -0
  52. data/schema/function.json +87 -0
  53. data/schema/location.json +56 -0
  54. data/schema/members.json +248 -0
  55. data/schema/methodType.json +44 -0
  56. data/schema/types.json +299 -0
  57. data/stdlib/benchmark/benchmark.rbs +151 -151
  58. data/stdlib/builtin/encoding.rbs +2 -0
  59. data/stdlib/builtin/enumerable.rbs +4 -4
  60. data/stdlib/builtin/enumerator.rbs +3 -1
  61. data/stdlib/builtin/fiber.rbs +5 -1
  62. data/stdlib/builtin/file.rbs +0 -3
  63. data/stdlib/builtin/io.rbs +4 -4
  64. data/stdlib/builtin/proc.rbs +1 -2
  65. data/stdlib/builtin/symbol.rbs +1 -1
  66. data/stdlib/builtin/thread.rbs +2 -2
  67. data/stdlib/csv/csv.rbs +4 -6
  68. data/stdlib/fiber/fiber.rbs +117 -0
  69. data/stdlib/json/json.rbs +1 -1
  70. data/stdlib/logger/formatter.rbs +23 -0
  71. data/stdlib/logger/log_device.rbs +39 -0
  72. data/stdlib/logger/logger.rbs +507 -0
  73. data/stdlib/logger/period.rbs +7 -0
  74. data/stdlib/logger/severity.rbs +8 -0
  75. data/stdlib/mutex_m/mutex_m.rbs +77 -0
  76. data/stdlib/pathname/pathname.rbs +6 -6
  77. data/stdlib/prime/integer-extension.rbs +1 -1
  78. data/stdlib/prime/prime.rbs +44 -44
  79. data/stdlib/pty/pty.rbs +159 -0
  80. data/stdlib/tmpdir/tmpdir.rbs +1 -1
  81. metadata +28 -116
  82. data/lib/rbs/test/test_helper.rb +0 -183
@@ -1,183 +0,0 @@
1
- module RBS
2
- module Test
3
- module TypeAssertions
4
- module ClassMethods
5
- attr_reader :target
6
-
7
- def library(*libs)
8
- @libs = libs
9
- @env = nil
10
- @target = nil
11
- end
12
-
13
- def env
14
- @env ||= begin
15
- loader = RBS::EnvironmentLoader.new
16
- (@libs || []).each do |lib|
17
- loader.add library: lib
18
- end
19
-
20
- RBS::Environment.new.tap do |env|
21
- loader.load(env: env)
22
- end
23
- end
24
- end
25
-
26
- def builder
27
- @builder ||= DefinitionBuilder.new(env: env)
28
- end
29
-
30
- def testing(type_or_string)
31
- type = case type_or_string
32
- when String
33
- RBS::Parser.parse_type(type_or_string, variables: [])
34
- else
35
- type_or_string
36
- end
37
-
38
- definition = case type
39
- when Types::ClassInstance
40
- builder.build_instance(type.name)
41
- when Types::ClassSingleton
42
- builder.build_singleton(type.name)
43
- else
44
- raise "Test target should be class instance or class singleton: #{type}"
45
- end
46
-
47
- @target = [type, definition]
48
- end
49
- end
50
-
51
- def self.included(base)
52
- base.extend ClassMethods
53
- end
54
-
55
- def env
56
- self.class.env
57
- end
58
-
59
- def builder
60
- self.class.builder
61
- end
62
-
63
- def targets
64
- @targets ||= []
65
- end
66
-
67
- def target
68
- targets.last || self.class.target
69
- end
70
-
71
- def testing(type_or_string)
72
- type = case type_or_string
73
- when String
74
- RBS::Parser.parse_type(type_or_string, variables: [])
75
- else
76
- type_or_string
77
- end
78
-
79
- definition = case type
80
- when Types::ClassInstance
81
- builder.build_instance(type.name)
82
- when Types::ClassSingleton
83
- builder.build_singleton(type.name)
84
- else
85
- raise "Test target should be class instance or class singleton: #{type}"
86
- end
87
-
88
- targets.push [type, definition]
89
-
90
- if block_given?
91
- begin
92
- yield
93
- ensure
94
- targets.pop
95
- end
96
- else
97
- [type, definition]
98
- end
99
- end
100
-
101
- ruby2_keywords def assert_send_type(method_type, receiver, method, *args, &block)
102
- trace = []
103
- spy = Spy.wrap(receiver, method)
104
- spy.callback = -> (result) { trace << result }
105
-
106
- exception = nil
107
-
108
- begin
109
- spy.wrapped_object.__send__(method, *args, &block)
110
- rescue => exn
111
- exception = exn
112
- end
113
-
114
- mt = case method_type
115
- when String
116
- RBS::Parser.parse_method_type(method_type, variables: [])
117
- when RBS::MethodType
118
- method_type
119
- end
120
-
121
- typecheck = TypeCheck.new(self_class: receiver.class, builder: builder)
122
- errors = typecheck.method_call(method, mt, trace.last, errors: [])
123
-
124
- assert_empty errors.map {|x| RBS::Test::Errors.to_string(x) }, "Call trace does not match with given method type: #{trace.last.inspect}"
125
-
126
- type, definition = target
127
- method_types = case
128
- when definition.instance_type?
129
- subst = Substitution.build(definition.declaration.type_params.each.map(&:name),
130
- type.args)
131
- definition.methods[method].method_types.map do |method_type|
132
- method_type.sub(subst)
133
- end
134
- when definition.class_type?
135
- definition.methods[method].method_types
136
- end
137
-
138
- all_errors = method_types.map {|t| typecheck.method_call(method, t, trace.last, errors: []) }
139
- assert all_errors.any? {|es| es.empty? }, "Call trace does not match one of method definitions:\n #{trace.last.inspect}\n #{method_types.join(" | ")}"
140
-
141
- if exception
142
- raise exception
143
- end
144
- end
145
-
146
- ruby2_keywords def refute_send_type(method_type, receiver, method, *args, &block)
147
- trace = []
148
- spy = Spy.wrap(receiver, method)
149
- spy.callback = -> (result) { trace << result }
150
-
151
- exception = nil
152
- begin
153
- spy.wrapped_object.__send__(method, *args, &block)
154
- rescue Exception => exn
155
- exception = exn
156
- end
157
-
158
- mt = case method_type
159
- when String
160
- RBS::Parser.parse_method_type(method_type, variables: [])
161
- when RBS::MethodType
162
- method_type
163
- end
164
-
165
- mt = mt.update(block: if mt.block
166
- MethodType::Block.new(
167
- type: mt.block.type.with_return_type(Types::Bases::Any.new(location: nil)),
168
- required: mt.block.required
169
- )
170
- end,
171
- type: mt.type.with_return_type(Types::Bases::Any.new(location: nil)))
172
-
173
- typecheck = TypeCheck.new(self_class: receiver.class, builder: builder)
174
- errors = typecheck.method_call(method, mt, trace.last, errors: [])
175
-
176
- assert_operator exception, :is_a?, ::Exception
177
- assert_empty errors.map {|x| RBS::Test::Errors.to_string(x) }
178
-
179
- exception
180
- end
181
- end
182
- end
183
- end