Skip to content

rubocop

rubocop documentation

rubocop - GitHub

Configuration in MegaLinter

Variable Description Default value
RUBY_RUBOCOP_ARGUMENTS User custom arguments to add in linter CLI call
Ex: -s --foo "bar"
RUBY_RUBOCOP_FILTER_REGEX_INCLUDE Custom regex including filter
Ex: (src\|lib)
Include every file
RUBY_RUBOCOP_FILTER_REGEX_EXCLUDE Custom regex excluding filter
Ex: (test\|examples)
Exclude no file
RUBY_RUBOCOP_CLI_LINT_MODE Override default CLI lint mode
- file: Calls the linter for each file
- list_of_files: Call the linter with the list of files as argument
- project: Call the linter from the root of the project
{linter.cli_lint_mode}
RUBY_RUBOCOP_FILE_EXTENSIONS Allowed file extensions. "*" matches any extension, "" matches empty extension. Empty list excludes all files
Ex: [".py", ""]
[".rb"]
RUBY_RUBOCOP_FILE_NAMES_REGEX File name regex filters. Regular expression list for filtering files by their base names using regex full match. Empty list includes all files
Ex: ["Dockerfile(-.+)?", "Jenkinsfile"]
Include every file
RUBY_RUBOCOP_PRE_COMMANDS List of bash commands to run before the linter None
RUBY_RUBOCOP_POST_COMMANDS List of bash commands to run after the linter None
RUBY_RUBOCOP_CONFIG_FILE rubocop configuration file name
Use LINTER_DEFAULT to let the linter find it
.ruby-lint.yml
RUBY_RUBOCOP_RULES_PATH Path where to find linter configuration file Workspace folder, then MegaLinter default rules
RUBY_RUBOCOP_DISABLE_ERRORS Run linter but consider errors as warnings false
RUBY_RUBOCOP_DISABLE_ERRORS_IF_LESS_THAN Maximum number of errors allowed 0

IDE Integration

Use rubocop in your favorite IDE to catch errors before MegaLinter !

IDE Extension Name Install
Atom linter-rubocop Visit Web Site
Brackets brackets-rubocop Visit Web Site
Emacs rubocop-emacs Visit Web Site
Emacs flycheck Visit Web Site
IDEA RubyMine Visit Web Site
Sublime Text sublime_rubocop Visit Web Site
vim vim-rubocop Visit Web Site
vim ale Visit Web Site
Visual Studio Code VsCode Ruby Extension Install in VsCode

MegaLinter Flavours

This linter is available in the following flavours

Flavor Description Embedded linters Info
all Default MegaLinter Flavor 95 Docker Image Size (tag) Docker Pulls
ruby Optimized for RUBY based projects 41 Docker Image Size (tag) Docker Pulls

Behind the scenes

How are identified applicable files

  • File extensions: .rb

How the linting is performed

  • rubocop is called one time by identified file

Example calls

rubocop --force-exclusion myfile.rb
rubocop --force-exclusion -c .ruby-lint.yml myfile.rb
rubocop --force-exclusion --safe-auto-correct -c .ruby-lint.yml myfile.rb

Help content

