Skip to content

Commit 4a30c69

Browse files
committed
Properly handle missing method names
1 parent 7c38d6d commit 4a30c69

File tree

2 files changed

+11
-15
lines changed

2 files changed

+11
-15
lines changed

src/yarp.c

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9774,7 +9774,6 @@ parse_conditional(yp_parser_t *parser, yp_context_t context) {
97749774
case YP_TOKEN_KEYWORD_TRUE: case YP_TOKEN_KEYWORD_UNDEF: case YP_TOKEN_KEYWORD_UNLESS: case YP_TOKEN_KEYWORD_UNTIL: \
97759775
case YP_TOKEN_KEYWORD_WHEN: case YP_TOKEN_KEYWORD_WHILE: case YP_TOKEN_KEYWORD_YIELD
97769776

9777-
97789777
// This macro allows you to define a case statement for all of the operators.
97799778
// It's meant to be used in a switch statement.
97809779
#define YP_CASE_OPERATOR YP_TOKEN_AMPERSAND: case YP_TOKEN_BACKTICK: case YP_TOKEN_BANG_EQUAL: \
@@ -10132,7 +10131,7 @@ parse_method_definition_name(yp_parser_t *parser) {
1013210131
parser_lex(parser);
1013310132
return parser->previous;
1013410133
default:
10135-
return not_provided(parser);
10134+
return (yp_token_t) { .type = YP_TOKEN_MISSING, .start = parser->current.start, .end = parser->current.end };
1013610135
}
1013710136
}
1013810137

@@ -12032,7 +12031,7 @@ parse_expression_prefix(yp_parser_t *parser, yp_binding_power_t binding_power) {
1203212031

1203312032
yp_node_t *receiver = NULL;
1203412033
yp_token_t operator = not_provided(parser);
12035-
yp_token_t name = not_provided(parser);
12034+
yp_token_t name = (yp_token_t) { .type = YP_TOKEN_MISSING, .start = def_keyword.end, .end = def_keyword.end };
1203612035

1203712036
context_push(parser, YP_CONTEXT_DEF_PARAMS);
1203812037
parser_lex(parser);
@@ -12056,10 +12055,6 @@ parse_expression_prefix(yp_parser_t *parser, yp_binding_power_t binding_power) {
1205612055

1205712056
operator = parser->previous;
1205812057
name = parse_method_definition_name(parser);
12059-
12060-
if (name.type == YP_TOKEN_MISSING) {
12061-
yp_diagnostic_list_append(&parser->error_list, parser->previous.start, parser->previous.end, YP_ERR_DEF_NAME_AFTER_RECEIVER);
12062-
}
1206312058
} else {
1206412059
yp_parser_scope_push(parser, true);
1206512060
name = parser->previous;
@@ -12126,9 +12121,6 @@ parse_expression_prefix(yp_parser_t *parser, yp_binding_power_t binding_power) {
1212612121
}
1212712122

1212812123
name = parse_method_definition_name(parser);
12129-
if (name.type == YP_TOKEN_MISSING) {
12130-
yp_diagnostic_list_append(&parser->error_list, parser->previous.start, parser->previous.end, YP_ERR_DEF_NAME_AFTER_RECEIVER);
12131-
}
1213212124
} else {
1213312125
name = identifier;
1213412126
}
@@ -12155,13 +12147,15 @@ parse_expression_prefix(yp_parser_t *parser, yp_binding_power_t binding_power) {
1215512147
default:
1215612148
yp_parser_scope_push(parser, true);
1215712149
name = parse_method_definition_name(parser);
12158-
12159-
if (name.type == YP_TOKEN_MISSING) {
12160-
yp_diagnostic_list_append(&parser->error_list, parser->previous.start, parser->previous.end, YP_ERR_DEF_NAME);
12161-
}
1216212150
break;
1216312151
}
1216412152

12153+
// If, after all that, we were unable to find a method name, add an
12154+
// error to the error list.
12155+
if (name.type == YP_TOKEN_MISSING) {
12156+
yp_diagnostic_list_append(&parser->error_list, parser->previous.start, parser->previous.end, YP_ERR_DEF_NAME);
12157+
}
12158+
1216512159
yp_token_t lparen;
1216612160
yp_token_t rparen;
1216712161
yp_parameters_node_t *params;

test/yarp/errors_test.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,14 +283,16 @@ def test_aliasing_global_variable_with_global_number_variable
283283

284284
def test_def_with_expression_receiver_and_no_identifier
285285
assert_errors expression("def (a); end"), "def (a); end", [
286-
["Expected a `.` or `::` after the receiver in a method definition", 7..7]
286+
["Expected a `.` or `::` after the receiver in a method definition", 7..7],
287+
["Expected a method name", 7..7]
287288
]
288289
end
289290

290291
def test_def_with_multiple_statements_receiver
291292
assert_errors expression("def (\na\nb\n).c; end"), "def (\na\nb\n).c; end", [
292293
["Expected a matching `)`", 7..7],
293294
["Expected a `.` or `::` after the receiver in a method definition", 7..7],
295+
["Expected a method name", 7..7],
294296
["Cannot parse the expression", 10..10],
295297
["Cannot parse the expression", 11..11]
296298
]

0 commit comments

Comments
 (0)