Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -e, --type, and --method-type options to rbs parse #1252

Merged
merged 3 commits into from
Apr 26, 2023
Merged
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
35 changes: 27 additions & 8 deletions lib/rbs/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,10 @@ def run_vendor(args, options)
end

def run_parse(args, options)
parse_method = :parse_signature
# @type var e_code: String?
e_code = nil

OptionParser.new do |opts|
opts.banner = <<-EOB
Usage: rbs parse [files...]
Expand All @@ -930,22 +934,37 @@ def run_parse(args, options)
Examples:

$ rbs parse sig/app/models.rbs sig/app/controllers.rbs

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()

syntax_error = false
args.each do |path|
bufs = args.flat_map do |path|
path = Pathname(path)
loader.each_file(path, skip_hidden: false, immediate: true) do |file_path|
RBS.logger.info "Parsing #{file_path}..."
buffer = Buffer.new(content: file_path.read, name: file_path)
Parser.parse_signature(buffer)
rescue RBS::ParsingError => ex
stdout.puts ex.message
syntax_error = true
loader.each_file(path, skip_hidden: false, immediate: true).map do |file_path|
Buffer.new(content: file_path.read, name: file_path)
end
end
bufs << Buffer.new(content: e_code, name: '-e') if e_code

bufs.each do |buf|
RBS.logger.info "Parsing #{buf.name}..."
case parse_method
when :parse_signature
Parser.parse_signature(buf)
else
Parser.public_send(parse_method, buf, require_eof: true)
end
rescue RBS::ParsingError => ex
stdout.puts ex.message
syntax_error = true
end

exit 1 if syntax_error
Expand Down
2 changes: 2 additions & 0 deletions lib/rbs/environment_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ def each_dir
end

def each_file(path, immediate:, skip_hidden:, &block)
return enum_for(__method__, path, immediate: immediate, skip_hidden: skip_hidden) unless block

case
when path.file?
if path.extname == ".rbs" || immediate
Expand Down
1 change: 1 addition & 0 deletions sig/environment_loader.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,6 @@ module RBS
def each_dir: { (source, Pathname) -> void } -> void

def each_file: (Pathname path, immediate: boolish, skip_hidden: boolish) { (Pathname) -> void } -> void
| (Pathname path, immediate: boolish, skip_hidden: boolish) -> Enumerator[Pathname, void]
end
end
36 changes: 36 additions & 0 deletions test/rbs/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,42 @@ def foo: () -> void
end
end

def test_parse_e
with_cli do |cli|
cli.run(['parse', '-e', 'class C end'])
assert_empty stdout.string

assert_raises(SystemExit) { cli.run(['parse', '-e', 'class C en']) }
assert_equal [
"-e:1:8...1:10: Syntax error: unexpected token for class/module declaration member, token=`en` (tLIDENT)"
], stdout.string.split("\n").sort
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