Skip to content

Commit

Permalink
Merge pull request #3444 from ruby/no-empty-statements-in-while
Browse files Browse the repository at this point in the history
Do not put empty statements in while because of -n
  • Loading branch information
kddnewton authored Jan 22, 2025
2 parents 28dc4ff + ebb9c36 commit 0d0f26c
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -22190,6 +22190,10 @@ parse_expression(pm_parser_t *parser, pm_binding_power_t binding_power, bool acc
static pm_statements_node_t *
wrap_statements(pm_parser_t *parser, pm_statements_node_t *statements) {
if (PM_PARSER_COMMAND_LINE_OPTION_P(parser)) {
if (statements == NULL) {
statements = pm_statements_node_create(parser);
}

pm_arguments_node_t *arguments = pm_arguments_node_create(parser);
pm_arguments_node_arguments_append(
arguments,
Expand All @@ -22205,6 +22209,10 @@ wrap_statements(pm_parser_t *parser, pm_statements_node_t *statements) {

if (PM_PARSER_COMMAND_LINE_OPTION_N(parser)) {
if (PM_PARSER_COMMAND_LINE_OPTION_A(parser)) {
if (statements == NULL) {
statements = pm_statements_node_create(parser);
}

pm_arguments_node_t *arguments = pm_arguments_node_create(parser);
pm_arguments_node_arguments_append(
arguments,
Expand Down Expand Up @@ -22273,9 +22281,7 @@ parse_program(pm_parser_t *parser) {
parser_lex(parser);
pm_statements_node_t *statements = parse_statements(parser, PM_CONTEXT_MAIN, 0);

if (statements == NULL) {
statements = pm_statements_node_create(parser);
} else if (!parser->parsing_eval) {
if (statements != NULL && !parser->parsing_eval) {
// If we have statements, then the top-level statement should be
// explicitly checked as well. We have to do this here because
// everywhere else we check all but the last statement.
Expand All @@ -22287,13 +22293,6 @@ parse_program(pm_parser_t *parser) {
pm_locals_order(parser, &parser->current_scope->locals, &locals, true);
pm_parser_scope_pop(parser);

// If this is an empty file, then we're still going to parse all of the
// statements in order to gather up all of the comments and such. Here we'll
// correct the location information.
if (pm_statements_node_body_length(statements) == 0) {
pm_statements_node_location_set(statements, parser->start, parser->start);
}

// At the top level, see if we need to wrap the statements in a program
// node with a while loop based on the options.
if (parser->command_line & (PM_OPTIONS_COMMAND_LINE_P | PM_OPTIONS_COMMAND_LINE_N)) {
Expand All @@ -22303,6 +22302,14 @@ parse_program(pm_parser_t *parser) {
pm_node_list_free(&current_block_exits);
}

// If this is an empty file, then we're still going to parse all of the
// statements in order to gather up all of the comments and such. Here we'll
// correct the location information.
if (statements == NULL) {
statements = pm_statements_node_create(parser);
pm_statements_node_location_set(statements, parser->start, parser->start);
}

return (pm_node_t *) pm_program_node_create(parser, &locals, statements);
}

Expand Down

0 comments on commit 0d0f26c

Please sign in to comment.