@@ -8,6 +8,15 @@ module Translate
88 class SorbetAssertionsToRBSComments < Translator
99 LINE_BREAK = "\n " . ord #: Integer
1010
11+ # @override
12+ #: (Prism::StatementsNode) -> void
13+ def visit_statements_node ( node )
14+ node . body . each do |statement |
15+ translated = maybe_translate_assertion ( statement )
16+ visit ( statement ) unless translated
17+ end
18+ end
19+
1120 # @override
1221 #: (Prism::IfNode) -> void
1322 def visit_if_node ( node )
@@ -29,21 +38,39 @@ def visit_if_node(node)
2938 end
3039 end
3140
32- # @override
33- #: (Prism::CallNode) -> void
34- def visit_call_node ( node )
35- return super unless t_annotation? ( node )
36- return super unless at_end_of_line? ( node )
41+ private
42+
43+ #: (Prism::Node) -> bool
44+ def maybe_translate_assertion ( node )
45+ node = case node
46+ when Prism ::MultiWriteNode ,
47+ Prism ::ClassVariableWriteNode , Prism ::ClassVariableAndWriteNode , Prism ::ClassVariableOperatorWriteNode , Prism ::ClassVariableOrWriteNode ,
48+ Prism ::ConstantWriteNode , Prism ::ConstantAndWriteNode , Prism ::ConstantOperatorWriteNode , Prism ::ConstantOrWriteNode ,
49+ Prism ::ConstantPathWriteNode , Prism ::ConstantPathAndWriteNode , Prism ::ConstantPathOperatorWriteNode , Prism ::ConstantPathOrWriteNode ,
50+ Prism ::GlobalVariableWriteNode , Prism ::GlobalVariableAndWriteNode , Prism ::GlobalVariableOperatorWriteNode , Prism ::GlobalVariableOrWriteNode ,
51+ Prism ::InstanceVariableWriteNode , Prism ::InstanceVariableAndWriteNode , Prism ::InstanceVariableOperatorWriteNode , Prism ::InstanceVariableOrWriteNode ,
52+ Prism ::LocalVariableWriteNode , Prism ::LocalVariableAndWriteNode , Prism ::LocalVariableOperatorWriteNode , Prism ::LocalVariableOrWriteNode ,
53+ Prism ::CallAndWriteNode , Prism ::CallOperatorWriteNode , Prism ::CallOrWriteNode
54+ node . value
55+ when Prism ::CallNode
56+ node
57+ else
58+ return false
59+ end
60+
61+ return false unless node . is_a? ( Prism ::CallNode )
62+ return false unless t_annotation? ( node )
63+ return false unless at_end_of_line? ( node )
3764
3865 value = T . must ( node . arguments &.arguments &.first )
3966 rbs_annotation = build_rbs_annotation ( node )
4067
4168 start_offset = node . location . start_offset
4269 end_offset = node . location . end_offset
4370 @rewriter << Source ::Replace . new ( start_offset , end_offset - 1 , "#{ dedent_value ( node , value ) } #{ rbs_annotation } " )
44- end
4571
46- private
72+ true
73+ end
4774
4875 #: (Prism::CallNode) -> void
4976 def build_rbs_annotation ( call )
0 commit comments