standard-erb
Lint the Ruby inside your ERB templates with Standard.
This gem is a thin adapter that makes rubocop-erb load as a Standard plugin. It is not a fork: rubocop-erb keeps doing all the work of extracting Ruby from templates; this gem only fixes the integration layer that breaks under Standard.
Why does this exist?
rubocop-erb works fine under plain RuboCop, but Standard does two things RuboCop doesn't:
- It validates plugin configs. RuboCop never validates the configuration a plugin contributes; Standard does. rubocop-erb's config contains an entry for a cop that doesn't exist (
Layout/LeadingEmptyLine) and a customization ofLint/Syntax, which RuboCop forbids configuring. Both are harmless under RuboCop and fatal under Standard. This gem drops those two entries. - It merges plugin configs first-come-first-served. Standard's own rules are always merged first, and once a cop is configured, later plugins can't touch it. rubocop-erb excludes
**/*.erbfrom ~50 layout and style cops that make no sense inside a template, but since Standard already configures all of those cops, the excludes are silently discarded. No plugin can fix this; it has to be done in your.standard.yml(see below).
On top of that, the gem's name (rubocop-erb) doesn't match its require path (rubocop/erb), so it can't be listed in plugins: without extra configuration. standard-erb can.
Installation
# Gemfile
gem "standard-erb", require: false
# .standard.yml
plugins:
- standard-erb
That's enough for standardrb to pick up .erb files and report offenses in them.
Silencing layout cops inside templates
Because of the merge behavior described above, you also need to tell Standard which cops to skip for ERB files. Standard's ignore: key is applied after all plugins are merged, so it's the one place this can be done. Copy this block into your .standard.yml (paths resolve relative to the config file):
ignore:
- "**/*.erb":
- Layout/ArgumentAlignment
- Layout/ArrayAlignment
- Layout/BlockAlignment
- Layout/ClosingParenthesisIndentation
- Layout/CommentIndentation
- Layout/InitialIndentation
- Layout/EmptyLineAfterGuardClause
- Layout/EndAlignment
- Layout/FirstArgumentIndentation
- Layout/FirstArrayElementIndentation
- Layout/FirstArrayElementLineBreak
- Layout/FirstHashElementIndentation
- Layout/FirstHashElementLineBreak
- Layout/FirstMethodArgumentLineBreak
- Layout/FirstParameterIndentation
- Layout/HashAlignment
- Layout/IndentationConsistency
- Layout/IndentationWidth
- Layout/LeadingEmptyLine
- Layout/LeadingEmptyLines
- Layout/LineEndStringConcatenationIndentation
- Layout/LineLength
- Layout/MultilineArrayBraceLayout
- Layout/MultilineAssignmentLayout
- Layout/MultilineHashBraceLayout
- Layout/MultilineMethodCallBraceLayout
- Layout/MultilineMethodCallIndentation
- Layout/MultilineMethodDefinitionBraceLayout
- Layout/MultilineOperationIndentation
- Layout/ParameterAlignment
- Layout/TrailingEmptyLines
- Layout/TrailingWhitespace
- Lint/EmptyFile
- Lint/OutOfRangeRegexpRef
- Lint/Syntax
- Lint/UselessAssignment
- Lint/Void
- Metrics/BlockLength
- Metrics/BlockNesting
- Naming/FileName
- Style/FrozenStringLiteralComment
- Style/IdenticalConditionalBranches
- Style/IfUnlessModifier
- Style/MultilineTernaryOperator
- Style/NestedTernaryOperator
- Style/Next
- Style/ParallelAssignment
- Style/RescueModifier
- Style/Semicolon
- Style/TrailingCommaInArguments
- Style/TrailingCommaInArrayLiteral
- Style/TrailingCommaInHashLiteral
- Style/WhileUntilDo
- Style/WhileUntilModifier
This is exactly the list of cops rubocop-erb itself excludes from .erb files. It's kept in sync with the installed rubocop-erb version by this gem's test suite.
Lint/Syntax is in the list because the Ruby that rubocop-erb extracts from a template isn't always parseable on its own. If you'd rather see RuboCop's syntax diagnostics on templates, remove it.
Development
Run bin/setup to install dependencies, then bundle exec rake to run the tests and lint the gem itself.
License
MIT. See LICENSE.txt.