Skip to content

Commit 8a74d85

Browse files
authored
Parser: Fix block detection to exclude closed brace blocks without yield (#1009)
Fixes: #995
1 parent ba1d227 commit 8a74d85

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

src/analyze.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,19 @@ static bool find_earliest_control_keyword_walker(const pm_node_t* node, void* da
192192
case PM_CALL_NODE: {
193193
pm_call_node_t* call = (pm_call_node_t*) node;
194194

195-
if (call->block != NULL) {
196-
current_type = CONTROL_TYPE_BLOCK;
197-
keyword_offset = (uint32_t) (node->location.start - context->source_start);
195+
if (call->block != NULL && call->block->type == PM_BLOCK_NODE) {
196+
pm_block_node_t* block_node = (pm_block_node_t*) call->block;
197+
size_t opening_length = block_node->opening_loc.end - block_node->opening_loc.start;
198+
bool has_do_opening =
199+
opening_length == 2 && block_node->opening_loc.start[0] == 'd' && block_node->opening_loc.start[1] == 'o';
200+
bool has_brace_opening = opening_length == 1 && block_node->opening_loc.start[0] == '{';
201+
bool has_closing_location = block_node->closing_loc.start != NULL && block_node->closing_loc.end != NULL
202+
&& (block_node->closing_loc.end - block_node->closing_loc.start) > 0;
203+
204+
if (has_do_opening || (has_brace_opening && !has_closing_location)) {
205+
current_type = CONTROL_TYPE_BLOCK;
206+
keyword_offset = (uint32_t) (node->location.start - context->source_start);
207+
}
198208
}
199209
break;
200210
}

test/analyze/block_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,5 +194,11 @@ class BlockTest < Minitest::Spec
194194
<% end %>
195195
HTML
196196
end
197+
198+
test "closed brace block with yield" do
199+
assert_parsed_snapshot(<<~HTML)
200+
<% content = capture { yield } if block_given? %>
201+
HTML
202+
end
197203
end
198204
end

test/snapshots/analyze/block_test/test_0023_closed_brace_block_with_yield_5f025404565a9875df5ffa171e8fb7b4.txt

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

0 commit comments

Comments
 (0)