Skip to content

Commit 8a62ffd

Browse files
committed
Parser: Support keyword arguments shorthands for Action View Helper attributes
1 parent ae806a8 commit 8a62ffd

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

src/analyze/action_view/attribute_extraction_helpers.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,30 @@ AST_HTML_ATTRIBUTE_NODE_T* extract_html_attribute_from_assoc(
104104

105105
position_T start_position =
106106
prism_location_to_position_with_offset(&assoc->key->location, original_source, erb_content_offset, source);
107+
108+
if (!assoc->value) {
109+
hb_allocator_dealloc(allocator, name_string);
110+
111+
return NULL;
112+
}
113+
114+
if (assoc->value->type == PM_IMPLICIT_NODE) {
115+
char* dashed_name = convert_underscores_to_dashes(name_string);
116+
117+
AST_HTML_ATTRIBUTE_NODE_T* attribute = create_html_attribute_with_ruby_literal(
118+
dashed_name ? dashed_name : name_string,
119+
name_string,
120+
start_position,
121+
start_position,
122+
allocator
123+
);
124+
125+
if (dashed_name) { free(dashed_name); }
126+
hb_allocator_dealloc(allocator, name_string);
127+
128+
return attribute;
129+
}
130+
107131
position_T end_position =
108132
prism_location_to_position_with_offset(&assoc->value->location, original_source, erb_content_offset, source);
109133

test/analyze/action_view/tag_helper/tag_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,12 @@ class TagTest < Minitest::Spec
246246
HTML
247247
end
248248

249+
test "tag.div with shorthand keyword arguments" do
250+
assert_parsed_snapshot(<<~HTML, action_view_helpers: true)
251+
<%= tag.div(height:, width:) %>
252+
HTML
253+
end
254+
249255
test "tag.meta nested as argument to content_for is not incorrectly detected as top-level tag helper" do
250256
assert_parsed_snapshot(<<~HTML, action_view_helpers: true)
251257
<% content_for :head, tag.meta(name: "viewport", content: "width=device-width, initial-scale=1") %>

test/snapshots/analyze/action_view/tag_helper/tag_test/test_0035_tag.div_with_shorthand_keyword_arguments_3fdb7b4f5bb3c8c34c745dd9200bb83c-ef4af315cb33925c38d24ea3c2e8a1cd.txt

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