Skip to content

Commit e406183

Browse files
committed
Add new Attribute#ivar API
1 parent 5576405 commit e406183

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

lib/rbs/ast/members.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,21 @@ def update(name: self.name, type: self.type, ivar_name: self.ivar_name, kind: se
304304
visibility: visibility
305305
)
306306
end
307+
308+
def ivar
309+
@ivar ||= begin
310+
ivar_name = case self.ivar_name
311+
when false
312+
return nil # Skip the instance variable declaration entirely
313+
when nil
314+
:"@#{name}" # Infer the instance variable name from the attribute name
315+
else
316+
self.ivar_name # Use the custom instance variable name given by the user
317+
end
318+
319+
InstanceVariable.new(name: ivar_name, type: type, location: location, comment: comment)
320+
end
321+
end
307322
end
308323

309324
class AttrReader < Base

sig/members.rbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ module RBS
185185
include _HashEqual
186186

187187
def update: (?name: Symbol, ?type: Types::t, ?ivar_name: Symbol | false | nil, ?kind: kind, ?annotations: Array[Annotation], ?location: loc?, ?comment: Comment?, ?visibility: visibility?) -> instance
188+
189+
def ivar: () -> InstanceVariable?
188190
end
189191

190192
class AttrReader < Base

test/rbs/signature_parsing_test.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,13 @@ def self?.three: -> bool
361361
assert_equal :a, m.name
362362
assert_nil m.ivar_name
363363
assert_equal parse_type("Integer"), m.type
364+
365+
ivar = m.ivar
366+
assert_instance_of Members::InstanceVariable, ivar
367+
assert_equal :@a, ivar.name # Inferred from the attribute name
368+
assert_equal m.type, ivar.type
369+
assert_equal m.location, ivar.location
370+
assert_equal m.comment, ivar.comment
364371
end
365372

366373
module_decl.members[11].yield_self do |m|
@@ -369,6 +376,13 @@ def self?.three: -> bool
369376
assert_equal :a, m.name
370377
assert_equal :@A, m.ivar_name
371378
assert_equal parse_type("String"), m.type
379+
380+
ivar = m.ivar
381+
assert_instance_of Members::InstanceVariable, ivar
382+
assert_equal :@A, ivar.name # Explicitly given ivar name
383+
assert_equal m.type, ivar.type
384+
assert_equal m.location, ivar.location
385+
assert_equal m.comment, ivar.comment
372386
end
373387

374388
module_decl.members[12].yield_self do |m|
@@ -377,6 +391,8 @@ def self?.three: -> bool
377391
assert_equal :a, m.name
378392
assert_equal false, m.ivar_name
379393
assert_equal parse_type("bool"), m.type
394+
395+
assert_nil m.ivar # The instance variable was explicitly skipped
380396
end
381397

382398
module_decl.members[13].yield_self do |m|
@@ -385,6 +401,13 @@ def self?.three: -> bool
385401
assert_equal :b, m.name
386402
assert_nil m.ivar_name
387403
assert_equal parse_type("Integer"), m.type
404+
405+
ivar = m.ivar
406+
assert_instance_of Members::InstanceVariable, ivar
407+
assert_equal :@b, ivar.name # Inferred from the attribute name
408+
assert_equal m.type, ivar.type
409+
assert_equal m.location, ivar.location
410+
assert_equal m.comment, ivar.comment
388411
end
389412

390413
module_decl.members[14].yield_self do |m|
@@ -393,6 +416,13 @@ def self?.three: -> bool
393416
assert_equal :b, m.name
394417
assert_equal :@B, m.ivar_name
395418
assert_equal parse_type("String"), m.type
419+
420+
ivar = m.ivar
421+
assert_instance_of Members::InstanceVariable, ivar
422+
assert_equal :@B, ivar.name # Explicitly given ivar name
423+
assert_equal m.type, ivar.type
424+
assert_equal m.location, ivar.location
425+
assert_equal m.comment, ivar.comment
396426
end
397427

398428
module_decl.members[15].yield_self do |m|
@@ -401,6 +431,8 @@ def self?.three: -> bool
401431
assert_equal :b, m.name
402432
assert_equal false, m.ivar_name
403433
assert_equal parse_type("bool"), m.type
434+
435+
assert_nil m.ivar # The instance variable was explicitly skipped
404436
end
405437

406438
module_decl.members[16].yield_self do |m|
@@ -409,6 +441,13 @@ def self?.three: -> bool
409441
assert_equal :c, m.name
410442
assert_nil m.ivar_name
411443
assert_equal parse_type("Integer"), m.type
444+
445+
ivar = m.ivar
446+
assert_instance_of Members::InstanceVariable, ivar
447+
assert_equal :@c, ivar.name # Inferred from the attribute name
448+
assert_equal m.type, ivar.type
449+
assert_equal m.location, ivar.location
450+
assert_equal m.comment, ivar.comment
412451
end
413452

414453
module_decl.members[17].yield_self do |m|
@@ -417,6 +456,13 @@ def self?.three: -> bool
417456
assert_equal :c, m.name
418457
assert_equal :@C, m.ivar_name
419458
assert_equal parse_type("String"), m.type
459+
460+
ivar = m.ivar
461+
assert_instance_of Members::InstanceVariable, ivar
462+
assert_equal :@C, ivar.name # Explicitly given ivar name
463+
assert_equal m.type, ivar.type
464+
assert_equal m.location, ivar.location
465+
assert_equal m.comment, ivar.comment
420466
end
421467

422468
module_decl.members[18].yield_self do |m|
@@ -425,6 +471,8 @@ def self?.three: -> bool
425471
assert_equal :c, m.name
426472
assert_equal false, m.ivar_name
427473
assert_equal parse_type("bool"), m.type
474+
475+
assert_nil m.ivar # The instance variable was explicitly skipped
428476
end
429477
end
430478
end

0 commit comments

Comments
 (0)