Skip to content

Commit d08e140

Browse files
committed
Change the location of an implicit begin to method
1 parent ed337ad commit d08e140

21 files changed

+47
-62
lines changed

src/prism.c

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12377,25 +12377,10 @@ parse_rescues(pm_parser_t *parser, pm_begin_node_t *parent_node, bool def_p) {
1237712377
}
1237812378

1237912379
static inline pm_begin_node_t *
12380-
parse_rescues_as_begin(pm_parser_t *parser, pm_statements_node_t *statements, bool def_p) {
12380+
parse_rescues_as_begin(pm_parser_t *parser, const uint8_t *start, pm_statements_node_t *statements, bool def_p) {
1238112381
pm_token_t no_begin_token = not_provided(parser);
1238212382
pm_begin_node_t *begin_node = pm_begin_node_create(parser, &no_begin_token, statements);
1238312383
parse_rescues(parser, begin_node, def_p);
12384-
12385-
// All nodes within a begin node are optional, so we look
12386-
// for the earliest possible node that we can use to set
12387-
// the BeginNode's start location
12388-
const uint8_t *start = begin_node->base.location.start;
12389-
if (begin_node->statements) {
12390-
start = begin_node->statements->base.location.start;
12391-
} else if (begin_node->rescue_clause) {
12392-
start = begin_node->rescue_clause->base.location.start;
12393-
} else if (begin_node->else_clause) {
12394-
start = begin_node->else_clause->base.location.start;
12395-
} else if (begin_node->ensure_clause) {
12396-
start = begin_node->ensure_clause->base.location.start;
12397-
}
12398-
1239912384
begin_node->base.location.start = start;
1240012385
return begin_node;
1240112386
}
@@ -12490,7 +12475,7 @@ parse_block(pm_parser_t *parser) {
1249012475

1249112476
if (match2(parser, PM_TOKEN_KEYWORD_RESCUE, PM_TOKEN_KEYWORD_ENSURE)) {
1249212477
assert(statements == NULL || PM_NODE_TYPE_P(statements, PM_STATEMENTS_NODE));
12493-
statements = (pm_node_t *) parse_rescues_as_begin(parser, (pm_statements_node_t *) statements, false);
12478+
statements = (pm_node_t *) parse_rescues_as_begin(parser, opening.start, (pm_statements_node_t *) statements, false);
1249412479
}
1249512480
}
1249612481

@@ -15290,7 +15275,7 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1529015275

1529115276
if (match2(parser, PM_TOKEN_KEYWORD_RESCUE, PM_TOKEN_KEYWORD_ENSURE)) {
1529215277
assert(statements == NULL || PM_NODE_TYPE_P(statements, PM_STATEMENTS_NODE));
15293-
statements = (pm_node_t *) parse_rescues_as_begin(parser, (pm_statements_node_t *) statements, false);
15278+
statements = (pm_node_t *) parse_rescues_as_begin(parser, class_keyword.start, (pm_statements_node_t *) statements, false);
1529415279
}
1529515280

1529615281
expect1(parser, PM_TOKEN_KEYWORD_END, PM_ERR_CLASS_TERM);
@@ -15343,7 +15328,7 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1534315328

1534415329
if (match2(parser, PM_TOKEN_KEYWORD_RESCUE, PM_TOKEN_KEYWORD_ENSURE)) {
1534515330
assert(statements == NULL || PM_NODE_TYPE_P(statements, PM_STATEMENTS_NODE));
15346-
statements = (pm_node_t *) parse_rescues_as_begin(parser, (pm_statements_node_t *) statements, false);
15331+
statements = (pm_node_t *) parse_rescues_as_begin(parser, class_keyword.start, (pm_statements_node_t *) statements, false);
1534715332
}
1534815333

1534915334
expect1(parser, PM_TOKEN_KEYWORD_END, PM_ERR_CLASS_TERM);
@@ -15612,7 +15597,7 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1561215597

1561315598
if (match2(parser, PM_TOKEN_KEYWORD_RESCUE, PM_TOKEN_KEYWORD_ENSURE)) {
1561415599
assert(statements == NULL || PM_NODE_TYPE_P(statements, PM_STATEMENTS_NODE));
15615-
statements = (pm_node_t *) parse_rescues_as_begin(parser, (pm_statements_node_t *) statements, true);
15600+
statements = (pm_node_t *) parse_rescues_as_begin(parser, def_keyword.start, (pm_statements_node_t *) statements, true);
1561615601
}
1561715602

1561815603
pm_accepts_block_stack_pop(parser);
@@ -15872,7 +15857,7 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1587215857

1587315858
if (match2(parser, PM_TOKEN_KEYWORD_RESCUE, PM_TOKEN_KEYWORD_ENSURE)) {
1587415859
assert(statements == NULL || PM_NODE_TYPE_P(statements, PM_STATEMENTS_NODE));
15875-
statements = (pm_node_t *) parse_rescues_as_begin(parser, (pm_statements_node_t *) statements, false);
15860+
statements = (pm_node_t *) parse_rescues_as_begin(parser, module_keyword.start, (pm_statements_node_t *) statements, false);
1587615861
}
1587715862

1587815863
pm_constant_id_list_t locals = parser->current_scope->locals;
@@ -16605,7 +16590,7 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1660516590

1660616591
if (match2(parser, PM_TOKEN_KEYWORD_RESCUE, PM_TOKEN_KEYWORD_ENSURE)) {
1660716592
assert(body == NULL || PM_NODE_TYPE_P(body, PM_STATEMENTS_NODE));
16608-
body = (pm_node_t *) parse_rescues_as_begin(parser, (pm_statements_node_t *) body, false);
16593+
body = (pm_node_t *) parse_rescues_as_begin(parser, opening.start, (pm_statements_node_t *) body, false);
1660916594
}
1661016595

1661116596
expect1(parser, PM_TOKEN_KEYWORD_END, PM_ERR_LAMBDA_TERM_END);

test/prism/location_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ def test_BeginNode
6363
assert_location(BeginNode, "begin foo; rescue bar\nelse baz end")
6464
assert_location(BeginNode, "begin foo; rescue bar\nelse baz\nensure qux end")
6565

66-
assert_location(BeginNode, "class Foo\nrescue then end", 10..25, &:body)
67-
assert_location(BeginNode, "module Foo\nrescue then end", 11..26, &:body)
66+
assert_location(BeginNode, "class Foo\nrescue then end", 0..25, &:body)
67+
assert_location(BeginNode, "module Foo\nrescue then end", 0..26, &:body)
6868
end
6969

7070
def test_BlockArgumentNode

test/prism/snapshots/arrays.txt

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/prism/snapshots/blocks.txt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/prism/snapshots/classes.txt

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/prism/snapshots/methods.txt

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/prism/snapshots/modules.txt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/prism/snapshots/procs.txt

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/prism/snapshots/rescue.txt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/prism/snapshots/seattlerb/defn_oneliner_rescue.txt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)