rbs-inline 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3e5b1f22a68a9b238d2e620d2694a3296b58ced7435e8e8035f000beb1ce40f6
4
- data.tar.gz: c81ba9017a9a54e2d8d4401d1ad8d6640fcfac87dd99ab060daad38df8fb3977
3
+ metadata.gz: 83b5fc9f8e331ee20d2e8dc61604dc9a21693dcf6869f79e29eebbc885c45554
4
+ data.tar.gz: 5c7a7abdc5acbf156bbd914ccaaa2eb86a54086ddf6562b6fc6f485921b120a0
5
5
  SHA512:
6
- metadata.gz: 0f54053824e26053cb0c229c06c1cb8987af68fae1e5340747cc4424f1b549ead53c6ec10cfc4287aaa0aea350848bbccd8c8baa70311c791d5b7300ae9b431b
7
- data.tar.gz: 52e6463d2240a0cc1373a15a326f18c1fd2c50ff22df210ed90e3dfd5cd635b43190e11321f8f903593b406b4ee7d3842f9ef3d3cc49a5a8735710b8e5f06901
6
+ metadata.gz: e4acaf6c3c40bcee7639b9d7f482b8e3acc47466c5069c15437c9f2205775938f6acba61ba4afe4612ab98a65dbeb4e2fd7894c6bc0071512edce0e89db76632
7
+ data.tar.gz: ab895615e1dab1d9ce750e55ee06499cfd224de8df9b754c592fd2224d8d89c33d2f64f14c312a9f61515e60627fd247f6d5646eb51d1b1731ed10b60b9c1524
data/README.md CHANGED
@@ -18,6 +18,8 @@ class Person
18
18
 
19
19
  attr_reader :addresses #: Array[String]
20
20
 
21
+ # You can write the type of parameters and return types.
22
+ #
21
23
  # @rbs name: String
22
24
  # @rbs addresses: Array[String]
23
25
  # @rbs return: void
@@ -26,10 +28,20 @@ class Person
26
28
  @addresses = addresses
27
29
  end
28
30
 
29
- def to_s #: String
31
+ # Or write the type of the method just after `@rbs` keyword.
32
+ #
33
+ # @rbs () -> String
34
+ def to_s
30
35
  "Person(name = #{name}, addresses = #{addresses.join(", ")})"
31
36
  end
32
37
 
38
+ # The `:` syntax is the shortest one.
39
+ #
40
+ #: () -> String
41
+ def hash
42
+ [name, addresses].hash
43
+ end
44
+
33
45
  # @rbs &block: (String) -> void
34
46
  def each_address(&block) #: void
35
47
  addresses.each(&block)
@@ -482,7 +482,7 @@ module RBS
482
482
  case last_token
483
483
  when Array
484
484
  # `*` clause
