Skip to content

Commit 8aeadab

Browse files
committed
Do not translate Sorbet assertions in ternary expressions
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
1 parent 0bfaf25 commit 8aeadab

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

lib/spoom/sorbet/translate/sorbet_assertions_to_rbs_comments.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,27 @@ module Translate
88
class SorbetAssertionsToRBSComments < Translator
99
LINE_BREAK = "\n".ord #: Integer
1010

11+
# @override
12+
#: (Prism::IfNode) -> void
13+
def visit_if_node(node)
14+
if node.if_keyword_loc
15+
# We do not translate assertions in ternary expressions to avoid altering the semantic of the code.
16+
#
17+
# For example:
18+
# ```rb
19+
# a = T.must(b) ? T.must(c) : T.must(d)
20+
# ```
21+
#
22+
# would become
23+
# ```rb
24+
# a = T.must(b) ? T.must(c) : d #: !nil
25+
# ```
26+
#
27+
# which does not match the original intent.
28+
super
29+
end
30+
end
31+
1132
# @override
1233
#: (Prism::CallNode) -> void
1334
def visit_call_node(node)

test/spoom/sorbet/translate/sorbet_assertions_to_rbs_comments_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,14 @@ def test_doesnt_translate_cast_in_string_interpolation
482482
assert_equal(rb, rbi_to_rbs(rb))
483483
end
484484

485+
def test_doesnt_translate_in_ternary_expressions
486+
rb = <<~RB
487+
a = T.must(b) ? T.must(c) : T.must(d)
488+
RB
489+
490+
assert_equal(rb, rbi_to_rbs(rb))
491+
end
492+
485493
private
486494

487495
#: (String) -> String

0 commit comments

Comments
 (0)