Skip to content

Parser: Introduce VoidElementContentError#1454

Merged
marcoroth merged 1 commit intomainfrom
VoidElementContentError
Mar 22, 2026
Merged

Parser: Introduce VoidElementContentError#1454
marcoroth merged 1 commit intomainfrom
VoidElementContentError

Conversation

@marcoroth
Copy link
Owner

@marcoroth marcoroth commented Mar 22, 2026

This pull request updates the parser to introduce a new VoidElementContentError that gets added to a HTMLElementNode that was transformed from a tag or content_tag call when the tag used is a void element.

For example:

<%= content_tag :img, "/image.png" %>

Now gets parsed as the following with action_view_helpers: true:

@ DocumentNode (location: (1:0)-(1:37))
└── children: (1 item)
    └── @ HTMLElementNode (location: (1:0)-(1:37))
        ├── errors: (1 error)
           └── @ VoidElementContentError (location: (1:0)-(1:37))
               ├── message: "Void element `img` cannot have content. `img` does not accept positional arguments."└── tag_name: "img" (location: (1:4)-(1:15))
        
        ├── open_tag:
           └── @ ERBOpenTagNode (location: (1:0)-(1:37))
               ├── tag_opening: "<%=" (location: (1:0)-(1:3))├── content: " content_tag :img, "/image.png" " (location: (1:3)-(1:35))
               ├── tag_closing: "%>" (location: (1:35)-(1:37))├── tag_name: "img" (location: (1:4)-(1:15))
               └── children: []
        
        ├── tag_name: "img" (location: (1:4)-(1:15))
        ├── body: (1 item)
           └── @ HTMLTextNode (location: (1:0)-(1:37))
               └── content: "/image.png"
        
        ├── close_tag: 
        ├── is_void: true
        └── element_source: "ActionView::Helpers::TagHelper#content_tag"

It also catches:

<%= tag.img "/image.png" %>
@ DocumentNode (location: (1:0)-(1:27))
└── children: (1 item)
    └── @ HTMLElementNode (location: (1:0)-(1:27))
        ├── errors: (1 error)
           └── @ VoidElementContentError (location: (1:0)-(1:27))
               ├── message: "Void element `img` cannot have content. `img` does not accept positional arguments."└── tag_name: "img" (location: (1:8)-(1:11))
        
        ├── open_tag:
           └── @ ERBOpenTagNode (location: (1:0)-(1:27))
               ├── tag_opening: "<%=" (location: (1:0)-(1:3))├── content: " tag.img "/image.png" " (location: (1:3)-(1:25))
               ├── tag_closing: "%>" (location: (1:25)-(1:27))├── tag_name: "img" (location: (1:8)-(1:11))
               └── children: []
        
        ├── tag_name: "img" (location: (1:8)-(1:11))
        ├── body: (1 item)
           └── @ HTMLTextNode (location: (1:0)-(1:27))
               └── content: "/image.png"
        
        ├── close_tag: 
        ├── is_void: true
        └── element_source: "ActionView::Helpers::TagHelper#tag"

@github-actions github-actions bot added the c label Mar 22, 2026
@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 22, 2026

npx https://pkg.pr.new/@herb-tools/formatter@1454
npx https://pkg.pr.new/@herb-tools/language-server@1454
npx https://pkg.pr.new/@herb-tools/linter@1454

commit: d1a6808

@github-actions
Copy link

github-actions bot commented Mar 22, 2026

🌿 Interactive Playground and Documentation Preview

A preview deployment has been built for this pull request. Try out the changes live in the interactive playground:


🌱 Grown from commit d1a6808


✅ Preview deployment has been cleaned up.

@marcoroth marcoroth merged commit f7f94c9 into main Mar 22, 2026
32 checks passed
@marcoroth marcoroth deleted the VoidElementContentError branch March 22, 2026 12:05
@marcoroth marcoroth added this to the v1.0.0 milestone Mar 22, 2026
marcoroth added a commit that referenced this pull request Mar 22, 2026
This pull request implements a new `actionview-no-void-element-content`
based on the new error introduced in #1454.

Because the `parser-no-errors` rule doesn't define `action_view_helpers:
true` it doesn't pick up and report the new `VoidElementContentError`.

So this rule parses the document with `action_view_helpers: true` and
surfaces the `VoidElementContentError` errors that way:

<img width="2227" height="548" alt="CleanShot 2026-03-22 at 13 47 02@2x"
src="https://github.com/user-attachments/assets/ee7d761e-838c-46cb-bccc-0c26f0ebbda0"
/>

Ideally in the future we could infer `action_view_helpers: true` somehow
and pass it along to the `parser-no-errors` rule.

Either by checking if you are running this in a ActionView-enabled
project, or by having a setting in `config.yml` like `framework:
"actionview"` or similar (related #1359).

Additionally, it improves the `VoidElementContentError` error message
and the location reporting.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant