rubocop-view_component
A RuboCop extension that encourages ViewComponent best practices.
Installation
Add to your Gemfile:
gem 'rubocop-view_component', require: falseAdd to your .rubocop.yml:
plugins:
- rubocop-view_componentNote: From v0.7.0 onwards, this gem makes use of rubydex to discover which classes inherit from ViewComponent::Base. UseProjectIndex is required. Without it, cops that depend on ancestry detection will raise an error.
AllCops:
UseProjectIndex: trueIf you use a ViewComponent library such as primer then you'll also need to set ProjectIndexIncludesGems: true so the cops can resolve ancestry chains that pass through gem classes.
For more background on how RuboCop uses rubydex for cross-file analysis, see RuboCop 1.89: Project-Wide Analysis with Rubydex.
Cops
This gem provides several cops to enforce ViewComponent best practices:
-
ViewComponent/ComponentSuffix - Enforce
-Componentsuffix for ViewComponent classes -
ViewComponent/NoGlobalState - Prevent direct access to
params,request,session, etc. - ViewComponent/PreferPrivateMethods - Suggest making helper methods private (analyzes ERB templates to avoid flagging methods used in views)
- ViewComponent/PreferSlots - Detect HTML parameters that should be slots
- ViewComponent/PreferComposition - Avoid inheriting one ViewComponent from another (prefer composition)
- ViewComponent/TestRenderedOutput - Encourage testing rendered output over private methods
-
ViewComponent/MissingPreview - Ensure every ViewComponent has a corresponding preview file (defaults to
test/components/previewsandspec/components/previewsifPreviewPathsis not configured). Abstract base classes with descendants are automatically exempt.
Optional Configuration
Components Directory
Several cops (ComponentSuffix, PreferComposition, TestRenderedOutput) default to running only on files under app/components/. If your project uses a different path, override Include in your .rubocop.yml:
# .rubocop.yml
ViewComponent/ComponentSuffix:
Include:
- 'app/components/**/*.rb'
- 'engines/*/app/components/**/*.rb'No Super
ViewComponent convention is to not call super in component initializers, but that may cause Lint/MissingSuper failures from RuboCop. We suggest disabling that rule for your view components directory, for example:
# .rubocop.yml
Lint/MissingSuper:
Exclude:
- 'app/components/**/*'Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install.
To release a new version:
- Update the version number in
lib/rubocop/view_component/version.rb - Run
bundle installto updateGemfile.lock - Commit and push both files
- Trigger the Push Gem workflow via GitHub Actions (uses trusted publishing — no API key needed)
Real-World Verification
The cops are tested against real-world component libraries as baselines to catch regressions.
The script/verify script downloads component libraries (cached in verification/), runs all ViewComponent cops against them, and compares the results to checked-in snapshots. This runs automatically in CI.
Primer ViewComponents
To verify against primer/view_components locally:
script/verify primerIf you intentionally change cop behavior, regenerate the snapshot:
script/verify primer --regenerateTo force download the latest Primer source:
script/verify primer --updatex-govuk Components
To verify against x-govuk/govuk-components locally:
script/verify govukIf you intentionally change cop behavior, regenerate the snapshot:
script/verify govuk --regenerateTo force download the latest x-govuk source:
script/verify govuk --updatePolaris ViewComponents
To verify against baoagency/polaris_view_components locally:
script/verify polarisIf you intentionally change cop behavior, regenerate the snapshot:
script/verify polaris --regenerateTo force download the latest Polaris source:
script/verify polaris --updateFlowbite Components
To verify against substancelab/flowbite-components locally:
script/verify flowbiteIf you intentionally change cop behavior, regenerate the snapshot:
script/verify flowbite --regenerateTo force download the latest Flowbite source:
script/verify flowbite --updateContributing
Bug reports and pull requests are welcome on GitHub at https://github.com/andyw8/rubocop-view_component.
License
The gem is available as open source under the terms of the MIT License.