Skip to content

Commit 3fca26a

Browse files
authored
Parser: Improve ERB case node and end tag matching (#1237)
This pull request improves the parser by properly matching `<% end %>` tags to `ERBCaseNode` when no `when`/`in` clauses are present. For the following example: ```html+erb <% case 1 %> <% end %> ``` The reported errors are now reduced to 1, down from 3. **Before** ``` Errors (3 total) 1. RubyParseError at test.html.erb:1:3 case_missing_conditions: expected a `when` or `in` clause after `case` 2. MissingERBEndTagError at test.html.erb:1:0 `<% case %>` started here but never closed with an end tag. The end tag may be in a different scope. 3. ERBControlFlowScopeError at test.html.erb:2:0 `<% end %>` appears outside its control flow block. Keep ERB control flow statements together within the same HTML scope (tag, attribute, or content). ``` **After** ``` Errors (1 total) 1. RubyParseError at test.html.erb:1:3 case_missing_conditions: expected a `when` or `in` clause after `case` ``` Inspired by ruby-gettext/gettext#117 /cc @joelhawksley
1 parent dc6cf82 commit 3fca26a

4 files changed

Lines changed: 117 additions & 1 deletion

File tree

src/analyze.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ static size_t process_control_structure(
656656
AST_ERB_CONTENT_NODE_T* erb_content = (AST_ERB_CONTENT_NODE_T*) next_node;
657657
control_type_t next_type = detect_control_type(erb_content);
658658

659-
if (next_type == CONTROL_TYPE_WHEN || next_type == CONTROL_TYPE_IN) { break; }
659+
if (next_type == CONTROL_TYPE_WHEN || next_type == CONTROL_TYPE_IN || next_type == CONTROL_TYPE_END) { break; }
660660
}
661661

662662
hb_array_append(non_when_non_in_children, next_node);

test/parser/case_when_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,21 @@ class CaseWhenTest < Minitest::Spec
109109
<% end %>
110110
HTML
111111
end
112+
113+
test "case without when" do
114+
assert_parsed_snapshot(<<~HTML)
115+
<% case 1 %>
116+
<% end %>
117+
HTML
118+
end
119+
120+
test "case with single when" do
121+
assert_parsed_snapshot(<<~HTML)
122+
<% case 1 %>
123+
<% when 1 %>
124+
<p>one</p>
125+
<% end %>
126+
HTML
127+
end
112128
end
113129
end

test/snapshots/parser/case_when_test/test_0008_case_without_when_4ec7986dc142768053964a35aca8fae4.txt

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

test/snapshots/parser/case_when_test/test_0009_case_with_single_when_9559f5a632c2f6780a6287b2b758f8c7.txt

Lines changed: 66 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)