@@ -5,6 +5,63 @@ module Prototype
55 module Helpers
66 private
77
8+ # Prism can't parse Ruby 3.2 code
9+ if RUBY_VERSION >= "3.3"
10+ def parse_comments ( string , include_trailing :)
11+ Prism . parse_comments ( string , version : "current" ) . yield_self do |prism_comments | # steep:ignore UnexpectedKeywordArgument
12+ prism_comments . each_with_object ( { } ) do |comment , hash | #$ Hash[Integer, AST::Comment]
13+ # Skip EmbDoc comments
14+ next unless comment . is_a? ( Prism ::InlineComment )
15+ # skip like `module Foo # :nodoc:`
16+ next if comment . trailing? && !include_trailing
17+
18+ line = comment . location . start_line
19+ body = "#{ comment . location . slice } \n "
20+ body = body [ 2 ..-1 ] or raise
21+ body = "\n " if body . empty?
22+
23+ comment = AST ::Comment . new ( string : body , location : nil )
24+ if prev_comment = hash . delete ( line - 1 )
25+ hash [ line ] = AST ::Comment . new ( string : prev_comment . string + comment . string ,
26+ location : nil )
27+ else
28+ hash [ line ] = comment
29+ end
30+ end
31+ end
32+ end
33+ else
34+ require "ripper"
35+ def parse_comments ( string , include_trailing :)
36+ Ripper . lex ( string ) . yield_self do |tokens |
37+ code_lines = { } #: Hash[Integer, bool]
38+ tokens . each . with_object ( { } ) do |token , hash | #$ Hash[Integer, AST::Comment]
39+ case token [ 1 ]
40+ when :on_sp , :on_ignored_nl
41+ # skip
42+ when :on_comment
43+ line = token [ 0 ] [ 0 ]
44+ # skip like `module Foo # :nodoc:`
45+ next if code_lines [ line ] && !include_trailing
46+ body = token [ 2 ] [ 2 ..-1 ] or raise
47+
48+ body = "\n " if body . empty?
49+
50+ comment = AST ::Comment . new ( string : body , location : nil )
51+ if prev_comment = hash . delete ( line - 1 )
52+ hash [ line ] = AST ::Comment . new ( string : prev_comment . string + comment . string ,
53+ location : nil )
54+ else
55+ hash [ line ] = comment
56+ end
57+ else
58+ code_lines [ token [ 0 ] [ 0 ] ] = true
59+ end
60+ end
61+ end
62+ end
63+ end
64+
865 def block_from_body ( node )
966 _ , args_node , body_node = node . children
1067 _pre_num , _pre_init , _opt , _first_post , _post_num , _post_init , _rest , _kw , _kwrest , block_var = args_from_node ( args_node )
0 commit comments