Skip to content
Open
Show file tree
Hide file tree
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
18 changes: 9 additions & 9 deletions lib/rbs/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ def parse_logging_options(opts)
opts
end

def has_parser?
def has_parser?(format)
return true if format == "rbi"
defined?(RubyVM::AbstractSyntaxTree) ? true : false
end

Expand Down Expand Up @@ -683,7 +684,7 @@ def autoload(name, path)
end

def run_prototype_file(format, args)
availability = unless has_parser?
availability = unless has_parser?(format)
"\n** This command does not work on this interpreter (#{RUBY_ENGINE}) **\n"
end

Expand Down Expand Up @@ -728,7 +729,7 @@ def run_prototype_file(format, args)

opts.parse!(args)

unless has_parser?
unless has_parser?(format)
stdout.puts "Not supported on this interpreter (#{RUBY_ENGINE})."
return 1
end
Expand All @@ -741,7 +742,7 @@ def run_prototype_file(format, args)
new_parser = -> do
case format
when "rbi"
Prototype::RBI.new()
Prototype::RBI
when "rb"
Prototype::RB.new()
else
Expand Down Expand Up @@ -796,7 +797,7 @@ def run_prototype_file(format, args)

parser = new_parser[]
begin
parser.parse file_path.read()
decls = parser.parse file_path.read()
rescue SyntaxError
stdout.puts " ⚠️ Unable to parse due to SyntaxError: `#{file_path}`"
next
Expand All @@ -817,7 +818,7 @@ def run_prototype_file(format, args)
(output_path.parent).mkpath
output_path.open("w") do |io|
writer = Writer.new(out: io)
writer.write(parser.decls)
writer.write(decls)
end
end
end
Expand All @@ -837,13 +838,12 @@ def run_prototype_file(format, args)
else
# file mode
parser = new_parser[]
writer = Writer.new(out: stdout)

input_paths.each do |file|
parser.parse file.read()
writer.write parser.parse(file.read())
end

writer = Writer.new(out: stdout)
writer.write parser.decls
end

0
Expand Down
40 changes: 22 additions & 18 deletions lib/rbs/prototype/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,28 @@ module Helpers

def parse_comments(string, include_trailing:)
Prism.parse_comments(string, version: "current").yield_self do |prism_comments| # steep:ignore UnexpectedKeywordArgument
prism_comments.each_with_object({}) do |comment, hash| #$ Hash[Integer, AST::Comment]
# Skip EmbDoc comments
next unless comment.is_a?(Prism::InlineComment)
# skip like `module Foo # :nodoc:`
next if comment.trailing? && !include_trailing

line = comment.location.start_line
body = "#{comment.location.slice}\n"
body = body[2..-1] or raise
body = "\n" if body.empty?

comment = AST::Comment.new(string: body, location: nil)
if prev_comment = hash.delete(line - 1)
hash[line] = AST::Comment.new(string: prev_comment.string + comment.string,
location: nil)
else
hash[line] = comment
end
process_comments(prism_comments, include_trailing: include_trailing)
end
end

def process_comments(comments, include_trailing:)
comments.each_with_object({}) do |comment, hash| #$ Hash[Integer, AST::Comment]
# Skip EmbDoc comments
next unless comment.is_a?(Prism::InlineComment)
# skip like `module Foo # :nodoc:`
next if comment.trailing? && !include_trailing

line = comment.location.start_line
body = "#{comment.slice}\n"
body = body[2..-1] or raise
body = "\n" if body.empty?

comment = AST::Comment.new(string: body, location: nil)
if prev_comment = hash.delete(line - 1)
hash[line] = AST::Comment.new(string: prev_comment.string + comment.string,
location: nil)
else
hash[line] = comment
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/rbs/prototype/rb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def parse(string)
comments = parse_comments(string, include_trailing: false)

process RubyVM::AbstractSyntaxTree.parse(string), decls: source_decls, comments: comments, context: Context.initial
decls
end

def process(node, decls:, comments:, context:)
Expand Down
Loading
Loading