Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 2 additions & 30 deletions src/analyze.c
Original file line number Diff line number Diff line change
Expand Up @@ -515,21 +515,7 @@ static size_t process_control_structure(

index++;

while (index < hb_array_size(array)) {
AST_NODE_T* child = hb_array_get(array, index);

if (!child) { break; }

if (child->type == AST_ERB_CONTENT_NODE) {
AST_ERB_CONTENT_NODE_T* child_erb = (AST_ERB_CONTENT_NODE_T*) child;
control_type_t child_type = detect_control_type(child_erb);

if (child_type == CONTROL_TYPE_END) { break; }
}

hb_array_append(else_children, child);
index++;
}
index = process_block_children(node, array, index, else_children, context, initial_type);

hb_array_T* else_errors = next_erb->base.errors;
next_erb->base.errors = NULL;
Expand Down Expand Up @@ -677,21 +663,7 @@ static size_t process_control_structure(

index++;

while (index < hb_array_size(array)) {
AST_NODE_T* child = hb_array_get(array, index);

if (!child) { break; }

if (child->type == AST_ERB_CONTENT_NODE) {
AST_ERB_CONTENT_NODE_T* child_erb = (AST_ERB_CONTENT_NODE_T*) child;
control_type_t child_type = detect_control_type(child_erb);

if (child_type == CONTROL_TYPE_ENSURE || child_type == CONTROL_TYPE_END) { break; }
}

hb_array_append(else_children, child);
index++;
}
index = process_block_children(node, array, index, else_children, context, initial_type);

hb_array_T* else_errors = next_erb->base.errors;
next_erb->base.errors = NULL;
Expand Down
45 changes: 45 additions & 0 deletions test/parser/case_match_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,50 @@ class CaseMatchTest < Minitest::Spec
<% end %>
HTML
end

test "case/in with if inside else branch" do
assert_parsed_snapshot(<<~HTML)
<% case value %>
<% in Integer %>
<p>Integer</p>
<% else %>
<% if unknown? %>
<p>Unknown type</p>
<% end %>
<% end %>
HTML
end

test "case/in with nested case/in inside else branch" do
assert_parsed_snapshot(<<~HTML)
<% case data %>
<% in { type: "primary" } %>
<p>Primary</p>
<% else %>
<% case fallback %>
<% in { status: "ok" } %>
<p>OK</p>
<% in { status: "error" } %>
<p>Error</p>
<% end %>
<% end %>
HTML
end

test "case/in with multiple nested structures in else branch" do
assert_parsed_snapshot(<<~HTML)
<% case result %>
<% in { success: true } %>
<p>Success</p>
<% else %>
<% if retry? %>
<p>Retrying</p>
<% end %>
<% unless failed? %>
<p>Still processing</p>
<% end %>
<% end %>
HTML
end
end
end
90 changes: 90 additions & 0 deletions test/parser/case_when_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,95 @@ class CaseWhenTest < Minitest::Spec
<% end %>
HTML
end

test "case/when with if inside else branch (issue 860)" do
assert_parsed_snapshot(<<~HTML)
<% case %>
<% when true %>
1
<% else %>
<% if true %>
2
<% end %>
<% end %>
HTML
end

test "case/when with if/elsif/else inside else branch" do
assert_parsed_snapshot(<<~HTML)
<% case status %>
<% when :active %>
<p>Active</p>
<% else %>
<% if admin? %>
<p>Admin view</p>
<% elsif moderator? %>
<p>Moderator view</p>
<% else %>
<p>User view</p>
<% end %>
<% end %>
HTML
end

test "case/when with unless inside else branch" do
assert_parsed_snapshot(<<~HTML)
<% case role %>
<% when :admin %>
<p>Admin</p>
<% else %>
<% unless guest? %>
<p>Authenticated</p>
<% end %>
<% end %>
HTML
end

test "case/when with nested case inside else branch" do
assert_parsed_snapshot(<<~HTML)
<% case type %>
<% when :primary %>
<p>Primary</p>
<% else %>
<% case subtype %>
<% when :secondary %>
<p>Secondary</p>
<% when :tertiary %>
<p>Tertiary</p>
<% end %>
<% end %>
HTML
end

test "case/when with multiple nested control structures in else branch" do
assert_parsed_snapshot(<<~HTML)
<% case status %>
<% when :ok %>
<p>OK</p>
<% else %>
<% if error? %>
<p>Error</p>
<% end %>
<% unless success? %>
<p>Not successful</p>
<% end %>
<% end %>
HTML
end

test "case/when with begin/rescue inside else branch" do
assert_parsed_snapshot(<<~HTML)
<% case result %>
<% when :success %>
<p>Success</p>
<% else %>
<% begin %>
<p>Attempting fallback</p>
<% rescue %>
<p>Fallback failed</p>
<% end %>
<% end %>
HTML
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading