Skip to content

Commit 425292a

Browse files
committed
Parser: Treat javascript_tag content as foreign content
1 parent a25dec0 commit 425292a

6 files changed

+101
-1
lines changed

‎src/analyze/action_view/tag_helpers.c‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ static AST_NODE_T* transform_erb_block_to_tag_helper(
687687
AST_NODE_T* close_tag = (AST_NODE_T*) block_node->end_node;
688688

689689
if (tag_name && parser_is_foreign_content_tag(hb_string_from_c_string(tag_name)) && context->source
690-
&& block_node->body && hb_array_size(block_node->body) > 0) {
690+
&& block_node->end_node != NULL) {
691691
size_t start_offset = block_node->tag_closing->range.to;
692692
size_t end_offset = block_node->end_node->tag_opening->range.from;
693693

@@ -959,6 +959,18 @@ void transform_tag_helper_blocks(const AST_NODE_T* node, analyze_ruby_context_T*
959959
case AST_HTML_OPEN_TAG_NODE: array = ((AST_HTML_OPEN_TAG_NODE_T*) node)->children; break;
960960
case AST_HTML_ATTRIBUTE_VALUE_NODE: array = ((AST_HTML_ATTRIBUTE_VALUE_NODE_T*) node)->children; break;
961961
case AST_ERB_BLOCK_NODE: array = ((AST_ERB_BLOCK_NODE_T*) node)->body; break;
962+
case AST_ERB_IF_NODE: array = ((AST_ERB_IF_NODE_T*) node)->statements; break;
963+
case AST_ERB_UNLESS_NODE: array = ((AST_ERB_UNLESS_NODE_T*) node)->statements; break;
964+
case AST_ERB_ELSE_NODE: array = ((AST_ERB_ELSE_NODE_T*) node)->statements; break;
965+
case AST_ERB_WHILE_NODE: array = ((AST_ERB_WHILE_NODE_T*) node)->statements; break;
966+
case AST_ERB_UNTIL_NODE: array = ((AST_ERB_UNTIL_NODE_T*) node)->statements; break;
967+
case AST_ERB_FOR_NODE: array = ((AST_ERB_FOR_NODE_T*) node)->statements; break;
968+
case AST_ERB_BEGIN_NODE: array = ((AST_ERB_BEGIN_NODE_T*) node)->statements; break;
969+
case AST_ERB_RESCUE_NODE: array = ((AST_ERB_RESCUE_NODE_T*) node)->statements; break;
970+
case AST_ERB_ENSURE_NODE: array = ((AST_ERB_ENSURE_NODE_T*) node)->statements; break;
971+
case AST_ERB_WHEN_NODE: array = ((AST_ERB_WHEN_NODE_T*) node)->statements; break;
972+
case AST_ERB_CASE_NODE: array = ((AST_ERB_CASE_NODE_T*) node)->children; break;
973+
case AST_ERB_CASE_MATCH_NODE: array = ((AST_ERB_CASE_MATCH_NODE_T*) node)->children; break;
962974
default: return;
963975
}
964976

‎test/analyze/action_view/javascript_helper/javascript_tag_test.rb‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,23 @@ class JavaScriptTagTest < Minitest::Spec
5555
<% end %>
5656
HTML
5757
end
58+
59+
test "javascript_tag with HTML-like content in block (gh-1426)" do
60+
assert_parsed_snapshot(<<~HTML, action_view_helpers: true)
61+
<%= javascript_tag do %>
62+
n <o.length
63+
<% end %>
64+
HTML
65+
end
66+
67+
test "javascript_tag with HTML-like content in block (gh-1426)" do
68+
assert_parsed_snapshot(<<~HTML, action_view_helpers: true)
69+
<%= javascript_tag do %>
70+
const html = "<code><%= content %></code>";
71+
72+
console.log(html)
73+
<% end %>
74+
HTML
75+
end
5876
end
5977
end

‎test/parser/tags_test.rb‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,5 +305,13 @@ class TagsTest < Minitest::Spec
305305
test "case-insensitive style tag" do
306306
assert_parsed_snapshot(%(<STYLE>.class { color: red; }</style>))
307307
end
308+
309+
test "less-than operator with dot method access is parsed as text not a tag (gh-1426)" do
310+
assert_parsed_snapshot("n <o.length")
311+
end
312+
313+
test "less-than operator followed by lowercase identifier and dot is parsed as text" do
314+
assert_parsed_snapshot("<foo.bar")
315+
end
308316
end
309317
end

‎test/snapshots/analyze/action_view/java_script_helper/java_script_tag_test/test_0009_javascript_tag_with_HTML-like_content_in_block_(gh-1426)_d2bd29d9ea354dbf58a894bd2a7bd725-ef4af315cb33925c38d24ea3c2e8a1cd.txt‎

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

‎test/snapshots/parser/tags_test/test_0055_less-than_operator_with_dot_method_access_is_parsed_as_text_not_a_tag_(gh-1426)_1348f72d270454c0c4e6035084d4694e.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.

‎test/snapshots/parser/tags_test/test_0056_less-than_operator_followed_by_lowercase_identifier_and_dot_is_parsed_as_text_01d9704e7c4382b0a62560ae3383b7d1.txt‎

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