Ensures that strict locals comments use the exact locals: ( ... ) syntax so they are properly recognized by Rails and tooling. Also validates that only keyword arguments are used (no positional, block, or splat arguments).
Strict locals comments declare which locals are expected in a template. Misspellings or malformed syntax silently disable the declaration, leading to confusing runtime errors when required locals are missing.
Additionally, Rails only supports keyword arguments in strict locals declarations. Positional, block, and splat arguments will raise an ActionView::Error at render-time.
This rule catches invalid comment forms and argument types early during development.
Most of these offenses are corrected by --fix. The rewrites are safe because the declaration they replace is one Rails rejects outright.
Before
After
<%# locals() %>
<%# locals: () %>
<%# local: (user:) %>
<%# locals: (user:) %>
<%# locals (user:) %>
<%# locals: (user:) %>
<%# locals:(user:) %>
<%# locals: (user:) %>
<%# locals: %>
<%# locals: () %>
<%# locals: user:, admin: false %>
<%# locals: (user:, admin: false) %>
<%# locals: (user: %>
<%# locals: (user:) %>
<% # locals: (user:) %>
<%# locals: (user:) %>
<%# locals: (user) %>
<%# locals: (user:) %>
<%# locals: (user:,) %>
<%# locals: (user:) %>
A parameter list written without parentheses is wrapped in them, and any bare name in it becomes a required keyword argument, since that is the only form Rails accepts.
Block arguments, splat arguments, and duplicate declarations are reported but not corrected. Each has more than one reasonable rewrite, so the choice is left to you.
Strict locals only support keyword arguments. Use `user:` instead of the positional argument `user`. (erb-strict-locals-comment-syntax)
Block argument:
erb
<%# locals: (&block) %>
Strict locals only support keyword arguments. The block argument `&block` is not supported. (erb-strict-locals-comment-syntax)
Single splat argument:
erb
<%# locals: (*args) %>
Strict locals only support keyword arguments. The splat argument `*args` is not supported. Use `**args` to accept arbitrary keyword arguments. (erb-strict-locals-comment-syntax)
Note: Double-splat (**attributes) IS supported for optional keyword arguments.
Linter Rule: Enforce strict locals comment syntax
Rule:
erb-strict-locals-comment-syntaxDescription
Ensures that strict locals comments use the exact
locals: ( ... )syntax so they are properly recognized by Rails and tooling. Also validates that only keyword arguments are used (no positional, block, or splat arguments).Rationale
Strict locals comments declare which locals are expected in a template. Misspellings or malformed syntax silently disable the declaration, leading to confusing runtime errors when required locals are missing.
Additionally, Rails only supports keyword arguments in strict locals declarations. Positional, block, and splat arguments will raise an
ActionView::Errorat render-time.This rule catches invalid comment forms and argument types early during development.
Autofix
Most of these offenses are corrected by
--fix. The rewrites are safe because the declaration they replace is one Rails rejects outright.<%# locals() %><%# locals: () %><%# local: (user:) %><%# locals: (user:) %><%# locals (user:) %><%# locals: (user:) %><%# locals:(user:) %><%# locals: (user:) %><%# locals: %><%# locals: () %><%# locals: user:, admin: false %><%# locals: (user:, admin: false) %><%# locals: (user: %><%# locals: (user:) %><% # locals: (user:) %><%# locals: (user:) %><%# locals: (user) %><%# locals: (user:) %><%# locals: (user:,) %><%# locals: (user:) %>A parameter list written without parentheses is wrapped in them, and any bare name in it becomes a required keyword argument, since that is the only form Rails accepts.
Block arguments, splat arguments, and duplicate declarations are reported but not corrected. Each has more than one reasonable rewrite, so the choice is left to you.
Examples
✅ Good
Required keyword argument:
Keyword argument with default value:
Complex default values:
No locals (empty):
Double-splat for optional keyword arguments:
🚫 Bad
Wrong comment syntax
Missing colon after
locals:Singular
localinstead oflocals:Missing colon before parentheses:
Missing parentheses around parameters:
Empty
locals:without parentheses:Missing space after the colon:
Unbalanced parentheses:
Wrong tag type (must use ERB comment tag)
Ruby comment in execution tag:
Unsupported argument types
Positional argument (use
user:instead):Block argument:
Single splat argument:
Note: Double-splat (
**attributes) IS supported for optional keyword arguments.Invalid Ruby syntax
Trailing comma:
Leading comma:
Double comma:
Duplicate declarations
Only one
locals:comment is allowed per file:Configuration
Strict locals is an Action View feature, so this rule only applies to Action View projects and needs
frameworkto be set:References