Usage: rubocop [options] [file1, file2, ...]
    -L, --list-target-files          List all files RuboCop will inspect.
        --except [COP1,COP2,...]     Disable the given cop(s).
        --only [COP1,COP2,...]       Run only the given cop(s).
        --only-guide-cops            Run only cops for rules that link to a
                                     style guide.
    -c, --config FILE                Specify configuration file.
        --force-exclusion            Force excluding files specified in the
                                     configuration `Exclude` even if they are
                                     explicitly passed as arguments.
        --ignore-parent-exclusion    Prevent from inheriting AllCops/Exclude from
                                     parent folders.
        --force-default-config       Use default configuration even if configuration
                                     files are present in the directory tree.
        --auto-gen-config            Generate a configuration file acting as a
                                     TODO list.
        --exclude-limit COUNT        Used together with --auto-gen-config to
                                     set the limit for how many Exclude
                                     properties to generate. Default is 15.
        --disable-uncorrectable      Used with --auto-correct to annotate any
                                     offenses that do not support autocorrect
                                     with `rubocop:todo` comments.
        --no-offense-counts          Do not include offense counts in configuration
                                     file generated by --auto-gen-config.
        --auto-gen-only-exclude      Generate only Exclude parameters and not Max
                                     when running --auto-gen-config, except if the
                                     number of files with offenses is bigger than
                                     exclude-limit.
        --no-auto-gen-timestamp      Do not include the date and time when
                                     the --auto-gen-config was run in the file it
                                     generates.
        --init                       Generate a .rubocop.yml file in the current directory.
    -f, --format FORMATTER           Choose an output formatter. This option
                                     can be specified multiple times to enable
                                     multiple formatters at the same time.
                                     [p]rogress is used by default
                                       [a]utogenconf
                                       [c]lang
                                       [e]macs
                                       [fi]les
                                       [fu]ubar
                                       [h]tml
                                       [j]son
                                       [ju]nit
                                       [o]ffenses
                                       [pa]cman
                                       [p]rogress
                                       [q]uiet
                                       [s]imple
                                       [t]ap
                                       [w]orst
                                       custom formatter class name
    -o, --out FILE                   Write output to a file instead of STDOUT.
                                     This option applies to the previously
                                     specified --format, or the default format
                                     if no format is specified.
    -r, --require FILE               Require Ruby file.
        --fail-level SEVERITY        Minimum severity (A/R/C/W/E/F) for exit
                                     with error code.
        --display-only-fail-level-offenses
                                     Only output offense messages at
                                     the specified --fail-level or above
        --show-cops [COP1,COP2,...]  Shows the given cops, or all cops by
                                     default, and their configurations for the
                                     current directory.
    -F, --fail-fast                  Inspect files in order of modification
                                     time and stop after the first file
                                     containing offenses.
    -C, --cache FLAG                 Use result caching (FLAG=true) or don't
                                     (FLAG=false), default determined by
                                     configuration parameter AllCops: UseCache.
    -d, --debug                      Display debug info.
    -D, --[no-]display-cop-names     Display cop names in offense messages.
                                     Default is true.
    -E, --extra-details              Display extra details in offense messages.
    -S, --display-style-guide        Display style guide URLs in offense messages.
    -a, --auto-correct               Auto-correct offenses.
        --disable-pending-cops       Run without pending cops.
        --enable-pending-cops        Run with pending cops.
        --ignore-disable-comments    Run cops even when they are disabled locally
                                     with a comment.
        --safe                       Run only safe cops.
        --[no-]color                 Force color output on or off.
    -v, --version                    Display version.
    -V, --verbose-version            Display verbose version.
    -P, --parallel                   Use available CPUs to execute inspection in
                                     parallel.
    -l, --lint                       Run only lint cops.
    -x, --fix-layout                 Run only layout cops, with auto-correct on.
        --safe-auto-correct          Run auto-correct only when it's safe.
    -s, --stdin FILE                 Pipe source from STDIN, using FILE in offense
                                     reports. This is useful for editor integration.

Installation on mega-linter Docker image

Example success log

Results of rubocop linter (version 0.82.0)
See documentation on https://megalinter.github.io/descriptors/ruby_rubocop/
-----------------------------------------------

[SUCCESS] .automation/test/ruby/ruby_good_1.rb
    Inspecting 1 file
    .

    1 file inspected, no offenses detected

Example error log

Results of rubocop linter (version 0.82.0)
See documentation on https://megalinter.github.io/descriptors/ruby_rubocop/
-----------------------------------------------

[ERROR] .automation/test/ruby/ruby_bad_1.rb
    Inspecting 1 file
    C

    Offenses:

    mp/lint/.automation/test/ruby/ruby_bad_1.rb:1:1: C: Style/FrozenStringLiteralComment: Missing frozen string literal comment.
    mp/lint/.automation/test/ruby/ruby_bad_1.rb:11:11: C: Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
    File.open('/tmp/urls.txt', " w" ) do | file|
              ^^^^^^^^^^^^^^^
    mp/lint/.automation/test/ruby/ruby_bad_1.rb:11:32: C: Layout/SpaceInsideParens: Space inside parentheses detected.
    File.open('/tmp/urls.txt', " w" ) do | file|
                                   ^
    mp/lint/.automation/test/ruby/ruby_bad_1.rb:11:39: C: Layout/SpaceAroundBlockParameters: Space before first block parameter detected.
    File.open('/tmp/urls.txt', " w" ) do | file|
                                          ^
    mp/lint/.automation/test/ruby/ruby_bad_1.rb:12:24: C: Layout/SpaceAroundBlockParameters: Space after last block parameter detected.
      Hook.active.map do |h |
                           ^
    mp/lint/.automation/test/ruby/ruby_bad_1.rb:13:13: C: Layout/SpaceInsideArrayLiteralBrackets: Do not use space inside array brackets.
        urls = [ ARRAY_OF_URLS_CALLING_INSTANCE]
                ^

    1 file inspected, 6 offenses detected