Skip to content

Commit ac5bbf5

Browse files
authored
Merge branch 'main' into deadcode-fix-heredoc-over-removal
2 parents cadd79e + 0f2afe8 commit ac5bbf5

5 files changed

Lines changed: 95 additions & 65 deletions

File tree

Gemfile.lock

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ GEM
2929
pp (>= 0.6.0)
3030
rdoc (>= 4.0.0)
3131
reline (>= 0.4.2)
32-
json (2.21.1)
32+
json (2.21.2)
3333
language_server-protocol (3.17.0.6)
3434
lint_roller (1.1.0)
3535
logger (1.7.0)
@@ -57,10 +57,10 @@ GEM
5757
racc (1.8.1)
5858
rainbow (3.1.1)
5959
rake (13.4.2)
60-
rbi (0.4.2)
60+
rbi (0.4.3)
6161
prism (~> 1.0)
6262
rbs (>= 4.0.1)
63-
rbs (4.1.1)
63+
rbs (4.1.3)
6464
logger
6565
prism (>= 1.6.0)
6666
tsort
@@ -73,7 +73,7 @@ GEM
7373
io-console (~> 0.5)
7474
require-hooks (0.4.0)
7575
rexml (3.4.4)
76-
rubocop (1.88.2)
76+
rubocop (1.89.0)
7777
json (~> 2.3)
7878
language_server-protocol (~> 3.17.0.2)
7979
lint_roller (~> 1.1.0)
@@ -91,24 +91,24 @@ GEM
9191
lint_roller (~> 1.1)
9292
rubocop (>= 1.75.0, < 2.0)
9393
rubocop-ast (>= 1.38.0, < 2.0)
94-
rubocop-shopify (3.0.1)
94+
rubocop-shopify (3.0.2)
9595
lint_roller
9696
rubocop (~> 1.72, >= 1.72.1)
97-
rubocop-sorbet (0.13.2)
97+
rubocop-sorbet (0.14.0)
9898
lint_roller
9999
rubocop (>= 1.75.2)
100100
ruby-progressbar (1.13.0)
101101
rubydex (0.2.6-arm64-darwin)
102102
rubydex (0.2.6-x86_64-darwin)
103103
rubydex (0.2.6-x86_64-linux)
104-
sorbet (0.6.13386)
105-
sorbet-static (= 0.6.13386)
106-
sorbet-runtime (0.6.13386)
107-
sorbet-static (0.6.13386-universal-darwin)
108-
sorbet-static (0.6.13386-x86_64-linux)
109-
sorbet-static-and-runtime (0.6.13386)
110-
sorbet (= 0.6.13386)
111-
sorbet-runtime (= 0.6.13386)
104+
sorbet (0.6.13414)
105+
sorbet-static (= 0.6.13414)
106+
sorbet-runtime (0.6.13414)
107+
sorbet-static (0.6.13414-universal-darwin)
108+
sorbet-static (0.6.13414-x86_64-linux)
109+
sorbet-static-and-runtime (0.6.13414)
110+
sorbet (= 0.6.13414)
111+
sorbet-runtime (= 0.6.13414)
112112
stringio (3.2.0)
113113
tapioca (0.19.2)
114114
benchmark

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

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,10 @@ def apply_class_annotations(node)
231231
node.expression.location.end_offset
232232
end
233233

234-
# Only translate (and `extend T::Helpers`) when there's at least one *known* class
234+
# Only translate (and `extend ::T::Helpers`) when there's at least one *known* class
235235
# annotation. A node with only unknown annotations (e.g. `@private`) is left untouched.
236236
if comments.class_annotations.any?
237-
unless already_extends?(node, /^(::)?T::Helpers$/)
238-
extend_with("T::Helpers", into: node, at: insert_pos)
239-
end
237+
extend_with("::T::Helpers", into: node, at: insert_pos)
240238

241239
comments.annotations.reverse_each do |annotation|
242240
content = case annotation.string
@@ -286,9 +284,7 @@ def apply_class_annotations(node)
286284
next
287285
end
288286

289-
unless already_extends?(node, /^(::)?T::Generic$/)
290-
extend_with("T::Generic", into: node, at: insert_pos)
291-
end
287+
extend_with("::T::Generic", into: node, at: insert_pos)
292288

293289
type_params.each do |type_param|
294290
type_member = "#{type_param.name} = type_member"
@@ -395,22 +391,6 @@ def rewrite_annotation(annotation, is_known:) = nil # no-op
395391
#: (String mixin_name, into: PrismTypes::anyScopeNode, at: Integer) -> void
396392
def extend_with(mixin_name, into:, at:) = raise
397393

