File tree Expand file tree Collapse file tree
lib/spoom/sorbet/translate
test/spoom/sorbet/translate Expand file tree Collapse file tree Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments