Skip to content

Engine: Fix newline handling after heredoc terminators#1246

Merged
marcoroth merged 1 commit intomainfrom
engine-heredocs-newline
Feb 27, 2026
Merged

Engine: Fix newline handling after heredoc terminators#1246
marcoroth merged 1 commit intomainfrom
engine-heredocs-newline

Conversation

@marcoroth
Copy link
Copy Markdown
Owner

@marcoroth marcoroth commented Feb 27, 2026

This pull request fixes newline handling after heredoc terminators in ERB tags. When compiling an ERBContentNode containing a heredoc, the compiled Ruby could end up with syntactically invalid Ruby in two specific cases:

  1. When the tag was at the start of a line, apply_trim appended " \n" (space + newline) as trailing whitespace after the code, which placed a trailing space on the heredoc terminator line. Ruby doesn't recognize a heredoc terminator with trailing whitespace characters.

    For the following snippet (from Engine: Herb builds invalid ruby when HEREDOC is present #1245):

    <%
      text = <<~TEXT
        Hello, world!
      TEXT
    %>

    The engine now produces:

    __herb = ::Herb::Engine; _buf = ::String.new; text = <<~TEXT
        Hello, world!
    -  TEXT 
    +  TEXT
    _buf.to_s
  2. When the tag was not at the start of a line (inline), add_code appended ; instead of \n after the code, placing a semicolon directly after the heredoc terminator and thus altering the heredoc terminator.

    For the following snippet:

    <div><% text = <<~TEXT
        Hello, world!
      TEXT
    %></div>

    The engine now produces:

    __herb = ::Herb::Engine; _buf = ::String.new; _buf << '<div>'.freeze; text = <<~TEXT
        Hello, world!
    -  TEXT; _buf << '</div>
    +  TEXT
    + _buf << '</div>
     '.freeze;
     _buf.to_s

Related #1206
Resolves #1245

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Feb 27, 2026

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

commit: 39ff6a3

@github-actions
Copy link
Copy Markdown

github-actions bot commented Feb 27, 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 39ff6a3


✅ Preview deployment has been cleaned up.

@marcoroth marcoroth removed the parser label Feb 27, 2026
@marcoroth marcoroth merged commit 34e6957 into main Feb 27, 2026
32 checks passed
@marcoroth marcoroth deleted the engine-heredocs-newline branch February 27, 2026 01:17
@marcoroth marcoroth added this to the v1.0.0 milestone Feb 27, 2026
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.

Engine: Herb builds invalid ruby when HEREDOC is present

1 participant