485
- namespace = Namespace(token_strs.join)
485
+ namespace = RBS::Namespace.parse(token_strs.join)
486
486
  @clauses << RBS::AST::Directives::Use::WildcardClause.new(
487
487
  namespace: namespace,
488
488
  location: nil
@@ -189,7 +189,7 @@ module RBS
189
189
 
190
190
  # @rbs %a{pure}
191
191
  # @rbs return: Types::t
192
- def type
192
+ def type(default_type)
193
193
  if assertion
194
194
  case assertion.type
195
195
  when MethodType, nil
@@ -203,7 +203,7 @@ module RBS
203
203
  return literal
204
204
  end
205
205
 
206
- Types::Bases::Any.new(location: nil)
206
+ default_type
207
207
  end
208
208
 
209
209
  # @rbs %a{pure}
@@ -158,7 +158,8 @@ module RBS
158
158
  end
159
159
  end
160
160
 
161
- def method_overloads #: Array[RBS::AST::Members::MethodDefinition::Overload]
161
+ # @rbs (::RBS::Types::t default_type) -> Array[RBS::AST::Members::MethodDefinition::Overload]
162
+ def method_overloads(default_type)
162
163
  case
163
164
  when method_types = annotated_method_types
164
165
  method_types.map do |method_type|
@@ -181,7 +182,7 @@ module RBS
181
182
  when Prism::RequiredParameterNode
182
183
  required_positionals << Types::Function::Param.new(
183
184
  name: param.name,
184
- type: var_type_hash[param.name] || Types::Bases::Any.new(location: nil),
185
+ type: var_type_hash[param.name] || default_type,
185
186
  location: nil
186
187
  )
187
188
  end
@@ -192,7 +193,7 @@ module RBS
192
193
  when Prism::OptionalParameterNode
193
194
  optional_positionals << Types::Function::Param.new(
194
195
  name: param.name,
195
- type: var_type_hash[param.name] || Types::Bases::Any.new(location: nil),
196
+ type: var_type_hash[param.name] || default_type,
196
197
  location: nil
197
198
  )
198
199
  end
@@ -207,7 +208,7 @@ module RBS
207
208
 
208
209
  rest_positionals = Types::Function::Param.new(
209
210
  name: rest.name,
210
- type: splat_type || Types::Bases::Any.new(location: nil),
211
+ type: splat_type || default_type,
211
212
  location: nil
212
213
  )
213
214
  end
@@ -216,7 +217,7 @@ module RBS
216
217
  if node.is_a?(Prism::RequiredKeywordParameterNode)
217
218
  required_keywords[node.name] = Types::Function::Param.new(
218
219
  name: nil,
219
- type: var_type_hash[node.name] || Types::Bases::Any.new(location: nil),
220
+ type: var_type_hash[node.name] || default_type,
220
221
  location: nil
221
222
  )
222
223
  end
@@ -224,7 +225,7 @@ module RBS
224
225
  if node.is_a?(Prism::OptionalKeywordParameterNode)
225
226
  optional_keywords[node.name] = Types::Function::Param.new(
226
227
  name: nil,
227
- type: var_type_hash[node.name] || Types::Bases::Any.new(location: nil),
228
+ type: var_type_hash[node.name] || default_type,
228
229
  location: nil
229
230
  )
230
231
  end
@@ -239,13 +240,13 @@ module RBS
239
240
 
240
241
  rest_keywords = Types::Function::Param.new(
241
242
  name: kw_rest.name,
242
- type: double_splat_type || Types::Bases::Any.new(location: nil),
243
+ type: double_splat_type || default_type,
243
244
  location: nil)
244
245
  end
245
246
 
246
247
  if node.parameters.block
247
248
  block = Types::Block.new(
248
- type: Types::UntypedFunction.new(return_type: Types::Bases::Any.new(location: nil)),
249
+ type: Types::UntypedFunction.new(return_type: default_type),
249
250
  required: false,
250
251
  self_type: nil
251
252
  )
@@ -268,7 +269,7 @@ module RBS
268
269
  required_keywords: required_keywords,
269
270
  optional_keywords: optional_keywords,
270
271
  rest_keywords: rest_keywords,
271
- return_type: return_type || Types::Bases::Any.new(location: nil)
272
+ return_type: return_type || default_type
272
273
  ),
273
274
  block: block,
274
275
  location: nil
@@ -414,22 +415,26 @@ module RBS
414
415
  class RubyAttr < RubyBase
415
416
  attr_reader :node #: Prism::CallNode
416
417
  attr_reader :comments #: AnnotationParser::ParsingResult?
418
+ attr_reader :visibility #: RBS::AST::Members::visibility?
417
419
  attr_reader :assertion #: Annotations::TypeAssertion?
418
420
 
419
421
  # @rbs node: Prism::CallNode
420
422
  # @rbs comments: AnnotationParser::ParsingResult?
423
+ # @rbs visibility: RBS::AST::Members::visibility?
421
424
  # @rbs assertion: Annotations::TypeAssertion?
422
425
  # @rbs return: void
423
- def initialize(node, comments, assertion)
426
+ def initialize(node, comments, visibility, assertion)
424
427
  super(node.location)
425
428
 
426
429
  @node = node
427
430
  @comments = comments
431
+ @visibility = visibility
428
432
  @assertion = assertion
