Skip to content

Linter: Add erb-prefer-pluralize-helper rule#1868

Open
joaoGabriel55 wants to merge 2 commits into
marcoroth:mainfrom
joaoGabriel55:add-Linter-Rule-TextHelper-pluralize-over-String-pluralize
Open

Linter: Add erb-prefer-pluralize-helper rule#1868
joaoGabriel55 wants to merge 2 commits into
marcoroth:mainfrom
joaoGabriel55:add-Linter-Rule-TextHelper-pluralize-over-String-pluralize

Conversation

@joaoGabriel55

@joaoGabriel55 joaoGabriel55 commented Jul 22, 2026

Copy link
Copy Markdown

Overview

Adds a new linter rule, erb-prefer-pluralize-helper, that flags calls to String#pluralize with a count argument in ERB templates and recommends the ActionView::Helpers::TextHelper#pluralize helper instead.

The helper form (pluralize("Alias", count)) prepends the count to the output and reads naturally in a template, whereas "Alias".pluralize(count) typically forces the count to be rendered separately — leading to duplicated output like <%= aliases.size %> <%= "Alias".pluralize(aliases.size) %>.

<!-- 🚫 Bad -->
<%= aliases.size %> Known <%= "Alias".pluralize(aliases.size) %>

<!-- ✅ Good -->
Known <%= pluralize("Alias", aliases.size) %>

The offense message includes a concrete, source-derived suggestion, e.g.:

Prefer the pluralize helper over String#pluralize for counts. Use <%= pluralize("Alias", aliases.size) %> instead.

Related Issue

Closes #1723

Affected Component(s)

  • 🔎 Linter
  • 📚 Documentation

Type of Change

  • ✨ New feature (non-breaking change that adds functionality)

Main Technical Changes

  • New rule erb-prefer-pluralize-helper (javascript/packages/linter/src/rules/erb-prefer-pluralize-helper.ts), modeled on the existing Prism-based erb-no-debug-output rule:
    • Uses parserOptions: { prism_program: true } and a PrismVisitor to walk CallNodes.
    • Flags a call when the method name is pluralize, the receiver is a string literal (StringNode / InterpolatedStringNode), and it has at least one argument (the count). This scoping avoids false positives on non-string receivers (e.g. model.pluralize(count)) and on the argument-less "Alias".pluralize, matching the "for counts" intent of the issue.
    • Default severity: warning; introducedIn: "unreleased".
    • The suggested fix is reconstructed from the original source spans of the receiver and arguments via a small slice() helper.
  • Registered the rule in src/rules.ts and re-exported it from src/rules/index.ts.
  • Added docs page docs/rules/erb-prefer-pluralize-helper.md and linked it from docs/rules/README.md (required by the rule-docs completeness test).

How to Test

cd javascript/packages/linter
npx vitest run test/rules/erb-prefer-pluralize-helper.test.ts   # new rule tests (9)
npx vitest run test/rule-docs.test.ts                           # docs completeness
npx tsc -b                                                      # typecheck

The rule test covers valid cases (helper form, argument-less String#pluralize, non-string receivers, unrelated string methods) and invalid cases (string-literal pluralize with a count in both <%= %> and <% %> tags).

Checklist

  • 🧪 Tests added or updated
  • 🔨 Ran bin/integration locally (ran the linter package test suite: 2438 passed)
  • 📚 Documentation updated (if applicable)
  • 🧭 Commit messages follow the Component: Description convention (Linter: Add \erb-prefer-pluralize-helper` rule`)

@github-actions github-actions Bot added documentation Improvements or additions to documentation linter typescript linter-rule labels Jul 22, 2026
Comment thread javascript/packages/linter/docs/rules/erb-prefer-pluralize-helper.md Outdated
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 linter-rule typescript

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Linter Rule: Prefer ActionView::Helpers::TextHelper#pluralize over String#pluralize for counts

2 participants