398-
#: (PrismTypes::anyScopeNode, Regexp) -> bool
399-
def already_extends?(node, constant_regex)
400-
node.child_nodes.any? do |c|
401-
next false unless c.is_a?(Prism::CallNode)
402-
next false unless c.message == "extend"
403-
next false unless c.receiver.nil? || c.receiver.is_a?(Prism::SelfNode)
404-
next false unless c.arguments&.arguments&.size == 1
405-
406-
arg = c.arguments&.arguments&.first
407-
next false unless arg.is_a?(Prism::ConstantPathNode)
408-
next false unless arg.slice.match?(constant_regex)
409-
410-
true
411-
end
412-
end
413-
414394
#: (Array[Prism::Comment]) -> Array[Spoom::RBS::TypeAlias]
415395
def collect_type_aliases(comments)
416396
type_aliases = [] #: Array[Spoom::RBS::TypeAlias]

rbi/spoom.rbi

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3375,14 +3375,6 @@ class Spoom::Sorbet::Translate::RBSCommentsToSorbetSigs::BaseTranslator < ::Spoo
33753375

33763376
private
33773377

3378-
sig do
3379-
params(
3380-
node: T.any(::Prism::ClassNode, ::Prism::ModuleNode, ::Prism::SingletonClassNode),
3381-
constant_regex: ::Regexp
3382-
).returns(T::Boolean)
3383-
end
3384-
def already_extends?(node, constant_regex); end
3385-
33863378
sig do
33873379
abstract
33883380
.params(

test/spoom/cli/srb/lsp_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class HoverTest; end
8383
result = @project.spoom("srb lsp --no-color hover lib/hover.rb 2 12")
8484
assert_equal(<<~MSG, result.out)
8585
Hovering `lib/hover.rb:2:12`:
86+
# class HoverTest
8687
T.class_of(HoverTest)
8788
MSG
8889
end

test/spoom/sorbet/translate/rbs_comments_to_sorbet_sigs_test.rb

Lines changed: 77 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def qux; end
167167

168168
to_pretty_format_for_humans: <<~RUBY,
169169
class Foo
170-
extend T::Helpers
170+
extend ::T::Helpers
171171
172172
abstract!
173173
@@ -177,7 +177,7 @@ def bar; end
177177
end
178178
179179
module Baz
180-
extend T::Helpers
180+
extend ::T::Helpers
181181
182182
abstract!
183183
@@ -189,14 +189,14 @@ def qux; end
189189

190190
to_line_matched_format_for_machines: <<~RUBY,
191191
# RBS_REWRITTEN_ANNOTATION: @abstract
192-
class Foo; extend T::Helpers; abstract!
192+
class Foo; extend ::T::Helpers; abstract!
193193
# @abstract
194194
#: -> String
195195
def bar; end
196196
end
197197
198198
# RBS_REWRITTEN_ANNOTATION: @abstract
199-
module Baz; extend T::Helpers; abstract!
199+
module Baz; extend ::T::Helpers; abstract!
200200
# @abstract
201201
#: -> String
202202
def qux; end
@@ -481,21 +481,21 @@ class << self
481481

482482
to_pretty_format_for_humans: <<~RUBY,
483483
class A
484-
extend T::Helpers
484+
extend ::T::Helpers
485485
486486
abstract!
487487
488488
requires_ancestor { ::T.class_of(Foo::Bar) }
489489
490490
module B
491-
extend T::Helpers
491+
extend ::T::Helpers
492492
493493
interface!
494494
495495
sealed!
496496
497497
class << self
498-
extend T::Helpers
498+
extend ::T::Helpers
499499
500500
final!
501501
end
@@ -506,12 +506,12 @@ class << self
506506
to_line_matched_format_for_machines: <<~RUBY,
507507
# RBS_REWRITTEN_ANNOTATION: @abstract
508508
# RBS_REWRITTEN_ANNOTATION: @requires_ancestor: singleton(Foo::Bar)
509-
class A; extend T::Helpers; abstract!; requires_ancestor { ::T.class_of(Foo::Bar) }
509+
class A; extend ::T::Helpers; abstract!; requires_ancestor { ::T.class_of(Foo::Bar) }
510510
# RBS_REWRITTEN_ANNOTATION: @interface
511511
# RBS_REWRITTEN_ANNOTATION: @sealed
512-
module B; extend T::Helpers; interface!; sealed!
512+
module B; extend ::T::Helpers; interface!; sealed!
513513
# RBS_REWRITTEN_ANNOTATION: @final
514-
class << self; extend T::Helpers; final!
514+
class << self; extend ::T::Helpers; final!
515515
end
516516
end
517517
end
@@ -549,7 +549,7 @@ def foo; end
549549
# @foo
550550
# @bar
551551
module Baz
552-
extend T::Helpers
552+
extend ::T::Helpers
553553
554554
requires_ancestor { Kernel }
555555
@@ -562,7 +562,7 @@ def foo; end
562562
# RBS_IGNORED_UNKNOWN_ANNOTATION: @foo
563563
# RBS_IGNORED_UNKNOWN_ANNOTATION: @bar
564564
# RBS_REWRITTEN_ANNOTATION: @requires_ancestor: Kernel
565-
module Baz; extend T::Helpers; requires_ancestor { Kernel }
565+
module Baz; extend ::T::Helpers; requires_ancestor { Kernel }
566566
sig { void }
567567
def foo; end
568568
end
@@ -586,21 +586,21 @@ class << self
586586

587587
to_pretty_format_for_humans: <<~RUBY,
588588
class A
589-
extend T::Generic
589+
extend ::T::Generic
590590
591591
A = type_member(:in)
592592
593593
B = type_member(:out)
594594
595595
module B
596-
extend T::Generic
596+
extend ::T::Generic
597597
598598
A = type_member
599599
600600
B = type_member {{ upper: C }}
601601
602602
class << self
603-
extend T::Generic
603+
extend ::T::Generic
604604
605605
A = type_member {{ fixed: ::T.class_of(Numeric) }}
606606
end
@@ -610,18 +610,75 @@ class << self
610610

611611
to_line_matched_format_for_machines: <<~RUBY,
612612
# RBS_WRITTEN_ANNOTATION: [in A, out B]
613-
class A; extend T::Generic; A = type_member(:in); B = type_member(:out)
613+
class A; extend ::T::Generic; A = type_member(:in); B = type_member(:out)
614614
# RBS_WRITTEN_ANNOTATION: [A, B < C]
615-
module B; extend T::Generic; A = type_member; B = type_member {{ upper: C }}
615+
module B; extend ::T::Generic; A = type_member; B = type_member {{ upper: C }}
616616
# RBS_WRITTEN_ANNOTATION: [A = singleton(Numeric)]
617-
class << self; extend T::Generic; A = type_member {{ fixed: ::T.class_of(Numeric) }}
617+
class << self; extend ::T::Generic; A = type_member {{ fixed: ::T.class_of(Numeric) }}
618618
end
619619
end
620620
end
621621
RUBY
622622
)
623623
end
624624

