Skip to content

Commit ea9cf09

Browse files
committed
Add force option to RBS comment translation
1 parent 56b59c5 commit ea9cf09

4 files changed

Lines changed: 27 additions & 9 deletions

File tree

lib/spoom/sorbet/translate.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,16 @@ def sorbet_sigs_to_rbs_comments(
6060
# Converts all the RBS comments in the given Ruby code to `sig` nodes.
6161
# It also handles type members and class annotations.
6262
#: (String ruby_contents, file: String, ?max_line_length: Integer?,
63-
#| ?overloads_strategy: Symbol, ?erase_generic_types: bool) -> String
63+
#| ?overloads_strategy: Symbol, ?erase_generic_types: bool, ?force: bool) -> String
6464
def rbs_comments_to_sorbet_sigs(ruby_contents, file:, max_line_length: nil, overloads_strategy: :translate_all,
65-
erase_generic_types: false)
65+
erase_generic_types: false, force: false)
6666
RBSCommentsToSorbetSigs.rewrite_if_needed(
6767
ruby_contents,
6868
file: file,
6969
max_line_length: max_line_length,
7070
overloads_strategy: overloads_strategy,
7171
erase_generic_types: erase_generic_types,
72+
force: force,
7273
)
7374
end
7475

lib/spoom/sorbet/translate/rbs_comments_to_sorbet_sigs.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,18 @@ def contains_rbs_syntax?(source)
2929
#| file: String,
3030
#| ?max_line_length: Integer?,
3131
#| ?overloads_strategy: Symbol,
32-
#| ?erase_generic_types: bool) -> String
32+
#| ?erase_generic_types: bool,
33+
#| ?force: bool) -> String
3334
def rewrite_if_needed(
3435
ruby_contents,
3536
file:,
3637
max_line_length: nil,
3738
overloads_strategy: :translate_all,
38-
erase_generic_types: false
39+
erase_generic_types: false,
40+
force: false
3941
)
40-
return ruby_contents unless contains_rbs_syntax?(ruby_contents)
42+
return ruby_contents unless ruby_contents.match?(RBS_REWRITE_PATTERN) &&
43+
(force || Sigils.contains_valid_sigil?(ruby_contents))
4144

4245
options = Options.new(
4346
overloads_strategy:,

rbi/spoom.rbi

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3241,10 +3241,11 @@ module Spoom::Sorbet::Translate
32413241
file: ::String,
32423242
max_line_length: T.nilable(::Integer),
32433243
overloads_strategy: ::Symbol,
3244-
erase_generic_types: T::Boolean
3244+
erase_generic_types: T::Boolean,
3245+
force: T::Boolean
32453246
).returns(::String)
32463247
end
3247-
def rbs_comments_to_sorbet_sigs(ruby_contents, file:, max_line_length: T.unsafe(nil), overloads_strategy: T.unsafe(nil), erase_generic_types: T.unsafe(nil)); end
3248+
def rbs_comments_to_sorbet_sigs(ruby_contents, file:, max_line_length: T.unsafe(nil), overloads_strategy: T.unsafe(nil), erase_generic_types: T.unsafe(nil), force: T.unsafe(nil)); end
32483249

32493250
sig do
32503251
params(
@@ -3324,10 +3325,11 @@ module Spoom::Sorbet::Translate::RBSCommentsToSorbetSigs
33243325
file: ::String,
33253326
max_line_length: T.nilable(::Integer),
33263327
overloads_strategy: ::Symbol,
3327-
erase_generic_types: T::Boolean
3328+
erase_generic_types: T::Boolean,
3329+
force: T::Boolean
33283330
).returns(::String)
33293331
end
3330-
def rewrite_if_needed(ruby_contents, file:, max_line_length: T.unsafe(nil), overloads_strategy: T.unsafe(nil), erase_generic_types: T.unsafe(nil)); end
3332+
def rewrite_if_needed(ruby_contents, file:, max_line_length: T.unsafe(nil), overloads_strategy: T.unsafe(nil), erase_generic_types: T.unsafe(nil), force: T.unsafe(nil)); end
33313333
end
33323334
end
33333335

test/spoom/sorbet/translate/rbs_comments_to_sorbet_sigs_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,6 +1597,18 @@ def foo; end
15971597
end
15981598
end
15991599

1600+
def test_rewrite_force_translates_files_without_typed_sigil
1601+
source = <<~RB
1602+
#: -> void
1603+
def foo; end
1604+
RB
1605+
1606+
assert_equal(<<~RB, RBSCommentsToSorbetSigs.rewrite_if_needed(source, file: "test.rb", force: true))
1607+
sig { void }
1608+
def foo; end
1609+
RB
1610+
end
1611+
16001612
private
16011613

16021614
#: (String,

0 commit comments

Comments
 (0)