Skip to content

Commit bb4a6f3

Browse files
zenspidermatzbot
authored andcommitted
[ruby/prism] Fixed Prism::Translation::RubyParser's comment processing
Tests were failing in Flay under Prism. ruby/prism@af9b3640a8
1 parent b8ba9ce commit bb4a6f3

File tree

1 file changed

+46
-16
lines changed

1 file changed

+46
-16
lines changed

lib/prism/translation/ruby_parser.rb

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -415,14 +415,18 @@ def visit_class_node(node)
415415
visit(node.constant_path)
416416
end
417417

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
418+
result =
419+
if node.body.nil?
420+
s(node, :class, name, visit(node.superclass))
421+
elsif node.body.is_a?(StatementsNode)
422+
compiler = copy_compiler(in_def: false)
423+
s(node, :class, name, visit(node.superclass)).concat(node.body.body.map { |child| child.accept(compiler) })
424+
else
425+
s(node, :class, name, visit(node.superclass), node.body.accept(copy_compiler(in_def: false)))
426+
end
427+
428+
attach_comments(result, node)
429+
result
426430
end
427431

428432
# ```
@@ -611,7 +615,9 @@ def visit_def_node(node)
611615
s(node, :defs, visit(node.receiver), name)
612616
end
613617

618+
attach_comments(result, node)
614619
result.line(node.name_loc.start_line)
620+
615621
if node.parameters.nil?
616622
result << s(node, :args).line(node.name_loc.start_line)
617623
else
@@ -1270,14 +1276,18 @@ def visit_module_node(node)
12701276
visit(node.constant_path)
12711277
end
12721278

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
1279+
result =
1280+
if node.body.nil?
1281+
s(node, :module, name)
1282+
elsif node.body.is_a?(StatementsNode)
1283+
compiler = copy_compiler(in_def: false)
1284+
s(node, :module, name).concat(node.body.body.map { |child| child.accept(compiler) })
1285+
else
1286+
s(node, :module, name, node.body.accept(copy_compiler(in_def: false)))
1287+
end
1288+
1289+
attach_comments(result, node)
1290+
result
12811291
end
12821292

12831293
# ```
@@ -1820,6 +1830,17 @@ def visit_yield_node(node)
18201830

18211831
private
18221832

1833+
# Attach prism comments to the given sexp.
1834+
def attach_comments(sexp, node)
1835+
return unless node.comments
1836+
return if node.comments.empty?
1837+
1838+
extra = node.location.start_line - node.comments.last.location.start_line
1839+
comments = node.comments.map(&:slice)
1840+
comments.concat([nil] * [0, extra].max)
1841+
sexp.comments = comments.join("\n")
1842+
end
1843+
18231844
# Create a new compiler with the given options.
18241845
def copy_compiler(in_def: self.in_def, in_pattern: self.in_pattern)
18251846
Compiler.new(file, in_def: in_def, in_pattern: in_pattern)
@@ -1898,6 +1919,14 @@ def parse_file(filepath)
18981919
translate(Prism.parse_file(filepath, partial_script: true), filepath)
18991920
end
19001921

1922+
# Parse the give file and translate it into the
1923+
# seattlerb/ruby_parser gem's Sexp format. This method is
1924+
# provided for API compatibility to RubyParser and takes an
1925+
# optional +timeout+ argument.
1926+
def process(ruby, file = "(string)", timeout = nil)
1927+
Timeout.timeout(timeout) { parse(ruby, file) }
1928+
end
1929+
19011930
class << self
19021931
# Parse the given source and translate it into the seattlerb/ruby_parser
19031932
# gem's Sexp format.
@@ -1922,6 +1951,7 @@ def translate(result, filepath)
19221951
raise ::RubyParser::SyntaxError, "#{filepath}:#{error.location.start_line} :: #{error.message}"
19231952
end
19241953

1954+
result.attach_comments!
19251955
result.value.accept(Compiler.new(filepath))
19261956
end
19271957
end

0 commit comments

Comments
 (0)