Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 46 additions & 16 deletions lib/prism/translation/ruby_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -415,14 +415,18 @@ def visit_class_node(node)
visit(node.constant_path)
end

if node.body.nil?
s(node, :class, name, visit(node.superclass))
elsif node.body.is_a?(StatementsNode)
compiler = copy_compiler(in_def: false)
s(node, :class, name, visit(node.superclass)).concat(node.body.body.map { |child| child.accept(compiler) })
else
s(node, :class, name, visit(node.superclass), node.body.accept(copy_compiler(in_def: false)))
end
result =
if node.body.nil?
s(node, :class, name, visit(node.superclass))
elsif node.body.is_a?(StatementsNode)
compiler = copy_compiler(in_def: false)
s(node, :class, name, visit(node.superclass)).concat(node.body.body.map { |child| child.accept(compiler) })
else
s(node, :class, name, visit(node.superclass), node.body.accept(copy_compiler(in_def: false)))
end

attach_comments(result, node)
result
end

# ```
Expand Down Expand Up @@ -611,7 +615,9 @@ def visit_def_node(node)
s(node, :defs, visit(node.receiver), name)
end

attach_comments(result, node)
result.line(node.name_loc.start_line)

if node.parameters.nil?
result << s(node, :args).line(node.name_loc.start_line)
else
Expand Down Expand Up @@ -1270,14 +1276,18 @@ def visit_module_node(node)
visit(node.constant_path)
end

if node.body.nil?
s(node, :module, name)
elsif node.body.is_a?(StatementsNode)
compiler = copy_compiler(in_def: false)
s(node, :module, name).concat(node.body.body.map { |child| child.accept(compiler) })
else
s(node, :module, name, node.body.accept(copy_compiler(in_def: false)))
end
result =
if node.body.nil?
s(node, :module, name)
elsif node.body.is_a?(StatementsNode)
compiler = copy_compiler(in_def: false)
s(node, :module, name).concat(node.body.body.map { |child| child.accept(compiler) })
else
s(node, :module, name, node.body.accept(copy_compiler(in_def: false)))
end

attach_comments(result, node)
result
end

# ```
Expand Down Expand Up @@ -1820,6 +1830,17 @@ def visit_yield_node(node)

private

# Attach prism comments to the given sexp.
def attach_comments(sexp, node)
return unless node.comments
return if node.comments.empty?

extra = node.location.start_line - node.comments.last.location.start_line
comments = node.comments.map(&:slice)
comments.concat([nil] * [0, extra].max)
sexp.comments = comments.join("\n")
end

# Create a new compiler with the given options.
def copy_compiler(in_def: self.in_def, in_pattern: self.in_pattern)
Compiler.new(file, in_def: in_def, in_pattern: in_pattern)
Expand Down Expand Up @@ -1898,6 +1919,14 @@ def parse_file(filepath)
translate(Prism.parse_file(filepath, partial_script: true), filepath)
end

# Parse the give file and translate it into the
# seattlerb/ruby_parser gem's Sexp format. This method is
# provided for API compatibility to RubyParser and takes an
# optional +timeout+ argument.
def process(ruby, file = "(string)", timeout = nil)
Timeout.timeout(timeout) { parse(ruby, file) }
end

class << self
# Parse the given source and translate it into the seattlerb/ruby_parser
# gem's Sexp format.
Expand All @@ -1922,6 +1951,7 @@ def translate(result, filepath)
raise ::RubyParser::SyntaxError, "#{filepath}:#{error.location.start_line} :: #{error.message}"
end

result.attach_comments!
result.value.accept(Compiler.new(filepath))
end
end
Expand Down
Loading