Skip to content

Commit

Permalink
Add --type and --method-type option to rbs parse
Browse files Browse the repository at this point in the history
  • Loading branch information
pocke committed Feb 24, 2023
1 parent aa14e4d commit f17d62b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/rbs/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,9 @@ def run_vendor(args, options)
end

def run_parse(args, options)
parse_method = :parse_signature
e_code = nil

OptionParser.new do |opts|
opts.banner = <<-EOB
Usage: rbs parse [files...]
Expand All @@ -919,6 +921,8 @@ def run_parse(args, options)
EOB

opts.on('-e CODE', 'One line RBS script to parse') { |e| e_code = e }
opts.on('--type', 'Parse code as a type') { |e| parse_method = :parse_type }
opts.on('--method-type', 'Parse code as a method type') { |e| parse_method = :parse_method_type }
end.parse!(args)

loader = options.loader()
Expand All @@ -934,7 +938,7 @@ def run_parse(args, options)

bufs.each do |buf|
RBS.logger.info "Parsing #{buf.name}..."
Parser.parse_signature(buf)
Parser.public_send(parse_method, buf)
rescue RBS::ParsingError => ex
stdout.puts ex.message
syntax_error = true
Expand Down
24 changes: 24 additions & 0 deletions test/rbs/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,30 @@ def test_parse_e
end
end

def test_parse_type
with_cli do |cli|
cli.run(['parse', '--type', '-e', 'bool'])
assert_empty stdout.string

assert_raises(SystemExit) { cli.run(['parse', '--type', '-e', '?']) }
assert_equal [
"-e:1:0...1:1: Syntax error: unexpected token for simple type, token=`?` (pQUESTION)",
], stdout.string.split("\n").sort
end
end

def test_parse_method_type
with_cli do |cli|
cli.run(['parse', '--method-type', '-e', '() -> void'])
assert_empty stdout.string

assert_raises(SystemExit) { cli.run(['parse', '--method-type', '-e', '()']) }
assert_equal [
"-e:1:2...1:3: Syntax error: expected a token `pARROW`, token=`` (pEOF)",
], stdout.string.split("\n").sort
end
end

def test_prototype_no_parser
Dir.mktmpdir do |dir|
with_cli do |cli|
Expand Down

0 comments on commit f17d62b

Please sign in to comment.