Using Rubocop gem in Rails

Tram Ho

Preamble

Rubocop is a ruby-style-guide code checking tool, built to serve Ruby developers. Rubocop uses predefined rules to compare them with your code and then issue error messages. We can easily see the benefits when using Rubocop below:

  • Assess the source of the project as the maximum length of lines, methods, classes, …
  • Easily check coding convention errors, save time for reviewers
  • Create synchronization on the source code of the project

Setting

To install you can type the following command on terminal:

Or, you can add the following to Gemfile and run the bundle install command:

Using

  • Run all directories:
  • Run a separate folder:
  • Run a separate file:
  • Temporarily run different directories and files:

Configuration

Basically, the installation above is enough for us to use this gem already. However, depending on the project, each company, our code rules will be different so rubocop does not completely fix the error rules. We can completely edit the configuration so that rubocop catches the error exactly what you want. You can edit it in the .rubocop.yml file, which will overwrite its default default cops.

The format of .rubocop.yml is in the form:

  • Inheritance

    Rubocop supports inheriting configurations from one or more different config files. The inherit_from option can be used to include config from one or more files. Control it can have common settings between projects in a certain original .rubocop.yml file and then can be customized in files, subfolders. The following example we have 2 files .rubocop_enabled.yml and .rubocop_disabled.yml are included in the file .rubocop.yml

  • Including / Excluding files

    Include: only check cops in the manifest file:

    Exclude: check cops except for the manifest files

  • The syntax of a cops is as follows:

    Inside:

    • rule_name: is the name of a cop
    • Description: describe cop
    • Enabled: used to enable (true) or disable (false) a cop, usually by default will be true
    • Key, value: used to add information for a given cop, such as Max values ​​for line length, method, class, etc.

For example: I have the following ruby ​​code

After running the command rubocop app / controllers / tests_controller.rb, the result is as follows:

As above we see rubocop has reported 5 errors about the convention, we analyze the errors as follows:

  • For the two errors, Style / Documentation: Missing top-level class documentation comment, Style / FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true for me personally, n is not really that important so we can disable it. in the config file as follows:
  • Use snake_case for method names This error is the method name must be in the form snake_case, in the example above we fix the following: badName => bad_name
  • Use def with parentheses when there are parameters: the method definition must have parentheses, in the example correction: bad_name (name)
  • Layout / IndentationWidth: Use 2 (not 0) spaces for indentation: When dropping to the second line

The complete code will look like this:

You can refer to some more cops here: ruby-style-guide

  • Some symbols may appear in the output as follows:
SymbolDescribe
.The file does not have any problems
CThe file contains convention related issues
EThe file contains an error
FThe file contains a fata error
WFile has no warning

summary

The article has references:

https://github.com/rubocop-hq/rubocop/blob/master/manual/configuration.md

https://github.com/rubocop-hq/rubocop

Share the news now

Source : Viblo