I introduced RuboCop to my Rails project the other day, but I have a lot of settings and I didn't know what to write, so I would like to introduce my settings. The target audience is those who are new to RuboCop.
RuboCop is one of the static code analysis tools that allows you to check if your code is written according to coding standards. I will omit the installation method of RuboCop.
In the first place, you do not have to write the RuboCop settings first, but you should try the code check once and write only the parts that do not match the coding standards of the individual or the team in the settings.
.rubocop.yml
.yml
require:
- rubocop-rails
- rubocop-performance
AllCops:
TargetRubyVersion: 2.7.1
TargetRailsVersion: 6.0.3.3
Exclude:
- "node_modules/**/*"
- "tmp/**/*"
- "vendor/**/*"
- ".git/**/*"
- "config/**/*"
- "app/channels/**/*"
- "app/mailers/*"
- "db/migrate/*"
- "bin/*"
- "db/schema.rb"
- "spec/rails_helper.rb"
- "spec/spec_helper.rb"
- "Gemfile"
- "Rakefile"
- "config.ru"
Rails:
Enabled: true
#Set default to double quotes
Style/StringLiterals:
EnforcedStyle: double_quotes
#Allow Japanese comments
Style/AsciiComments:
Enabled: false
#class description No comment required
Style/Documentation:
Enabled: false
#Frozen literal comments not required
Style/FrozenStringLiteralComment:
Enabled: false
The gems introduced this time are the following three.
require: rubocop-performance This description is required when installing gem rubocop-performance.
require: rubocop-rails This is a necessary description when installing gem rubocop-rails.
AllCops: TargetRubyVersion: Specify the version of Ruby you are using.
AllCops: TargetRailsVersion: Specify the version of Rails you are using.
AllCops: Exclide: Specify the file for which code checking is not performed. If you specify an automatically generated file or a file that is rarely changed here, an error will not be output unnecessarily.
Rails: Enabled: true Set to true when gem rubocop-rails is installed.
Style/StringLiterals: It is a specification of a string literal. I basically try to use double quotes for string literals, but RuboCop defaults to single quotes when there is no expression expansion. If you want to use double quotes, specify EnforcedStyle: double_quotes.
Style/AsciiComments: By default, writing comments in code in Japanese is not recommended. However, for now, only I can read the code, so specify Enabled: false to allow commenting in Japanese. Enabled: false means that this item itself is not checked.
Style/Documentation: This is an item to write a comment to explain the class before defining the class called class hoge, but since I can not write a comment for all classes, I set it to false.
Style/FrozenStringLiteralComment: Frozen literal comments are for making strings in a file immutable by including the comment # frozen_string_literal: true. I decided that it was not necessary for this development and set it to false.
To make the code easier to read and maintain, we want to set it appropriately.
Recommended Posts