@@ -27,6 +27,8 @@ def initialize(location, message)
2727 NonConstantClassName = _ = Class . new ( Base )
2828 NonConstantModuleName = _ = Class . new ( Base )
2929 TopLevelMethodDefinition = _ = Class . new ( Base )
30+ UnusedInlineAnnotation = _ = Class . new ( Base )
31+ AnnotationSyntaxError = _ = Class . new ( Base )
3032 end
3133
3234 def self . parse ( buffer , prism )
@@ -38,14 +40,15 @@ def self.parse(buffer, prism)
3840 end
3941
4042 class Parser < Prism ::Visitor
41- attr_reader :module_nesting , :result
43+ attr_reader :module_nesting , :result , :comments
4244
4345 include AST ::Ruby ::Helpers ::ConstantHelper
4446 include AST ::Ruby ::Helpers ::LocationHelper
4547
4648 def initialize ( result )
4749 @result = result
4850 @module_nesting = [ ]
51+ @comments = CommentAssociation . build ( result . buffer , result . prism_result )
4952 end
5053
5154 def buffer
@@ -85,6 +88,10 @@ def visit_class_node(node)
8588 push_module_nesting ( class_decl ) do
8689 visit_child_nodes ( node )
8790 end
91+
92+ comments . each_enclosed_block ( node ) do |block |
93+ report_unused_block ( block )
94+ end
8895 end
8996
9097 def visit_module_node ( node )
@@ -101,6 +108,10 @@ def visit_module_node(node)
101108 push_module_nesting ( module_decl ) do
102109 visit_child_nodes ( node )
103110 end
111+
112+ comments . each_enclosed_block ( node ) do |block |
113+ report_unused_block ( block )
114+ end
104115 end
105116
106117 def visit_def_node ( node )
@@ -114,8 +125,24 @@ def visit_def_node(node)
114125
115126 case current = current_module
116127 when AST ::Ruby ::Declarations ::ClassDecl , AST ::Ruby ::Declarations ::ModuleDecl
117- defn = AST ::Ruby ::Members ::DefMember . new ( buffer , node . name , node )
128+ leading_block = comments . leading_block! ( node )
129+
130+ if node . end_keyword_loc
131+ # Not an end-less def
132+ end_loc = node . rparen_loc || node . parameters &.location || node . name_loc
133+ trailing_block = comments . trailing_block! ( end_loc )
134+ end
135+
136+ method_type , leading_unuseds , trailing_unused = AST ::Ruby ::Members ::MethodTypeAnnotation . build ( leading_block , trailing_block , [ ] )
137+ report_unused_annotation ( trailing_unused , *leading_unuseds )
138+
139+ defn = AST ::Ruby ::Members ::DefMember . new ( buffer , node . name , node , method_type )
118140 current . members << defn
141+
142+ # Skip other comments in `def` node
143+ comments . each_enclosed_block ( node ) do |block |
144+ comments . associated_blocks << block
145+ end
119146 else
120147 diagnostics << Diagnostic ::TopLevelMethodDefinition . new (
121148 rbs_location ( node . name_loc ) ,
@@ -131,6 +158,32 @@ def insert_declaration(decl)
131158 result . declarations << decl
132159 end
133160 end
161+
162+ def report_unused_annotation ( *annotations )
163+ annotations . each do |annotation |
164+ case annotation
165+ when AST ::Ruby ::CommentBlock ::AnnotationSyntaxError
166+ diagnostics << Diagnostic ::AnnotationSyntaxError . new (
167+ annotation . location , "Syntax error: " + annotation . error . error_message
168+ )
169+ when AST ::Ruby ::Annotations ::Base
170+ diagnostics << Diagnostic ::UnusedInlineAnnotation . new (
171+ annotation . location , "Unused inline rbs annotation"
172+ )
173+ end
174+ end
175+ end
176+
177+ def report_unused_block ( block )
178+ block . each_paragraph ( [ ] ) do |paragraph |
179+ case paragraph
180+ when Location
181+ # noop
182+ else
183+ report_unused_annotation ( paragraph )
184+ end
185+ end
186+ end
134187 end
135188 end
136189end
0 commit comments