Add markers for conditional attributes and attribute values in ERB#111
Open
tk0miya wants to merge 3 commits into
Open
Add markers for conditional attributes and attribute values in ERB#111tk0miya wants to merge 3 commits into
tk0miya wants to merge 3 commits into
Conversation
Add markers for static HTML content within HTML open tags to distinguish branches when ERB conditionals are used. This prevents RuboCop from incorrectly flagging branches as duplicates when only the HTML content differs. Two cases are now handled: 1. LiteralNode in HTMLAttributeValueNode - Static text mixed with ERB in attribute values (e.g., class="<%= x %> world") 2. HTMLAttributeNode in ERB statements - Conditional attributes (e.g., <div <% if cond %>class="a"<% else %>class="b"<% end %>>) https://claude.ai/code/session_019mQDDT3CvsyfXrQBQpev8e
Add tag recording in NodeLocationCollector for: - LiteralNode in HTMLAttributeValueNode with ERB - HTMLAttributeNode in ERB control structure statements This ensures hybrid_code displays the original HTML content instead of the marker placeholders (_b;, _c;), making diagnostics more readable. https://claude.ai/code/session_019mQDDT3CvsyfXrQBQpev8e
Replace manual child traversal (node.statements.each, node.children.each) with proper Visitor pattern using @open_tag_has_erb flag. The flag is set when any ERB node is encountered inside an HTMLOpenTagNode, ensuring markers are only added for open tags that actually contain ERB — not for static open tags merely wrapped by outer ERB control structures. Key changes: - Replace visit_html_attribute_value_node with visit_html_attribute_node and visit_literal_node for finer-grained marker control - Add visit_html_open_tag_node to both collectors to track open tag scope - Use @open_tag_has_erb instead of @erb_depth for marker conditions - Remove manual traversal helpers (erb_child?, record_statements_attribute_tags, render_statements_attribute_markers, etc.) https://claude.ai/code/session_019mQDDT3CvsyfXrQBQpev8e
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds marker rendering for HTML attributes and attribute values that contain ERB expressions. These markers help distinguish different branches in conditional statements, preventing false positives from RuboCop's
Lint/DuplicateBranchcop.Key Changes
visit_html_attribute_value_node: Renders markers forLiteralNodechildren in attribute values that contain ERB expressionsHTMLAttributeNodechildren in conditional ERB blocks (if/else/when/etc.)render_attribute_value_literals: Marks static text segments in attribute values with ERBrender_literal_marker: Places markers at the start of literal nodes (requires ≥3 chars)render_statements_attribute_markers: Marks conditionally rendered attributeserb_child?: Checks if a node contains ERB contentrender_attribute_marker: Places markers at attribute start positionsImplementation Details
_x;format (alphabetic: _a, _b, _c, etc.) to avoid conflicts with Ruby's numbered parametershtml_visualizationis enabledTests Added
Three new test cases verify the marker rendering:
https://claude.ai/code/session_019mQDDT3CvsyfXrQBQpev8e