Skip to content

Linter Rule: Enforce trailing newline

Rule: erb-require-trailing-newline

Description

This rule enforces that all HTML+ERB template files end with exactly one trailing newline character. This is a formatting convention widely adopted across many languages and tools.

Rationale

Ensuring HTML+ERB files end with a single trailing newline aligns with POSIX conventions, where text files should end with a newline character.

This practice avoids unnecessary diffs from editors or formatters that auto-insert final newlines, improving compatibility with command-line tools and version control systems. It also helps maintain a clean, predictable structure across view files.

Trailing newlines are a common convention in Ruby and are enforced by tools like RuboCop and many Git-based workflows.

Examples

✅ Good

<%= render partial: "header" %>
<%= render partial: "footer" %>

🚫 Bad

erb
<%= render partial: "header" %>
The partial `header` is looked up relative to the directory of the template rendering it. Moving this template changes which file that resolves to, and renaming the partial means hunting for callers that never spell its full name. Write the full path from the view root so it resolves to the same file from any template, and a search for that path finds every caller. (actionview-prefer-qualified-partial-path)
<%= render partial: "footer" %>
File must end with trailing newline. (erb-require-trailing-newline)
The partial `footer` is looked up relative to the directory of the template rendering it. Moving this template changes which file that resolves to, and renaming the partial means hunting for callers that never spell its full name. Write the full path from the view root so it resolves to the same file from any template, and a search for that path finds every caller. (actionview-prefer-qualified-partial-path)

References

Released under the MIT License.