diff --git a/src/parser.c b/src/parser.c index 5effb219e..de7e82dc2 100644 --- a/src/parser.c +++ b/src/parser.c @@ -2350,7 +2350,7 @@ static bool parse_variable_member(rbs_parser_t *parser, rbs_position_t comment_p if (parser->next_token.type == tAIDENT || parser->next_token.type == kATRBS) { rbs_parser_advance(parser); } else { - rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error"); + rbs_parser_set_error(parser, parser->next_token, true, "unexpected token for class instance variable name"); return false; } diff --git a/test/rbs/errors_test.rb b/test/rbs/errors_test.rb index 6aed3fbb0..a152b5ba3 100644 --- a/test/rbs/errors_test.rb +++ b/test/rbs/errors_test.rb @@ -24,6 +24,29 @@ class Foo end end + def test_parse_signature_with_malformed_class_instance_variable + ["class M self.", "module M self.", "class M self.5", %q{class M self."x"}].each do |source| + assert_raises RBS::ParsingError, "#{source.inspect} should raise RBS::ParsingError" do + RBS::Parser.parse_signature(buffer(source)) + end + end + end + + def test_parse_signature_with_malformed_class_instance_variable_detailed_message + omit "Exception#detailed_message does not supported" unless Exception.method_defined?(:detailed_message) + + assert_raises RBS::ParsingError do + RBS::Parser.parse_signature(buffer("class M self.5")) + end.tap do |exn| + assert_equal <<~DETAILED_MESSAGE, exn.detailed_message + test.rbs:1:13...1:14: Syntax error: unexpected token for class instance variable name, token=`5` (tINTEGER) (RBS::ParsingError) + + class M self.5 + ^ + DETAILED_MESSAGE + end + end + def test_parse_type_with_parsing_error_detailed_message omit "Exception#detailed_message does not supported" unless Exception.method_defined?(:detailed_message)