625+
def test_translate_to_rbi_fully_qualifies_extend_t_generic_and_helpers
626+
assert_rewrites_rbs(
627+
from: <<~RUBY,
628+
# @final
629+
#: [T]
630+
class Box
631+
end
632+
RUBY
633+
634+
to_pretty_format_for_humans: <<~RUBY,
635+
class Box
636+
extend ::T::Helpers
637+
638+
final!
639+
extend ::T::Generic
640+
641+
T = type_member
642+
end
643+
RUBY
644+
645+
to_line_matched_format_for_machines: <<~RUBY,
646+
# RBS_REWRITTEN_ANNOTATION: @final
647+
# RBS_WRITTEN_ANNOTATION: [T]
648+
class Box; extend ::T::Helpers; final!; extend ::T::Generic; T = type_member
649+
end
650+
RUBY
651+
)
652+
end
653+
654+
def test_translate_to_rbi_with_pre_existing_t_helpers
655+
assert_rewrites_rbs(
656+
from: <<~RUBY,
657+
# @final
658+
class Box
659+
extend T::Helpers
660+
end
661+
RUBY
662+
663+
to_pretty_format_for_humans: <<~RUBY,
664+
class Box
665+
extend ::T::Helpers
666+
667+
final!
668+
669+
extend T::Helpers
670+
end
671+
RUBY
672+
673+
to_line_matched_format_for_machines: <<~RUBY,
674+
# RBS_REWRITTEN_ANNOTATION: @final
675+
class Box; extend ::T::Helpers; final!
676+
extend T::Helpers
677+
end
678+
RUBY
679+
)
680+
end
681+
625682
def test_translate_to_rbi_preserves_generic_types
626683
contents = <<~RB
627684
#: -> Array[Integer]
@@ -1379,7 +1436,7 @@ class Foo
13791436
RUBY
13801437
to_pretty_format_for_humans: <<~RUBY,
13811438
class Foo
1382-
extend T::Generic
1439+
extend ::T::Generic
13831440
13841441
A = type_member
13851442
B = type_member
@@ -1388,7 +1445,7 @@ class Foo
13881445
to_line_matched_format_for_machines: <<~RUBY,
13891446
# RBS_WRITTEN_ANNOTATION: [A,
13901447
# RBS_WRITTEN_ANNOTATION: B]
1391-
class Foo; extend T::Generic; A = type_member; B = type_member
1448+
class Foo; extend ::T::Generic; A = type_member; B = type_member
13921449
end
13931450
RUBY
13941451
)

0 commit comments

Comments
 (0)