429
433
  end
430
434
 
431
435
  # @rbs return Array[RBS::AST::Members::AttrReader | RBS::AST::Members::AttrWriter | RBS::AST::Members::AttrAccessor]?
432
- def rbs
436
+ # @rbs default_type: RBS::Types::t
437
+ def rbs(default_type)
433
438
  if comments
434
439
  comment = RBS::AST::Comment.new(string: comments.content(trim: true), location: nil)
435
440
  end
@@ -460,13 +465,13 @@ module RBS
460
465
  args.map do |arg|
461
466
  klass.new(
462
467
  name: arg,
463
- type: attribute_type,
468
+ type: attribute_type || default_type,
464
469
  ivar_name: nil,
465
470
  kind: :instance,
466
471
  annotations: [],
467
472
  location: nil,
468
473
  comment: comment,
469
- visibility: nil
474
+ visibility: visibility
470
475
  )
471
476
  end
472
477
  end
@@ -474,13 +479,11 @@ module RBS
474
479
 
475
480
  # Returns the type of the attribute
476
481
  #
477
- # Returns `untyped` when not annotated.
478
- #
479
- def attribute_type #: Types::t
482
+ def attribute_type #: Types::t?
480
483
  type = assertion&.type
481
484
  raise if type.is_a?(MethodType)
482
485
 
483
- type || Types::Bases::Any.new(location: nil)
486
+ type
484
487
  end
485
488
  end
486
489
 
@@ -73,6 +73,7 @@ module RBS
73
73
  base_paths = [Pathname("lib"), Pathname("app")]
74
74
  output_path = nil #: Pathname?
75
75
  opt_in = true
76
+ unknown_type = nil #: String?
76
77
 
77
78
  OptionParser.new do |opts|
78
79
  opts.on("--base=BASE", "The path to calculate relative path of files (defaults to #{base_paths.join(File::PATH_SEPARATOR)})") do |str|
@@ -96,6 +97,10 @@ module RBS
96
97
  opt_in = true
97
98
  end
98
99
 
100
+ opts.on("--unknown-type=TYPE", "Use given type instead of untyped for unknown types (default: untyped)") do
101
+ unknown_type = _1
102
+ end
103
+
99
104
  opts.on("--verbose") do
100
105
  logger.level = :DEBUG
101
106
  end
@@ -141,6 +146,12 @@ module RBS
141
146
 
142
147
  if (uses, decls, rbs_decls = Parser.parse(Prism.parse_file(target.to_s), opt_in: opt_in))
143
148
  writer = Writer.new()
149
+
150
+ if unknown_type
151
+ type = RBS::Parser.parse_type(unknown_type, require_eof: true)
152
+ type && writer.default_type = type
153
+ end
154
+
144
155
  writer.header("Generated from #{target.relative? ? target : target.relative_path_from(Pathname.pwd)} with RBS::Inline")
145
156
  writer.write(uses, decls, rbs_decls)
146
157
 
@@ -304,7 +304,7 @@ module RBS
304
304
  end #: AST::Annotations::TypeAssertion?
305
305
  end
306
306
 
307
- current_class_module_decl!.members << AST::Members::RubyAttr.new(node, comment, assertion)
307
+ current_class_module_decl!.members << AST::Members::RubyAttr.new(node, comment, current_visibility, assertion)
308
308
 
309
309
  return
310
310
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RBS
4
4
  module Inline
5
- VERSION = "0.10.0"
5
+ VERSION = "0.11.0"
6
6
  end
7
7
  end
@@ -15,17 +15,22 @@ module RBS
15
15
  attr_reader :output #: String
16
16
  attr_reader :writer #: RBS::Writer
17
17
 
18
+ attr_accessor :default_type #: Types::t
19
+
18
20
  # @rbs buffer: String
19
21
  def initialize(buffer = +"") #: void
20
22
  @output = buffer
21
23
  @writer = RBS::Writer.new(out: StringIO.new(buffer))
24
+ @default_type = Types::Bases::Any.new(location: nil)
22
25
  end
23
26
 
