rbs 0.12.0 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +10 -10
  3. data/.gitignore +0 -1
  4. data/CHANGELOG.md +24 -0
  5. data/Gemfile +3 -0
  6. data/README.md +8 -2
  7. data/Rakefile +9 -4
  8. data/Steepfile +1 -0
  9. data/bin/annotate-with-rdoc +1 -1
  10. data/bin/setup +0 -2
  11. data/bin/test_runner.rb +3 -6
  12. data/docs/CONTRIBUTING.md +1 -0
  13. data/goodcheck.yml +22 -5
  14. data/lib/rbs/ast/comment.rb +1 -1
  15. data/lib/rbs/cli.rb +12 -4
  16. data/lib/rbs/constant.rb +1 -1
  17. data/lib/rbs/constant_table.rb +9 -8
  18. data/lib/rbs/definition_builder.rb +4 -5
  19. data/lib/rbs/environment.rb +5 -1
  20. data/lib/rbs/environment_loader.rb +12 -12
  21. data/lib/rbs/namespace.rb +1 -1
  22. data/lib/rbs/parser.rb +3146 -0
  23. data/lib/rbs/parser.y +7 -2
  24. data/lib/rbs/test/setup_helper.rb +4 -4
  25. data/lib/rbs/test/type_check.rb +2 -2
  26. data/lib/rbs/type_name.rb +1 -1
  27. data/lib/rbs/variance_calculator.rb +2 -2
  28. data/lib/rbs/version.rb +1 -1
  29. data/lib/rbs/writer.rb +25 -15
  30. data/sig/constant.rbs +21 -0
  31. data/sig/constant_table.rbs +30 -0
  32. data/sig/declarations.rbs +1 -1
  33. data/sig/definition.rbs +1 -1
  34. data/sig/definition_builder.rbs +4 -5
  35. data/sig/environment_loader.rbs +54 -0
  36. data/sig/namespace.rbs +3 -3
  37. data/sig/parser.rbs +25 -0
  38. data/sig/substitution.rbs +3 -3
  39. data/sig/typename.rbs +1 -1
  40. data/sig/types.rbs +1 -1
  41. data/sig/version.rbs +3 -0
  42. data/sig/writer.rbs +40 -0
  43. data/stdlib/benchmark/benchmark.rbs +2 -2
  44. data/stdlib/builtin/basic_object.rbs +54 -54
  45. data/stdlib/builtin/binding.rbs +42 -42
  46. data/stdlib/builtin/class.rbs +33 -33
  47. data/stdlib/builtin/complex.rbs +90 -90
  48. data/stdlib/builtin/encoding.rbs +33 -33
  49. data/stdlib/builtin/enumerable.rbs +32 -32
  50. data/stdlib/builtin/enumerator.rbs +35 -35
  51. data/stdlib/builtin/errors.rbs +1 -1
  52. data/stdlib/builtin/exception.rbs +50 -50
  53. data/stdlib/builtin/false_class.rbs +6 -6
  54. data/stdlib/builtin/fiber.rbs +14 -14
  55. data/stdlib/builtin/fiber_error.rbs +1 -1
  56. data/stdlib/builtin/float.rbs +161 -161
  57. data/stdlib/builtin/gc.rbs +1 -1
  58. data/stdlib/builtin/io.rbs +83 -83
  59. data/stdlib/builtin/kernel.rbs +70 -68
  60. data/stdlib/builtin/match_data.rbs +1 -1
  61. data/stdlib/builtin/method.rbs +19 -19
  62. data/stdlib/builtin/nil_class.rbs +20 -20
  63. data/stdlib/builtin/numeric.rbs +101 -101
  64. data/stdlib/builtin/object.rbs +172 -172
  65. data/stdlib/builtin/proc.rbs +91 -91
  66. data/stdlib/builtin/range.rbs +2 -4
  67. data/stdlib/builtin/rational.rbs +83 -83
  68. data/stdlib/builtin/signal.rbs +7 -7
  69. data/stdlib/builtin/string.rbs +4 -4
  70. data/stdlib/builtin/string_io.rbs +1 -1
  71. data/stdlib/builtin/thread.rbs +185 -185
  72. data/stdlib/builtin/thread_group.rbs +2 -2
  73. data/stdlib/builtin/true_class.rbs +9 -9
  74. data/stdlib/builtin/warning.rbs +1 -1
  75. data/stdlib/date/date.rbs +2 -2
  76. data/stdlib/find/find.rbs +10 -10
  77. data/stdlib/pathname/pathname.rbs +2 -0
  78. data/stdlib/pty/pty.rbs +5 -29
  79. data/stdlib/tmpdir/tmpdir.rbs +12 -12
  80. data/stdlib/uri/generic.rbs +1 -1
  81. data/stdlib/uri/http.rbs +158 -0
  82. data/stdlib/uri/https.rbs +108 -0
  83. data/stdlib/uri/ldap.rbs +224 -0
  84. data/stdlib/uri/ldaps.rbs +108 -0
  85. data/steep/Gemfile.lock +13 -17
  86. metadata +12 -3
