Skip to content

rubocop

GitHub last commit

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
file
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 97 Docker Image Size (tag) Docker Pulls
ruby Optimized for RUBY based projects 42 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, ...]

Basic Options:
    -l, --lint                       Run only lint cops.
    -x, --fix-layout                 Run only layout cops, with autocorrect on.
        --safe                       Run only safe cops.
        --except [COP1,COP2,...]     Exclude 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.
    -F, --fail-fast                  Inspect files in order of modification
                                     time and stop after the first file
                                     containing 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
                                     by a `rubocop:disable` directive.
        --force-exclusion            Any files excluded by `Exclude` in configuration
                                     files will be excluded, even if given explicitly
                                     as arguments.
        --only-recognized-file-types Inspect files given on the command line only if
                                     they are listed in `AllCops/Include` parameters
                                     of user configuration or default configuration.
        --ignore-parent-exclusion    Prevent from inheriting `AllCops/Exclude` from
                                     parent folders.
        --ignore-unrecognized-cops   Ignore unrecognized cops or departments in the config.
        --force-default-config       Use default configuration even if configuration
                                     files are present in the directory tree.
    -s, --stdin FILE                 Pipe source from STDIN, using FILE in offense
                                     reports. This is useful for editor integration.
    -P, --[no-]parallel              Use available CPUs to execute inspection in
                                     parallel. Default is true.
        --fail-level SEVERITY        Minimum severity for exit with error code.
                                       [A] autocorrect
                                       [I] info
                                       [R] refactor
                                       [C] convention
                                       [W] warning
                                       [E] error
                                       [F] fatal

Caching:
    -C, --cache FLAG                 Use result caching (FLAG=true) or don't
                                     (FLAG=false), default determined by
                                     configuration parameter AllCops: UseCache.
        --cache-root DIR             Set the cache root directory.
                                     Takes precedence over the configuration
                                     parameter AllCops: CacheRootDirectory and
                                     the $RUBOCOP_CACHE_ROOT environment variable.

Output Options:
    -f, --format FORMATTER           Choose an output formatter. This option
                                     can be specified multiple times to enable
                                     multiple formatters at the same time.
                                       [a]utogenconf
                                       [c]lang
                                       [e]macs
                                       [fi]les
                                       [fu]ubar
                                       [g]ithub
                                       [h]tml
                                       [j]son
                                       [ju]nit
                                       [m]arkdown
                                       [o]ffenses
                                       [pa]cman
                                       [p]rogress (default)
                                       [q]uiet
                                       [s]imple
                                       [t]ap
                                       [w]orst
                                       custom formatter class name
    -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.
    -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.
        --stderr                     Write all output to stderr except for the
                                     autocorrected source. This is especially useful
                                     when combined with --autocorrect and --stdin.
        --display-time               Display elapsed time in seconds.
        --display-only-failed        Only output offense messages. Omit passing
                                     cops. Only valid for --format junit.
        --display-only-fail-level-offenses
                                     Only output offense messages at
                                     the specified --fail-level or above
        --display-only-correctable   Only output correctable offense messages.
        --display-only-safe-correctable
                                     Only output safe-correctable offense messages
                                     when combined with --display-only-correctable.

Autocorrection:
    -a, --autocorrect                Autocorrect offenses (only when it's safe).
        --auto-correct               (same, deprecated)
        --safe-auto-correct          (same, deprecated)
    -A, --autocorrect-all            Autocorrect offenses (safe and unsafe).
        --auto-correct-all           (same, deprecated)
        --disable-uncorrectable      Used with --autocorrect to annotate any
                                     offenses that do not support autocorrect
                                     with `rubocop:todo` comments.

Config Generation:
        --auto-gen-config            Generate a configuration file acting as a
                                     TODO list.
        --regenerate-todo            Regenerate the TODO configuration file using
                                     the last configuration. If there is no existing
                                     TODO file, acts like --auto-gen-config.
        --exclude-limit COUNT        Set the limit for how many files to explicitly exclude.
                                     If there are more files than the limit, the cop will
                                     be disabled instead. Default is 15.
        --[no-]offense-counts        Include offense counts in configuration
                                     file generated by --auto-gen-config.
                                     Default is true.
        --[no-]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. Default is false.
        --[no-]auto-gen-timestamp    Include the date and time when the --auto-gen-config
                                     was run in the file it generates. Default is true.

Additional Modes:
    -L, --list-target-files          List all files RuboCop will inspect.
        --show-cops [COP1,COP2,...]  Shows the given cops, or all cops by
                                     default, and their configurations for the
                                     current directory.
        --show-docs-url [COP1,COP2,...]
                                     Display url to documentation for the given
                                     cops, or base url by default.

General Options:
        --init                       Generate a .rubocop.yml file in the current directory.
    -c, --config FILE                Specify configuration file.
    -d, --debug                      Display debug info.
    -r, --require FILE               Require Ruby file.
        --[no-]color                 Force color output on or off.
    -v, --version                    Display version.
    -V, --verbose-version            Display verbose version.

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