@@ -35,6 +35,16 @@ def initialize(file, in_def: false, in_pattern: false)
3535 @in_pattern = in_pattern
3636 end
3737
38+ def attach_comments sexp , node # :nodoc:
39+ return unless node . comments
40+ return if node . comments . empty?
41+
42+ extra = node . location . start_line - node . comments . last . location . start_line
43+ comments = node . comments . map ( &:slice )
44+ comments . concat [ nil ] * extra . clamp ( 0 , nil )
45+ sexp . comments = comments . join "\n "
46+ end
47+
3848 # ```
3949 # alias foo bar
4050 # ^^^^^^^^^^^^^
@@ -415,14 +425,19 @@ def visit_class_node(node)
415425 visit ( node . constant_path )
416426 end
417427
418- if node . body . nil?
419- s ( node , :class , name , visit ( node . superclass ) )
420- elsif node . body . is_a? ( StatementsNode )
421- compiler = copy_compiler ( in_def : false )
422- s ( node , :class , name , visit ( node . superclass ) ) . concat ( node . body . body . map { |child | child . accept ( compiler ) } )
423- else
424- s ( node , :class , name , visit ( node . superclass ) , node . body . accept ( copy_compiler ( in_def : false ) ) )
425- end
428+ result =
429+ if node . body . nil?
430+ s ( node , :class , name , visit ( node . superclass ) )
431+ elsif node . body . is_a? ( StatementsNode )
432+ compiler = copy_compiler ( in_def : false )
433+ s ( node , :class , name , visit ( node . superclass ) ) . concat ( node . body . body . map { |child | child . accept ( compiler ) } )
434+ else
435+ s ( node , :class , name , visit ( node . superclass ) , node . body . accept ( copy_compiler ( in_def : false ) ) )
436+ end
437+
438+ attach_comments ( result , node )
439+
440+ result
426441 end
427442
428443 # ```
@@ -611,7 +626,10 @@ def visit_def_node(node)
611626 s ( node , :defs , visit ( node . receiver ) , name )
612627 end
613628
629+ attach_comments ( result , node )
630+
614631 result . line ( node . name_loc . start_line )
632+
615633 if node . parameters . nil?
616634 result << s ( node , :args ) . line ( node . name_loc . start_line )
617635 else
@@ -1270,14 +1288,19 @@ def visit_module_node(node)
12701288 visit ( node . constant_path )
12711289 end
12721290
1273- if node . body . nil?
1274- s ( node , :module , name )
1275- elsif node . body . is_a? ( StatementsNode )
1276- compiler = copy_compiler ( in_def : false )
1277- s ( node , :module , name ) . concat ( node . body . body . map { |child | child . accept ( compiler ) } )
1278- else
1279- s ( node , :module , name , node . body . accept ( copy_compiler ( in_def : false ) ) )
1280- end
1291+ result =
1292+ if node . body . nil?
1293+ s ( node , :module , name )
1294+ elsif node . body . is_a? ( StatementsNode )
1295+ compiler = copy_compiler ( in_def : false )
1296+ s ( node , :module , name ) . concat ( node . body . body . map { |child | child . accept ( compiler ) } )
1297+ else
1298+ s ( node , :module , name , node . body . accept ( copy_compiler ( in_def : false ) ) )
1299+ end
1300+
1301+ attach_comments ( result , node )
1302+
1303+ result
12811304 end
12821305
12831306 # ```
@@ -1898,6 +1921,16 @@ def parse_file(filepath)
18981921 translate ( Prism . parse_file ( filepath , partial_script : true ) , filepath )
18991922 end
19001923
1924+ # Parse the give file and translate it into the
1925+ # seattlerb/ruby_parser gem's Sexp format. This method is
1926+ # provided for API compatibility to RubyParser and takes an
1927+ # optional +timeout+ argument.
1928+ def process ruby , file = "(string)" , timeout = nil
1929+ Timeout . timeout timeout do
1930+ parse ruby , file
1931+ end
1932+ end
1933+
19011934 class << self
19021935 # Parse the given source and translate it into the seattlerb/ruby_parser
19031936 # gem's Sexp format.
@@ -1922,6 +1955,8 @@ def translate(result, filepath)
19221955 raise ::RubyParser ::SyntaxError , "#{ filepath } :#{ error . location . start_line } :: #{ error . message } "
19231956 end
19241957
1958+ result . attach_comments!
1959+
19251960 result . value . accept ( Compiler . new ( filepath ) )
19261961 end
19271962 end
0 commit comments