@@ -1,5 +1,6 @@
1
1
  class RBS::Parser
2
- token tUIDENT tLIDENT tNAMESPACE tINTERFACEIDENT tLKEYWORD tUKEYWORD tGLOBALIDENT
2
+ token tUIDENT tLIDENT tNAMESPACE tINTERFACEIDENT tGLOBALIDENT
3
+ tLKEYWORD tUKEYWORD tLKEYWORD_Q_E tUKEYWORD_Q_E
3
4
  tIVAR tCLASSVAR
4
5
  tANNOTATION
5
6
  tSTRING tSYMBOL tINTEGER tWRITE_ATTR
@@ -846,7 +847,7 @@ rule
846
847
  result = val[0]
847
848
  }
848
849
 
849
- keyword: tLKEYWORD | tUKEYWORD
850
+ keyword: tLKEYWORD | tUKEYWORD | tLKEYWORD_Q_E | tUKEYWORD_Q_E
850
851
 
851
852
  function_type:
852
853
  kLPAREN params kRPAREN kARROW simple_type {
@@ -1344,8 +1345,12 @@ def next_token
1344
1345
  new_token(:tNAMESPACE)
1345
1346
  when input.scan(/[a-z_]\w*:/)
1346
1347
  new_token(:tLKEYWORD, input.matched.chop.to_sym)
1348
+ when input.scan(/[a-z_]\w*[?!]:/)
1349
+ new_token(:tLKEYWORD_Q_E, input.matched.chop.to_sym)
1347
1350
  when input.scan(/[A-Z]\w*:/)
1348
1351
  new_token(:tUKEYWORD, input.matched.chop.to_sym)
1352
+ when input.scan(/[A-Z]\w*[?!]:/)
1353
+ new_token(:tUKEYWORD_Q_E, input.matched.chop.to_sym)
1349
1354
  when input.scan(/\$[A-Za-z_]\w*/)
1350
1355
  new_token(:tGLOBALIDENT)
1351
1356
  when input.scan(/@[a-zA-Z_]\w*/)
@@ -3,15 +3,15 @@ module RBS
3
3
  module SetupHelper
4
4
  class InvalidSampleSizeError < StandardError
5
5
  attr_reader :string
6
-
6
+
7
7
  def initialize(string)
8
8
  @string = string
9
9
  super("Sample size should be a positive integer: `#{string}`")
10
10
  end
11
11
  end
12
-
12
+
13
13
  DEFAULT_SAMPLE_SIZE = 100
14
-
14
+
15
15
  def get_sample_size(string)
16
16
  case string
17
17
  when ""
@@ -32,7 +32,7 @@ module RBS
32
32
  when 'rspec'
33
33
  ['::RSpec::Mocks::Double']
34
34
  when 'minitest'
35
- ['::Minitest::Mock']
35
+ ['::Minitest::Mock']
36
36
  else
37
37
  RBS.logger.warn "Unknown test suite - defaults to nil"
38
38
  nil
@@ -212,7 +212,7 @@ module RBS
212
212
  def value(val, type)
213
213
  if is_double?(val)
214
214
  RBS.logger.info("A double (#{val.inspect}) is detected!")
215
- return true
215
+ return true
216
216
  end
217
217
 
218
218
  case type
@@ -306,7 +306,7 @@ module RBS
306
306
  Test.call(val, IS_AP, ::Array) &&
307
307
  type.types.map.with_index {|ty, index| value(val[index], ty) }.all?
308
308
  when Types::Record
309
- Test::call(val, IS_AP, ::Hash) &&
309
+ Test::call(val, IS_AP, ::Hash) &&
310
310
  type.fields.map {|key, type| value(val[key], type) }.all?
311
311
  when Types::Proc
312
312
  Test::call(val, IS_AP, ::Proc)
@@ -23,7 +23,7 @@ module RBS
23
23
  def ==(other)
24
24
  other.is_a?(self.class) && other.namespace == namespace && other.name == name
25
25
  end
26
-
26
+
27
27
  alias eql? ==
28
28
 
29
29
  def hash
@@ -120,7 +120,7 @@ module RBS
120
120
 
121
121
  type.args.each.with_index do |ty, i|
122
122
  var = type_params.params[i]
123
- case var.variance
123
+ case var&.variance
124
124
  when :invariant
125
125
  type(ty, result: result, context: :invariant)
126
126
  when :covariant
@@ -135,7 +135,7 @@ module RBS
135
135
  when :contravariant
136
136
  :covariant
137
137
  else
138
- raise
138
+ raise
139
139
  end
140
140
  type(ty, result: result, context: con)
141
141
  end
@@ -1,3 +1,3 @@
1
1
  module RBS
2
- VERSION = "0.12.0"
2
+ VERSION = "0.14.0"
3
3
  end
@@ -60,6 +60,8 @@ module RBS
60
60
 
61
61
  def write(decls)
62
62
  [nil, *decls].each_cons(2) do |prev, decl|
63
+ raise unless decl
64
+
63
65
  preserve_empty_line(prev, decl)
64
66
  write_decl decl
65
67
  end
@@ -68,8 +70,8 @@ module RBS
68
70
  def write_decl(decl)
69
71
  case decl
70
72
  when AST::Declarations::Class
71
- super_class = if decl.super_class
72
- " < #{name_and_args(decl.super_class.name, decl.super_class.args)}"
73
+ super_class = if super_class = decl.super_class
74
+ " < #{name_and_args(super_class.name, super_class.args)}"
73
75
  end
74
76
  write_comment decl.comment
75
77
  write_annotation decl.annotations
@@ -77,6 +79,8 @@ module RBS
77
79
 
78
80
  indent do
79
81
  [nil, *decl.members].each_cons(2) do |prev, member|
82
+ raise unless member
83
+
80
84
  preserve_empty_line prev, member
81
85
  write_member member
82
86
  end
@@ -134,9 +138,6 @@ module RBS
134
138
 
135
139
  puts "end"
136
140
 
137
- when AST::Declarations::Extension
138
- RBS.logger.warn "Extension is ignored: #{decl.name}"
139
-
140
141
  end
141
142
  end
142
143
 
@@ -288,20 +289,29 @@ module RBS
288
289
  end
289
290
 
290
291
  def preserve_empty_line(prev, decl)
291
- return unless prev
292
+ # @type var decl: _Located
292
293
 
293
- decl = decl.comment if decl.respond_to?(:comment) && decl.comment
294
+ return unless prev
294
295
 
295
- # When the signature is not constructed by the parser,
296
- # it always inserts an empty line.
297
- if !prev.location || !decl.location
298
- puts
299
- return
296
+ if (_ = decl).respond_to?(:comment)
297
+ if comment = (_ = decl).comment
298
+ decl = comment
299
+ end
300
300
  end
301
301
 
302
- prev_end_line = prev.location.end_line
303
- start_line = decl.location.start_line
304
- if start_line - prev_end_line > 1
302
+ prev_loc = prev.location
303
+ decl_loc = decl.location
304
+
305
+ if prev_loc && decl_loc
306
+ prev_end_line = prev_loc.end_line
307
+ start_line = decl_loc.start_line
308
+
309
+ if start_line - prev_end_line > 1
310
+ puts
311
+ end
312
+ else
313
+ # When the signature is not constructed by the parser,
314
+ # it always inserts an empty line.
305
315
  puts
306
316
  end
307
317
  end
@@ -0,0 +1,21 @@
1
+ module RBS
2
+ class Constant
3
+ type constant_entry = Environment::ClassEntry
4
+ | Environment::ModuleEntry
5
+ | Environment::SingleEntry[TypeName, AST::Declarations::Constant]
6
+
7
+ attr_reader name: TypeName
8
+
9
+ attr_reader type: Types::t
10
+
11
+ attr_reader entry: constant_entry
12
+
13
+ def initialize: (name: TypeName, type: Types::t, entry: constant_entry) -> void
14
+
15
+ def ==: (untyped other) -> bool
16
+
17
+ alias eql? ==
18
+
19
+ def hash: () -> Integer
20
+ end
21
+ end
@@ -0,0 +1,30 @@
1
+ module RBS
2
+ class ConstantTable
3
+ attr_reader definition_builder: DefinitionBuilder
4
+ attr_reader constant_scopes_cache: Hash[TypeName, Array[Namespace]]
5
+ attr_reader resolver: TypeNameResolver
6
+ attr_reader env(): Environment
7
+
8
+ def initialize: (builder: DefinitionBuilder) -> void
9
+
10
+ def absolute_type: (Types::t, context: Array[Namespace]) -> Types::t
11
+
12
+ def absolute_type_name: (TypeName, context: Array[Namespace], location: Location?) -> TypeName
13
+
14
+ def name_to_constant: (TypeName) -> Constant?
15
+
16
+ def split_name: (TypeName) -> Array[Symbol]
17
+
18
+ def resolve_constant_reference: (TypeName name, context: Array[Namespace]) -> Constant?
19
+
20
+ def resolve_constant_reference_context: (Symbol, context: Array[Namespace]) -> Constant?
21
+
22
+ def resolve_constant_reference_inherit: (Symbol, scopes: Array[Namespace], ?no_object: bool) -> Constant?
23
+
24
+ def constant_scopes: (TypeName) -> Array[Namespace]
25
+
26
+ def constant_scopes_module: (TypeName, scopes: Array[Namespace]) -> Array[Namespace]
27
+
28
+ def constant_scopes0: (TypeName, ?scopes: Array[Namespace]) -> Array[Namespace]
29
+ end
30
+ end
@@ -2,7 +2,7 @@ module RBS
2
2
  module AST
3
3
  module Declarations
4
4
  type t = Class | Module | Interface | Constant | Global | Alias
5
-
5
+
6
6
  class Base
7
7
  end
8
8
 
@@ -28,7 +28,7 @@ module RBS
28
28
  def annotations: () -> Array[AST::Annotation]
29
29
 
30
30
  def update: (?type: MethodType, ?member: method_member, ?defined_in: TypeName?, ?implemented_in: TypeName?) -> TypeDef
31
-
31
+
32
32
  def overload?: () -> bool
33
33
  end
34
34
 
@@ -29,14 +29,14 @@ module RBS
29
29
 
30
30
  attr_reader env: Environment
31
31
  attr_reader type_name_resolver: TypeNameResolver
32
-
32
+
33
33
  attr_reader instance_cache: Hash[TypeName, Definition | false | nil]
34
34
  attr_reader singleton_cache: Hash[TypeName, Definition | false | nil]
35
35
  attr_reader interface_cache: Hash[TypeName, Definition | false | nil]
36
36
 
37
37
  attr_reader one_instance_cache: Hash[TypeName, Definition]
38
38
  attr_reader one_singleton_cache: Hash[TypeName, Definition]
39
-
39
+
40
40
  attr_reader instance_ancestors_cache: Hash[TypeName, Definition::InstanceAncestors]
41
41
  attr_reader singleton_ancestor_cache: Hash[TypeName, Definition::SingletonAncestors]
42
42
 
@@ -48,7 +48,7 @@ module RBS
48
48
  def validate_super_class!: (TypeName, Environment::ClassEntry) -> void
49
49
 
50
50
  def one_instance_ancestors: (TypeName) -> OneAncestors
51
-
51
+
52
52
  def one_singleton_ancestors: (TypeName) -> OneAncestors
53
53
 
54
54
  def instance_ancestors: (TypeName, ?building_ancestors: Array[Definition::Ancestor::t]) -> Definition::InstanceAncestors
@@ -69,7 +69,7 @@ module RBS
69
69
 
70
70
  def build_one_singleton: (TypeName) -> Definition
71
71
 
72
- def merge_definitions: (TypeName,
72
+ def merge_definitions: (TypeName,
73
73
  Array[[Definition::Ancestor::t, Definition]],
74
74
  entry: Environment::ModuleEntry | Environment::ClassEntry,
75
75
  self_type: Definition::self_type,
@@ -92,4 +92,3 @@ module RBS
92
92
  def expand_alias: (TypeName) -> Types::t
93
93
  end
94
94
  end
95
-
@@ -1,4 +1,58 @@
1
1
  module RBS
2
2
  class EnvironmentLoader
3
+ class UnknownLibraryNameError < StandardError
4
+ attr_reader name: String
5
+
6
+ def initialize: (name: String) -> void
7
+ end
8
+
9
+ class LibraryPath
10
+ attr_reader name: String
11
+ attr_reader path: Pathname
12
+
13
+ def initialize: (name: String, path: Pathname) -> void
14
+ end
15
+
16
+ class GemPath
17
+ attr_reader name: String
18
+ attr_reader version: String?
19
+ attr_reader path: Pathname
20
+
21
+ def initialize: (name: String, version: String?, path: Pathname) -> void
22
+ end
23
+
24
+ STDLIB_ROOT: Pathname
25
+
26
+ type path = Pathname | LibraryPath | GemPath
27
+
28
+ attr_reader paths: Array[path]
29
+ attr_reader stdlib_root: Pathname
30
+ attr_reader gem_vendor_path: Pathname?
31
+
32
+ def self.gem_sig_path: (String, String?) -> Pathname?
33
+
34
+ def initialize: (?stdlib_root: Pathname, ?gem_vendor_path: Pathname?) -> void
35
+
36
+ def add: (path: Pathname?) -> void
37
+ | (library: String?) -> void
38
+
39
+ def self.parse_library: (String) -> [String, String?]
40
+
41
+ def stdlib?: (String) -> Pathname?
42
+
43
+ def gem?: (String, String?) -> Pathname?
44
+
45
+ def each_signature: (Pathname, ?immediate: bool) { (Pathname) -> void } -> void
46
+ | (Pathname, ?immediate: bool) -> Enumerator[Pathname, void]
47
+
48
+ def each_library_path: { (path, Pathname) -> void } -> void
49
+
50
+ def no_builtin!: (?bool) -> self
51
+
52
+ def no_builtin?: () -> bool
53
+
54
+ def each_decl: () { (AST::Declarations::t, Buffer, Pathname, path | :stdlib) -> void } -> void
55
+
56
+ def load: (env: Environment) -> Array[[AST::Declarations::t, Pathname, path | :stdlib]]
3
57
  end
4
58
  end
@@ -1,6 +1,6 @@
1
1
  module RBS
2
2
  # Namespace instance represents a _prefix of module names_.
3
- #
3
+ #
4
4
  # vvvvvvvvvvvvvv TypeName
5
5
  # RBS::Namespace
6
6
  # ^^^^^ Namespace
@@ -44,7 +44,7 @@ module RBS
44
44
  # If `other` is an absolute namespace, it returns `other`.
45
45
  #
46
46
  # Namespace("Foo::") + Namespace("::Bar::") # => ::Bar::
47
- #
47
+ #
48
48
  def +: (Namespace other) -> Namespace
49
49
 
50
50
  # Add one path component to self.
@@ -62,7 +62,7 @@ module RBS
62
62
 
63
63
  # Returns true if self is absolute namespace.
64
64
  def absolute?: () -> bool
65
-
65
+
66
66
  # Returns true if self is relative namespace.
67
67
  def relative?: () -> bool
68
68
 
@@ -0,0 +1,25 @@
1
+ module RBS
2
+ class Parser
3
+ class SyntaxError < StandardError
4
+ attr_reader token_str: String
5
+ attr_reader error_value: untyped
6
+ attr_reader value_stack: untyped?
7
+
8
+ def initialize: (token_str: String, error_value: untyped, ?value_stack: untyped?) -> void
9
+ end
10
+
11
+ class SemanticsError < StandardError
12
+ attr_reader subject: untyped
13
+ attr_reader location: Location
14
+ attr_reader original_message: String
15
+
16
+ def initialize: (String message, subject: untyped, location: Location) -> void
17
+ end
18
+
19
+ def self.parse_method_type: (String | Buffer, ?variables: Array[Symbol], ?eof_re: Regexp?) -> MethodType
20
+
21
+ def self.parse_type: (String | Buffer, ?variables: Array[Symbol], ?eof_re: Regexp?) -> Types::t
22
+
23
+ def self.parse_signature: (String | Buffer, ?eof_re: Regexp?) -> Array[AST::Declarations::t]
24
+ end
25
+ end
@@ -1,6 +1,6 @@
1
1
  module RBS
2
2
  # Substitution from type variables to types.
3
- #
3
+ #
4
4
  # The substitution construction is in _destructive_ manner.
5
5
  #
6
6
  # sub = Substitution.new
@@ -25,7 +25,7 @@ module RBS
25
25
  # Utility method to construct a substitution.
26
26
  # Raises an error when `variables.size != types.size`.
27
27
  # `instance_type` defaults to `nil`.
28
- #
28
+ #
29
29
  # Yields types in `types` and the block value is used if block is given.
30
30
  #
31
31
  def self.build: (Array[Symbol] variables, Array[Types::t] types, ?instance_type: Types::t?) ?{ (Types::t) -> Types::t } -> instance
@@ -36,4 +36,4 @@ module RBS
36
36
  # Returns a substitution without variables given in `vars`.
37
37
  def without: (*Symbol vars) -> Substitution
38
38
  end
39
- end
39
+ end
@@ -1,6 +1,6 @@
1
1
  module RBS
2
2
  # TypeName represents name of types in RBS.
3
- #
3
+ #
4
4
  # TypeNames are one of the three kind, class, alias, and interface.
5
5
  # *class* type names corresponds to Ruby classes and modules.
6
6
  # There are no corresponding Ruby value to *alias* and *interface* type names.
@@ -154,7 +154,7 @@ module RBS
154
154
 
155
155
  include NoFreeVariables
156
156
  include NoSubst
157
- include EmptyEachType
157
+ include EmptyEachType
158
158
  end
159
159
 
160
160
  module Application