Skip to content

Linter: Implement erb-no-module-definitions rule - #2170

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

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

Conversation

@markokajzer

@markokajzer markokajzer commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

closes #197

Rule: erb-no-module-definitions

Description

Disallow Ruby module ... end definitions inside ERB templates.

Rationale

Modules introduce reusable application structure, which belongs in a helper, library, or initializer rather than a view. Defining one while rendering a template can pollute the view context, prevents normal autoloading, and makes the template harder to understand and reuse.

Examples

✅ Good

# app/helpers/display_helpers.rb
module DisplayHelpers
  def highlight(text)
    "<mark>#{text}</mark>".html_safe
  end
end
<%= highlight("Important") %>

🚫 Bad

<%
  module DisplayHelpers
    def highlight(text)
      "<mark>#{text}</mark>".html_safe
    end
  end
%>

<%= highlight("Important") %>
<%
  decorator = Module.new do
    def highlight
      "<mark>#{text}</mark>".html_safe
    end
  end
%>

@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
Comment thread javascript/packages/linter/src/rules/erb-no-module-definitions.ts Outdated
@markokajzer
markokajzer force-pushed the marko/erb-no-module-definitions branch 3 times, most recently from 0ad2adb to 346d83f Compare August 11, 2026 07:10
Comment on lines +26 to +30
module DisplayHelpers
def highlight(text)
"<mark>#{text}</mark>".html_safe
end
end

@marcoroth marcoroth Aug 11, 2026

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.

lets use a different example here. Promoting .html_safe is not great, especially since this could become a problem if highlight gets called with user-controlled data.

Maybe an example with like pluralize could work?

@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.

switched to pluralize for example and specs, and used a more generic error message 🙏

let me know if any other changes are necessary!

i'm currently running against a few repos of https://github.com/steveclarke/real-world-rails, and i'm slowly adding more of them to have a wider base of examples. if you have a good one, feel free to suggest 👀

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.

@markokajzer directly related: https://github.com/marcoroth/herb-corpus

We are running against this corpus on every commit, at least for the parse + printer, but not the linter yet.

@markokajzer
markokajzer force-pushed the marko/erb-no-module-definitions branch 4 times, most recently from 34fd701 to ebd7c70 Compare August 11, 2026 14:51
Comment thread javascript/packages/linter/docs/rules/erb-no-module-definitions.md Outdated
@markokajzer
markokajzer force-pushed the marko/erb-no-module-definitions branch from ebd7c70 to 831cfbe Compare August 11, 2026 15:43

@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.

Sweet, thank you @markokajzer! 🙏🏼

@marcoroth
marcoroth merged commit f7a8308 into marcoroth:main Aug 11, 2026
15 checks passed
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 module definitions in ERB templates

2 participants