Skip to content

Add markers for conditional attributes and attribute values in ERB#111

Open
tk0miya wants to merge 3 commits into
mainfrom
claude/fix-erb-duplicate-branch-zcmeG
Open

Add markers for conditional attributes and attribute values in ERB#111
tk0miya wants to merge 3 commits into
mainfrom
claude/fix-erb-duplicate-branch-zcmeG

Conversation

@tk0miya

@tk0miya tk0miya commented Jan 28, 2026

Copy link
Copy Markdown
Owner

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/DuplicateBranch cop.

Key Changes

  • New visitor method visit_html_attribute_value_node: Renders markers for LiteralNode children in attribute values that contain ERB expressions
  • Enhanced ERB statement visitors: Added marker rendering for HTMLAttributeNode children in conditional ERB blocks (if/else/when/etc.)
  • New helper methods:
    • render_attribute_value_literals: Marks static text segments in attribute values with ERB
    • render_literal_marker: Places markers at the start of literal nodes (requires ≥3 chars)
    • render_statements_attribute_markers: Marks conditionally rendered attributes
    • erb_child?: Checks if a node contains ERB content
    • render_attribute_marker: Places markers at attribute start positions

Implementation Details

  • Markers use the existing _x; format (alphabetic: _a, _b, _c, etc.) to avoid conflicts with Ruby's numbered parameters
  • Markers are only rendered when html_visualization is enabled
  • Literal markers require at least 3 characters of space to fit the marker syntax
  • The solution handles three scenarios:
    1. Static text differences in attribute values with ERB
    2. Conditionally rendered attributes in ERB statements
    3. Multiple static text segments around ERB in attribute values

Tests Added

Three new test cases verify the marker rendering:

  • Attribute values with different static text in if-else branches
  • Conditional attributes in if-else blocks
  • Attribute values with static text surrounding ERB expressions

https://claude.ai/code/session_019mQDDT3CvsyfXrQBQpev8e

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants