Skip to content

Commit

Permalink
Merge pull request #1559 from haldun/haldun/check-class-name
Browse files Browse the repository at this point in the history
Check class name to be a constant path node or a constant read node
  • Loading branch information
kddnewton authored Sep 22, 2023
2 parents 89b59b0 + fd7c44f commit 0b03079
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/yarp.c
Original file line number Diff line number Diff line change
Expand Up @@ -12304,6 +12304,11 @@ parse_expression_prefix(yp_parser_t *parser, yp_binding_power_t binding_power) {
yp_constant_id_list_t locals = parser->current_scope->locals;
yp_parser_scope_pop(parser);
yp_do_loop_stack_pop(parser);

if (!YP_NODE_TYPE_P(constant_path, YP_CONSTANT_PATH_NODE) && !(YP_NODE_TYPE_P(constant_path, YP_CONSTANT_READ_NODE))) {
yp_diagnostic_list_append(&parser->error_list, constant_path->location.start, constant_path->location.end, YP_ERR_CLASS_NAME);
}

return (yp_node_t *) yp_class_node_create(parser, &locals, &class_keyword, constant_path, &name, &inheritance_operator, superclass, statements, &parser->previous);
}
case YP_TOKEN_KEYWORD_DEF: {
Expand Down
7 changes: 7 additions & 0 deletions test/yarp/errors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,13 @@ def test_parameter_name_ending_with_bang_or_question_mark
assert_errors expression(source), source, errors, compare_ripper: false
end

def test_class_name
source = "class 0.X end"
assert_errors expression(source), source, [
["Expected a constant name after `class`", 6..9],
]
end

private

def assert_errors(expected, source, errors, compare_ripper: RUBY_ENGINE == "ruby")
Expand Down

0 comments on commit 0b03079

Please sign in to comment.