Skip to content

Autofix corrupts multi-line <% ... %> blocks by stripping the opening tag #461

Description

@darronschall

I'm seeing an issue where Layout/LeadingEmptyLines autocorrect deletes the <% delimiter and breaks the template, using: erb_lint 0.9.0, rubocop 1.84.2, ruby 4.0.2

Save this five-line file as bug.html.erb:

<%
  bar = 1
  baz = 2
%>
<p><%= bar + baz %></p>

(e.g. printf '<%%\n bar = 1\n baz = 2\n%%>\n<p><%%= bar + baz %%></p>\n' > bug.html.erb)

Run bundle exec erb_lint bug.html.erb (with standard-erb-lint / the default Rubocop-backed ruleset, in particular Layout/LeadingEmptyLines enabled as autocorrectable).

It reports:

Layout/LeadingEmptyLines: Unnecessary blank line at the beginning of the source.
In file: bug.html.erb:2

Layout/TrailingWhitespace: Trailing whitespace detected.
In file: bug.html.erb:1

2 error(s) were found in ERB files

After autofixing (bundle exec erb_lint -a bug.html.erb) the now-4-line file reads:

bar = 1
  baz = 2
%>
<p><%= bar + baz %></p>

The opening <% delimiter has been removed. bar = 1 and baz = 2 are now literal HTML text, the closing %> is orphaned, and the template raises undefined local variable or method 'bar' at render time.

Expected: the autofix should leave multi-line <% ... %>blocks alone when the "leading empty line" it detects is the newline immediately after the opening <% delimiter.

Workaround: keep Ruby inside <% %> on a single line per statement so RuboCop's Layout cops have nothing to rewrite across the ERB boundary.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions