Skip to content
Open
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
7 changes: 7 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ errors:

fields: []

- name: ERBCaseWithConditionsError
message:
template: "A `case` statement with `when`/`in` in a single ERB tag cannot be formatted. Use separate tags for `case` and its conditions."
arguments: []

fields: []

warnings:
fields: []
types: []
Expand Down
8 changes: 8 additions & 0 deletions src/analyze.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ static bool analyze_erb_content(const AST_NODE_T* node, void* data) {
erb_content_node->base.errors
);
}

if (analyzed->inline_conditionals_count > 0) {
append_erb_case_with_conditions_error(
erb_content_node->base.location.start,
erb_content_node->base.location.end,
erb_content_node->base.errors
);
}
} else {
erb_content_node->parsed = false;
erb_content_node->valid = true;
Expand Down
10 changes: 10 additions & 0 deletions src/analyze_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,9 @@ bool search_unclosed_control_flows(const pm_node_t* node, void* data) {

if (has_location(case_node->case_keyword_loc) && !is_end_keyword(case_node->end_keyword_loc)) {
analyzed->unclosed_control_flow_count++;
if (case_node->conditions.size > 0) {
analyzed->inline_conditionals_count++;
}
}

break;
Expand All @@ -416,6 +419,13 @@ bool search_unclosed_control_flows(const pm_node_t* node, void* data) {

if (has_location(case_match_node->case_keyword_loc) && !is_end_keyword(case_match_node->end_keyword_loc)) {
analyzed->unclosed_control_flow_count++;
if (case_match_node->conditions.size > 0) {
analyzed->inline_conditionals_count++;
}

if (case_match_node->predicate && case_match_node->predicate->type == PM_MATCH_PREDICATE_NODE) {
analyzed->inline_conditionals_count++;
}
}

break;
Expand Down
1 change: 1 addition & 0 deletions src/analyzed_ruby.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ analyzed_ruby_T* init_analyzed_ruby(hb_string_T source) {
analyzed->case_match_node_count = 0;
analyzed->when_node_count = 0;
analyzed->in_node_count = 0;
analyzed->inline_conditionals_count = 0;
analyzed->for_node_count = 0;
analyzed->while_node_count = 0;
analyzed->until_node_count = 0;
Expand Down
1 change: 1 addition & 0 deletions src/include/analyzed_ruby.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ typedef struct ANALYZED_RUBY_STRUCT {
int case_match_node_count;
int when_node_count;
int in_node_count;
int inline_conditionals_count;
int for_node_count;
int while_node_count;
int until_node_count;
Expand Down
20 changes: 20 additions & 0 deletions test/parser/multiple_control_flow_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -361,5 +361,25 @@ class MultipleControlFlowTest < Minitest::Spec
%>
ERB
end

test "case and when in same ERB tag" do
assert_parsed_snapshot(<<~ERB)
<% case variable when "a" %>
A
<% when "b" %>
B
<% end %>
ERB
end

test "case in pattern in same ERB tag" do
assert_parsed_snapshot(<<~ERB)
<% case value in 1 %>
One
<% in 2 %>
Two
<% end %>
ERB
end
end
end

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

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