bbatsov/rubocop

View on GitHub
docs/modules/ROOT/pages/integration_with_other_tools.adoc

Summary

Maintainability
Test Coverage
= Integration with Other Tools

== Speeding up integrations

RuboCop integrates with quite a number of other tools, including editors which may attempt
to do autocorrection for you. In these cases, `rubocop` ends up getting called repeatedly,
which may result in some slowness, as `rubocop` has to require its entire environment on
each call.

You can alleviate some of that boot time by using xref:usage/server.adoc["Server"] or
https://github.com/fohte/rubocop-daemon[rubocop-daemon]. `rubocop-daemon` is a
wrapper around `rubocop` that loads everything into a daemonized process so that
subsequent runs save on that boot time after the first execution. Please see the
rubocop-daemon documentation for setup instructions and examples of how to use it
with some editors and other tools.

NOTE: RuboCop's built-in xref:usage/caching.adoc[caching] should also be used to ensure
that source files that have not been changed are not being re-evaluated unnecessarily.

== Editor integration

All popular editors provide some form of linter integration. Typically this is done
either via LSP (if supported by the underlying server) or by shelling out to a linter and processing its output so it could be displayed inside the editor.

As noted above, the `rubocop` binary starts relatively slowly which makes it problematic in the shelling out case. The RuboCop xref:usage/server.adoc["Server"] functionality
has designed to address this problem and provide lightning fast editor integration.

=== LSP

https://microsoft.github.io/language-server-protocol/[The Language Server Protocol] is the modern standard for providing cross-editor support for various programming languages. The RuboCop xref:usage/lsp.adoc[LSP] functionality has designed to provide built-in language server. The following is a LSP client for interacting with the built-in server:

- https://github.com/rubocop/vscode-rubocop[vscode-rubocop]

And the following Ruby LSP servers are using RuboCop internally to provide code linting functionality:

- https://github.com/Shopify/ruby-lsp[ruby-lsp]
- https://solargraph.org/[Solargraph]

=== Emacs

https://github.com/rubocop/rubocop-emacs[rubocop.el] is a simple
Emacs interface for RuboCop. It allows you to run RuboCop inside Emacs
and quickly jump between problems in your code.

https://github.com/flycheck/flycheck[flycheck] > 0.9 also supports
RuboCop and uses it by default when available.

=== Vim

RuboCop is supported by
https://github.com/vim-syntastic/syntastic[syntastic],
https://github.com/neomake/neomake[neomake],
and https://github.com/dense-analysis/ale[ale].

There is also the https://github.com/ngmy/vim-rubocop[vim-rubocop] plugin.

=== Helix

Helix supports Solargraph natively to provide LSP features. For formatting support, see the https://github.com/helix-editor/helix/wiki/External-binary-formatter-configuration#rubocop[External binary formatter configuration for RuboCop] to use either your bundled or globally installed version of RuboCop.

=== Sublime Text

For ST you might find the
https://github.com/SublimeLinter/SublimeLinter-rubocop[SublimeLinter-rubocop] or the
https://github.com/pderichs/sublime_rubocop[Sublime RuboCop] plugin useful.

You may also consider using one of the LSP servers mentioned above which utilize RuboCop by using the https://github.com/sublimelsp/LSP[Sublime-LSP] plugin and follow its https://lsp.sublimetext.io/language_servers/#ruby-ruby-on-rails[documentation] for configuration.

=== Brackets

The https://github.com/smockle-archive/brackets-rubocop[brackets-rubocop]
extension displays RuboCop results in Brackets.
It can be installed via the extension manager in Brackets.

=== TextMate2

The https://github.com/mrdougal/textmate2-rubocop[textmate2-rubocop]
bundle displays formatted RuboCop results in a new window.
Installation instructions can be found https://github.com/mrdougal/textmate2-rubocop#installation[here].

=== Atom

The https://github.com/AtomLinter/linter-rubocop[linter-rubocop] plugin for Atom's
https://github.com/steelbrain/linter[linter] runs RuboCop and highlights the offenses in Atom.

=== LightTable

The https://github.com/seancaffery/lt-rubocop[lt-rubocop] plugin
provides LightTable integration.

=== RubyMine / Intellij IDEA

RuboCop support is https://www.jetbrains.com/help/idea/2017.1/rubocop.html[available] as of the 2017.1 releases.

=== Visual Studio Code

The https://marketplace.visualstudio.com/items?itemName=rebornix.Ruby[ruby] extension
provides RuboCop integration for Visual Studio Code. RuboCop is also used for the formatting
capabilities of this extension.

=== Other Editors

Here's one great opportunity to contribute to RuboCop - implement
RuboCop integration for your favorite editor.

== Git pre-commit hook integration with overcommit

https://github.com/sds/overcommit[overcommit] is a fully configurable and
extendable Git commit hook manager. To use RuboCop with overcommit, add the
following to your `.overcommit.yml` file:

[source,yaml]
----
PreCommit:
  RuboCop:
    enabled: true
----

== Git pre-commit hook integration with pre-commit

https://pre-commit.com/[pre-commit] is a framework for managing and maintaining
multi-language pre-commit hooks. To use RuboCop with pre-commit, add the
following to your `.pre-commit-config.yaml` file:

[source,yaml]
----
- repo: https://github.com/rubocop/rubocop
  rev: v1.8.1
  hooks:
    - id: rubocop
----

If your RuboCop configuration uses extensions, be sure to include the gems as
entries in `additional_dependencies`:

[source,yaml]
----
- repo: https://github.com/rubocop/rubocop
  rev: v1.8.1
  hooks:
    - id: rubocop
      additional_dependencies:
        - rubocop-rails
        - rubocop-rspec
----

== Guard integration

If you're fond of https://github.com/guard/guard[Guard] you might
like
https://github.com/rubocop/guard-rubocop[guard-rubocop]. It
allows you to automatically check Ruby code style with RuboCop when
files are modified.

== Mega-Linter integration

You can use https://megalinter.io/latest/[Mega-Linter]
to run RuboCop automatically on every PR, and also lint all file
types detected in your repository.

Please follow the
https://megalinter.io/latest/installation[installation instructions]
to activate RuboCop without any additional configuration.

https://megalinter.io/latest/flavors/ruby/[Mega-Linter's Ruby flavor]
is optimized for Ruby linting.

== Rake integration

To use RuboCop in your `Rakefile` add the following:

[source,ruby]
----
require 'rubocop/rake_task'

RuboCop::RakeTask.new
----

If you run `rake -T`, the following two RuboCop tasks should show up:

[source,sh]
----
$ rake rubocop                                 # Run RuboCop
$ rake rubocop:autocorrect                     # Autocorrect RuboCop offenses
----

The above will use default values

[source,ruby]
----
require 'rubocop/rake_task'

desc 'Run RuboCop on the lib directory'
RuboCop::RakeTask.new(:rubocop) do |task|
  task.patterns = ['lib/**/*.rb']
  # only show the files with failures
  task.formatters = ['files']
  # don't abort rake on failure
  task.fail_on_error = false
end
----