Skip to content

Linter: Implement erb-no-method-definitions rule - #2172

Merged
marcoroth merged 1 commit into
marcoroth:mainfrom
markokajzer:marko/erb-no-method-definitions
Aug 11, 2026
Merged

Linter: Implement erb-no-method-definitions rule#2172
marcoroth merged 1 commit into
marcoroth:mainfrom
markokajzer:marko/erb-no-method-definitions

Conversation

@markokajzer

@markokajzer markokajzer commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

closes #195

Rule: erb-no-method-definitions

Description

Disallow defining Ruby methods inside ERB files using def ... end or define_method. View templates should only contain logic necessary for rendering the page. Defining methods in ERB mixes concerns and breaks MVC conventions.

Rationale

Defining methods in a view template:

  • Pollutes the view context
  • Makes templates harder to read and debug
  • Introduces global-like behavior into views
  • Encourages non-reusable, non-testable logic
  • Violates Rails' separation of concerns

Method definitions belong in helpers, presenters, or view components, not in view files.

Examples

✅ Good

# app/helpers/application_helper.rb

module ApplicationHelper
  def format_date(date)
    date.strftime("%B %d")
  end

  def admin?
    current_user.admin?
  end
end

🚫 Bad

<% def format_date(date) %>
  <%= date.strftime("%B %d") %>
<% end %>

<p><%= format_date(Date.today) %></p>
<% def admin? = current_user.admin? %>
<% define_method(:admin?) { current_user.admin? } %>

@github-actions github-actions Bot added documentation Improvements or additions to documentation linter @herb-tools/linter for HTML+ERB templates typescript TypeScript source across the javascript/ packages linter-rule Individual linter rules and their documentation labels Aug 11, 2026
@markokajzer
markokajzer force-pushed the marko/erb-no-method-definitions branch 4 times, most recently from 0298b09 to a6e7b7f Compare August 11, 2026 07:09
`)
})

test("ignores lambdas", () => {

@markokajzer markokajzer Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if we want to catch these

issue description was somewhat specific regarding def ... end blocks, but knowing ruby there are a bunch of other ways to define "methods"

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine to leave them for now. If'd want to catch them we could still introduce a new rule specifically for lambdas.

@markokajzer
markokajzer force-pushed the marko/erb-no-method-definitions branch 2 times, most recently from 4260a9f to 87e8417 Compare August 11, 2026 15:37
@markokajzer
markokajzer force-pushed the marko/erb-no-method-definitions branch from 87e8417 to 76ff309 Compare August 11, 2026 16:01

@marcoroth marcoroth left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @markokajzer! 🙏🏼

To only thing to note here is that we can't catch issues like #990 because they are just straight up a parser error.

Though, we might be able to set consumesParserErrors = true on the rule itself, and then, since we can look at the extracted Ruby from that file, detect the method definition that way since then its all valid Ruby again 🤔

@marcoroth
marcoroth merged commit 7e04d93 into marcoroth:main Aug 11, 2026
15 checks passed
@markokajzer

Copy link
Copy Markdown
Contributor Author

i didnt even know that was possible haha

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

Labels

documentation Improvements or additions to documentation linter @herb-tools/linter for HTML+ERB templates linter-rule Individual linter rules and their documentation typescript TypeScript source across the javascript/ packages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Linter Rule: Disallow method definitions inside ERB templates

2 participants