Skip to content

Formatter: Keep herb:disable comments with their own anchor's line - #2785

Closed
markokajzer wants to merge 1 commit into
marcoroth:mainfrom
markokajzer:fix-herb-disable-duplicate-anchors
Closed

markokajzer wants to merge 1 commit into
marcoroth:mainfrom
markokajzer:fix-herb-disable-duplicate-anchors

Conversation

@markokajzer

Copy link
Copy Markdown
Contributor

reproduction

<% foo %> <%# herb:disable erb-no-unused-expressions %>
<% foo %> <%# herb:disable erb-no-unused-expressions %>

before

<% foo %> <%# herb:disable erb-no-unused-expressions %> <%# herb:disable erb-no-unused-expressions %>
<% foo %>

after

unchanged input.

<% foo %> <%# herb:disable erb-no-unused-expressions %>
<% foo %> <%# herb:disable erb-no-unused-expressions %>

cause

findOutputLineForHerbDisable re-attached each collected comment to the first output line containing the anchor's search text (<% foo %>, <div, …), so identical anchors all resolved to the first match. This also put a disable on a second <div> onto the first <div>.

fix

count how often the search text appears in the source before the anchor and pick that same occurrence in the output.

alternatives considered

  • Record each anchor's output line while printing
    • probably the "cleanest" way, but requires a bunch of changes across a few files
    • was not sure if i should go down this route, open to feedback!

@github-actions github-actions Bot added formatter @herb-tools/formatter for HTML+ERB templates typescript TypeScript source across the javascript/ packages labels Oct 7, 2026
@markokajzer markokajzer changed the title Formatter: Splice herb:disable comments back onto their own anchor's line Formatter: Keep herb:disable comments with their own anchor's line Oct 7, 2026
@markokajzer

markokajzer commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor Author

example where a more complex solution might be desired

with the above fix, the following still is not handled correctly

<%=foo%>
<%= foo %> <%# herb:disable some-rule %>

Output:

<%= foo %> <%# herb:disable some-rule %>
<%= foo %>

The fix assumes the search text appears the same number of times in the source and in the output, so it still misplaces the comment when the formatter rewrites an earlier identical ERB tag

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

formatter @herb-tools/formatter for HTML+ERB templates typescript TypeScript source across the javascript/ packages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant