Skip to content

Commit 2146722

Browse files
committed
Fully qualify T.type_alias to ::T.type_alias
When erasing generic types we replace `type_member`s with a `T.anything` type alias of the same name. Qualifying `T.type_alias` to `::T.type_alias` ensures we avoid conflicts like `T = T.type_alias`. This matches
1 parent da7d320 commit 2146722

3 files changed

Lines changed: 21 additions & 21 deletions

File tree

lib/spoom/sorbet/translate/rbs_comments_to_sorbet_sigs/base_translator.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def apply_class_annotations(node)
270270
type_params.each do |type_param|
271271
@rewriter << Source::Insert.new(
272272
insert_pos,
273-
"\n#{indent}#{type_param.name} = T.type_alias { ::T.anything }\n",
273+
"\n#{indent}#{type_param.name} = ::T.type_alias { ::T.anything }\n",
274274
)
275275
end
276276

@@ -417,7 +417,7 @@ def apply_type_aliases(comments)
417417
)
418418

419419
@rewriter << Source::Delete.new(from, to)
420-
content = "#{indent}#{alias_name} = T.type_alias { #{sorbet_type.to_rbi} }\n"
420+
content = "#{indent}#{alias_name} = ::T.type_alias { #{sorbet_type.to_rbi} }\n"
421421
@rewriter << Source::Insert.new(insert_pos, content)
422422
rescue ::RBS::ParsingError, ::RBI::Error
423423
# Ignore type aliases with errors

test/spoom/cli/srb/sigs_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def push(value)
223223
# typed: true
224224
225225
class Box
226-
E = T.type_alias { ::T.anything }
226+
E = ::T.type_alias { ::T.anything }
227227
228228
sig { returns(Array) }
229229
def values

test/spoom/sorbet/translate/rbs_comments_to_sorbet_sigs_test.rb

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -490,17 +490,17 @@ def parse_int(str)
490490

491491
assert_equal(<<~RB, rbs_comments_to_sorbet_sigs(contents, erase_generic_types: true))
492492
class Result
493-
T = T.type_alias { ::T.anything }
493+
T = ::T.type_alias { ::T.anything }
494494
495-
A = T.type_alias { ::T.anything }
495+
A = ::T.type_alias { ::T.anything }
496496
497-
B = T.type_alias { ::T.anything }
497+
B = ::T.type_alias { ::T.anything }
498498
499-
C = T.type_alias { ::T.anything }
499+
C = ::T.type_alias { ::T.anything }
500500
501-
D = T.type_alias { ::T.anything }
501+
D = ::T.type_alias { ::T.anything }
502502
503-
E = T.type_alias { ::T.anything }
503+
E = ::T.type_alias { ::T.anything }
504504
505505
sig { params(value: T, error: ::T.nilable(E)).void }
506506
def initialize(value, error)
@@ -560,7 +560,7 @@ def wrap(value)
560560
end
561561
562562
class << self
563-
V = T.type_alias { ::T.anything }
563+
V = ::T.type_alias { ::T.anything }
564564
565565
sig { returns(V) }
566566
def build; end
@@ -687,8 +687,8 @@ def bar(a)
687687

688688
to_pretty_format_for_humans: <<~RUBY,
689689
module Aliases
690-
Foo = T.type_alias { ::T.any(Integer, String) }
691-
MultiLine = T.type_alias { ::T.any(Integer, String) }
690+
Foo = ::T.type_alias { ::T.any(Integer, String) }
691+
MultiLine = ::T.type_alias { ::T.any(Integer, String) }
692692
end
693693
694694
sig { params(a: Aliases::Foo).returns(Aliases::MultiLine) }
@@ -712,8 +712,8 @@ def process_user(data)
712712
RUBY
713713

714714
to_pretty_format_for_humans: <<~RUBY,
715-
Foo::UserId = T.type_alias { Integer }
716-
::Bar::UserData = T.type_alias { { id: Foo::UserId, name: String } }
715+
Foo::UserId = ::T.type_alias { Integer }
716+
::Bar::UserData = ::T.type_alias { { id: Foo::UserId, name: String } }
717717
718718
sig { params(data: ::Bar::UserData).returns(Foo::UserId) }
719719
def process_user(data)
@@ -738,7 +738,7 @@ def get_status
738738

739739
to_pretty_format_for_humans: <<~RUBY,
740740
class Example
741-
Status = T.type_alias { Symbol }
741+
Status = ::T.type_alias { Symbol }
742742
743743
sig { returns(Status) }
744744
def get_status
@@ -761,7 +761,7 @@ def status; end
761761

762762
to_pretty_format_for_humans: <<~RUBY,
763763
class Example
764-
Status = T.type_alias { Symbol }
764+
Status = ::T.type_alias { Symbol }
765765
sig { returns(Status) }
766766
def status; end
767767
end
@@ -783,7 +783,7 @@ def double_items(items)
783783
RUBY
784784

785785
to_pretty_format_for_humans: <<~RUBY,
786-
List = T.type_alias { ::T::Array[Integer] }
786+
List = ::T.type_alias { ::T::Array[Integer] }
787787
788788
sig { params(items: List).returns(List) }
789789
def double_items(items)
@@ -808,7 +808,7 @@ def build_box
808808
assert_equal(<<~RB, rbs_comments_to_sorbet_sigs(contents, erase_generic_types: true))
809809
class Box; end
810810
811-
BoxedString = T.type_alias { Box }
811+
BoxedString = ::T.type_alias { Box }
812812
813813
sig { returns(BoxedString) }
814814
def build_box
@@ -829,7 +829,7 @@ def ensure_string(text)
829829
RUBY
830830

831831
to_pretty_format_for_humans: <<~RUBY,
832-
NullableString = T.type_alias { ::T.nilable(String) }
832+
NullableString = ::T.type_alias { ::T.nilable(String) }
833833
834834
sig { params(text: NullableString).returns(String) }
835835
def ensure_string(text)
@@ -871,7 +871,7 @@ def foo
871871
RUBY
872872

873873
to_pretty_format_for_humans: <<~RUBY,
874-
MultiLine = T.type_alias { ::T.any(String, Integer) }
874+
MultiLine = ::T.type_alias { ::T.any(String, Integer) }
875875
# foo bar baz
876876
#| | Symbol
877877
@@ -908,7 +908,7 @@ class Range
908908

909909
to_pretty_format_for_humans: <<~RUBY,
910910
module Foo
911-
SerializedRange = T.type_alias { [Integer, Integer] }
911+
SerializedRange = ::T.type_alias { [Integer, Integer] }
912912
class Range
913913
end
914914
end

0 commit comments

Comments
 (0)