rubocop-view_component

A RuboCop extension that encourages ViewComponent best practices.

Installation

Add to your Gemfile:

gem 'rubocop-view_component', require: false

Add to your .rubocop.yml:

require:
  - rubocop-view_component

Cops

This gem provides several cops to enforce ViewComponent best practices:

  • ViewComponent/ComponentSuffix - Enforce -Component suffix 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 (requires PreviewPaths configuration). Classes listed in ViewComponentParentClasses are automatically exempt, as abstract base classes are not rendered standalone.

Optional Configuration

Base Class

By default, the cops detect classes that inherit from ViewComponent::Base or ApplicationComponent. To add extra parent classes, use inherit_mode: merge so the defaults are preserved:

# .rubocop.yml
ViewComponent:
  inherit_mode:
    merge:
      - ViewComponentParentClasses
  ViewComponentParentClasses:
    - MyApp::BaseComponent

To replace the defaults entirely (e.g. if your project has renamed ApplicationComponent), omit inherit_mode:

# .rubocop.yml
ViewComponent:
  ViewComponentParentClasses:
    - ViewComponent::Base
    - MyApp::BaseComponent

Components Directory

Several cops (ComponentSuffix, PreferComposition, MissingPreview, 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:

  1. Update the version number in lib/rubocop/view_component/version.rb
  2. Run bundle install to update Gemfile.lock
  3. Commit and push both files
  4. 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 primer

If you intentionally change cop behavior, regenerate the snapshot:

script/verify primer --regenerate

To force download the latest Primer source:

script/verify primer --update

x-govuk Components

To verify against x-govuk/govuk-components locally:

script/verify govuk

If you intentionally change cop behavior, regenerate the snapshot:

script/verify govuk --regenerate

To force download the latest x-govuk source:

script/verify govuk --update

Polaris ViewComponents

To verify against baoagency/polaris_view_components locally:

script/verify polaris

If you intentionally change cop behavior, regenerate the snapshot:

script/verify polaris --regenerate

To force download the latest Polaris source:

script/verify polaris --update

Contributing

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.