24
27
  # @rbs uses: Array[AST::Annotations::Use]
25
28
  # @rbs decls: Array[AST::Declarations::t]
26
29
  # @rbs rbs_decls: Array[RBS::AST::Declarations::t]
27
- def self.write(uses, decls, rbs_decls) #: void
30
+ # @rbs &: ? (Writer) -> void
31
+ def self.write(uses, decls, rbs_decls, &) #: void
28
32
  writer = Writer.new()
33
+ yield writer if block_given?
29
34
  writer.write(uses, decls, rbs_decls)
30
35
  writer.output
31
36
  end
@@ -200,7 +205,7 @@ module RBS
200
205
  attributes = decl.each_attribute.map do |name, type|
201
206
  RBS::AST::Members::AttrReader.new(
202
207
  name: name,
203
- type: type&.type || Types::Bases::Any.new(location: nil),
208
+ type: type&.type || default_type,
204
209
  ivar_name: false,
205
210
  comment: nil,
206
211
  kind: :instance,
@@ -220,7 +225,7 @@ module RBS
220
225
  type: Types::Function.empty(Types::Bases::Instance.new(location: nil)).update(
221
226
  required_positionals: decl.each_attribute.map do |name, attr|
222
227
  RBS::Types::Function::Param.new(
223
- type: attr&.type || Types::Bases::Any.new(location: nil),
228
+ type: attr&.type || default_type,
224
229
  name: name,
225
230
  location: nil
226
231
  )
@@ -239,7 +244,7 @@ module RBS
239
244
  [
240
245
  name,
241
246
  RBS::Types::Function::Param.new(
242
- type: attr&.type || Types::Bases::Any.new(location: nil),
247
+ type: attr&.type || default_type,
243
248
  name: nil,
244
249
  location: nil
245
250
  )
@@ -317,7 +322,7 @@ module RBS
317
322
  if decl.readonly_attributes?
318
323
  RBS::AST::Members::AttrReader.new(
319
324
  name: name,
320
- type: type&.type || Types::Bases::Any.new(location: nil),
325
+ type: type&.type || default_type,
321
326
  ivar_name: false,
322
327
  comment: nil,
323
328
  kind: :instance,
@@ -328,7 +333,7 @@ module RBS
328
333
  else
329
334
  RBS::AST::Members::AttrAccessor.new(
330
335
  name: name,
331
- type: type&.type || Types::Bases::Any.new(location: nil),
336
+ type: type&.type || default_type,
332
337
  ivar_name: false,
333
338
  comment: nil,
334
339
  kind: :instance,
@@ -353,7 +358,7 @@ module RBS
353
358
  if decl.positional_init?
354
359
  attr_params = decl.each_attribute.map do |name, attr|
355
360
  RBS::Types::Function::Param.new(
356
- type: attr&.type || Types::Bases::Any.new(location: nil),
361
+ type: attr&.type || default_type,
357
362
  name: name,
358
363
  location: nil
359
364
  )
@@ -378,7 +383,7 @@ module RBS
378
383
  [
379
384
  name,
380
385
  RBS::Types::Function::Param.new(
381
- type: attr&.type || Types::Bases::Any.new(location: nil),
386
+ type: attr&.type || default_type,
382
387
  name: nil,
383
388
  location: nil
384
389
  )
@@ -407,7 +412,7 @@ module RBS
407
412
  t.update(required_positionals: [
408
413
  RBS::Types::Function::Param.new(
409
414
  type: RBS::Types::Record.new(all_fields: decl.each_attribute.map do |name, attr|
410
- [name, attr&.type || Types::Bases::Any.new(location: nil)]
415
+ [name, attr&.type || default_type]
411
416
  end.to_h, location: nil),
412
417
  name: nil,
413
418
  location: nil
@@ -428,7 +433,12 @@ module RBS
428
433
  members: [*attributes, new],
429
434
  super_class: RBS::AST::Declarations::Class::Super.new(
430
435
  name: RBS::TypeName.new(name: :Struct, namespace: RBS::Namespace.empty),
431
- args: [RBS::Types::Bases::Any.new(location: nil)],
436
+ args: [
437
+ RBS::Types::Union.new(
438
+ types: decl.each_attribute.map { |_, attr| attr&.type || default_type }.uniq,
439
+ location: nil
440
+ )
441
+ ],
432
442
  location: nil
433
443
  ),
434
444
  annotations: decl.class_annotations,
@@ -480,7 +490,7 @@ module RBS
480
490
  rbs << RBS::AST::Members::MethodDefinition.new(
481
491
  name: member.method_name,
482
492
  kind: kind,
483
- overloads: member.method_overloads,
493
+ overloads: member.method_overloads(default_type),
484
494
  annotations: member.method_annotations,
485
495
  location: nil,
486
496
  comment: comment,
@@ -492,10 +502,11 @@ module RBS
492
502
  comment = RBS::AST::Comment.new(string: member.comments.content(trim: true), location: nil)
493
503
  end
494
504
 
505
+ kind = decl ? :singleton : :instance #: RBS::AST::Members::Alias::kind
495
506
  rbs << RBS::AST::Members::Alias.new(
496
507
  new_name: member.new_name,
497
508
  old_name: member.old_name,
498
- kind: :instance,
509
+ kind: kind,
499
510
  annotations: [],
500
511
  location: nil,
501
512
  comment: comment
@@ -505,7 +516,7 @@ module RBS
505
516
  rbs << m
506
517
  end
507
518
  when AST::Members::RubyAttr
508
- if m = member.rbs
519
+ if m = member.rbs(default_type)
509
520
  rbs.concat m
510
521
  end
511
522
  when AST::Members::RubyPrivate
@@ -615,24 +626,19 @@ module RBS
615
626
  # @rbs decl: AST::Declarations::ConstantDecl
616
627
  # @rbs return: RBS::Types::t
617
628
  def constant_decl_to_type(decl)
618
- type = decl.type
629
+ type = decl.type(default_type)
619
630
  return type unless type.is_a?(RBS::Types::ClassInstance)
620
631
  return type if type.args.any?
621
632
 
622
633
  case decl.node.value
623
634
  when Prism::ArrayNode
624
- RBS::BuiltinNames::Array.instance_type(untyped)
635
+ RBS::BuiltinNames::Array.instance_type(default_type)
625
636
  when Prism::HashNode
626
- RBS::BuiltinNames::Hash.instance_type(untyped, untyped)
637
+ RBS::BuiltinNames::Hash.instance_type(default_type, default_type)
627
638
  else
628
639
  type
629
640
  end
630
641
  end
631
-
632
- # @rbs return: RBS::Types::Bases::Any
633
- def untyped
634
- @untyped ||= RBS::Types::Bases::Any.new(location: nil)
635
- end
636
642
  end
637
643
  end
638
644
  end
@@ -99,7 +99,7 @@ module RBS
99
99
  # @rbs %a{pure}
100
100
  # @rbs return: Types::t
101
101
  %a{pure}
102
- def type: () -> Types::t
102
+ def type: (untyped default_type) -> Types::t
103
103
 
104
104
  # @rbs %a{pure}
105
105
  # @rbs return Types::t?
@@ -68,7 +68,8 @@ module RBS
68
68
 
69
69
  def overloading?: () -> bool
70
70
 
71
- def method_overloads: () -> Array[RBS::AST::Members::MethodDefinition::Overload]
71
+ # @rbs (::RBS::Types::t default_type) -> Array[RBS::AST::Members::MethodDefinition::Overload]
72
+ def method_overloads: (::RBS::Types::t default_type) -> Array[RBS::AST::Members::MethodDefinition::Overload]
72
73
 
73
74
  def method_annotations: () -> Array[RBS::AST::Annotation]
74
75
 
@@ -124,21 +125,23 @@ module RBS
124
125
 
125
126
  attr_reader comments: AnnotationParser::ParsingResult?
126
127
 
128
+ attr_reader visibility: RBS::AST::Members::visibility?
129
+
127
130
  attr_reader assertion: Annotations::TypeAssertion?
128
131
 
129
132
  # @rbs node: Prism::CallNode
130
133
  # @rbs comments: AnnotationParser::ParsingResult?
134
+ # @rbs visibility: RBS::AST::Members::visibility?
131
135
  # @rbs assertion: Annotations::TypeAssertion?
132
136
  # @rbs return: void
133
- def initialize: (Prism::CallNode node, AnnotationParser::ParsingResult? comments, Annotations::TypeAssertion? assertion) -> void
137
+ def initialize: (Prism::CallNode node, AnnotationParser::ParsingResult? comments, RBS::AST::Members::visibility? visibility, Annotations::TypeAssertion? assertion) -> void
134
138
 
135
139
  # @rbs return Array[RBS::AST::Members::AttrReader | RBS::AST::Members::AttrWriter | RBS::AST::Members::AttrAccessor]?
136
- def rbs: () -> Array[RBS::AST::Members::AttrReader | RBS::AST::Members::AttrWriter | RBS::AST::Members::AttrAccessor]?
140
+ # @rbs default_type: RBS::Types::t
141
+ def rbs: (RBS::Types::t default_type) -> Array[RBS::AST::Members::AttrReader | RBS::AST::Members::AttrWriter | RBS::AST::Members::AttrAccessor]?
137
142
 
138
143
  # Returns the type of the attribute
139
- #
140
- # Returns `untyped` when not annotated.
141
- def attribute_type: () -> Types::t
144
+ def attribute_type: () -> Types::t?
142
145
  end
143
146
 
144
147
  # `private` call without arguments
@@ -13,13 +13,16 @@ module RBS
13
13
 
14
14
  attr_reader writer: RBS::Writer
15
15
 
16
+ attr_accessor default_type: Types::t
17
+
16
18
  # @rbs buffer: String
17
19
  def initialize: (?String buffer) -> void
18
20
 
19
21
  # @rbs uses: Array[AST::Annotations::Use]
20
22
  # @rbs decls: Array[AST::Declarations::t]
21
23
  # @rbs rbs_decls: Array[RBS::AST::Declarations::t]
22
- def self.write: (Array[AST::Annotations::Use] uses, Array[AST::Declarations::t] decls, Array[RBS::AST::Declarations::t] rbs_decls) -> void
24
+ # @rbs &: ? (Writer) -> void
25
+ def self.write: (Array[AST::Annotations::Use] uses, Array[AST::Declarations::t] decls, Array[RBS::AST::Declarations::t] rbs_decls) ?{ (Writer) -> void } -> void
23
26
 
24
27
  # @rbs *lines: String
25
28
  # @rbs return: void
@@ -111,9 +114,6 @@ module RBS
111
114
  # @rbs decl: AST::Declarations::ConstantDecl
112
115
  # @rbs return: RBS::Types::t
113
116
  def constant_decl_to_type: (AST::Declarations::ConstantDecl decl) -> RBS::Types::t
114
-
115
- # @rbs return: RBS::Types::Bases::Any
116
- def untyped: () -> RBS::Types::Bases::Any
117
117
  end
118
118
  end
119
119
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbs-inline
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Soutaro Matsumoto
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2024-11-18 00:00:00.000000000 Z
10
+ date: 2025-03-06 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: prism
@@ -95,7 +94,6 @@ metadata:
95
94
  homepage_uri: https://github.com/soutaro/rbs-inline
96
95
  source_code_uri: https://github.com/soutaro/rbs-inline
97
96
  changelog_uri: https://github.com/soutaro/rbs-inline/blob/master/CHANGELOG.md
98
- post_install_message:
99
97
  rdoc_options: []
100
98
  require_paths:
101
99
  - lib
@@ -110,8 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
108
  - !ruby/object:Gem::Version
111
109
  version: '0'
112
110
  requirements: []
113
- rubygems_version: 3.5.11
114
- signing_key:
111
+ rubygems_version: 3.6.2
115
112
  specification_version: 4
116
113
  summary: Inline RBS type declaration.
117
114
  test_files: []