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

Should raise ParsingError instead of ArgumentError #1652

Merged
merged 1 commit into from
Nov 27, 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
8 changes: 8 additions & 0 deletions ext/rbs_extension/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,14 @@ static VALUE parse_function_param(parserstate *state) {
param_range.start = type_range.start;
param_range.end = name_range.end;

if (!is_keyword_token(state->current_token.type)) {
raise_syntax_error(
state,
state->current_token,
"unexpected token for function parameter name"
);
}

VALUE name = rb_to_symbol(rbs_unquote_string(state, state->current_token.range, 0));
VALUE location = rbs_new_location(state->buffer, param_range);
rbs_loc *loc = rbs_check_location(location);
Expand Down
14 changes: 13 additions & 1 deletion test/rbs/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ def test_parse_method_type
assert_raises RBS::ParsingError do
RBS::Parser.parse_method_type(buffer("(foo + 1) -> void"))
end.tap do |exn|
assert_equal "test.rbs:1:7...1:8: Syntax error: unexpected token for method type parameters, token=`1` (tINTEGER)", exn.message
assert_equal "test.rbs:1:5...1:6: Syntax error: unexpected token for function parameter name, token=`+` (tOPERATOR)", exn.message
end

assert_raises RBS::ParsingError do
Expand All @@ -650,6 +650,18 @@ def test_parse_method_type
assert_equal "test.rbs:1:15...1:18: Syntax error: required keyword argument type is expected, token=`Bar` (tUIDENT)", exn.message
end

assert_raises RBS::ParsingError do
RBS::Parser.parse_method_type(buffer("(foo`: untyped) -> void"))
end.tap do |exn|
assert_equal "test.rbs:1:4...1:5: Syntax error: unexpected token for function parameter name, token=``` (tOPERATOR)", exn.message
end

assert_raises RBS::ParsingError do
RBS::Parser.parse_method_type(buffer("(?foo\": untyped) -> void"))
end.tap do |exn|
assert_equal "test.rbs:1:5...1:6: Syntax error: unexpected token for function parameter name, token=`\"` (ErrorToken)", exn.message
end

assert_raises RBS::ParsingError do
RBS::Parser.parse_method_type(buffer("(**untyped, ?Bar) -> void"))
end.tap do |exn|
Expand Down