Rubocop, how to Disable/Enable cops on blocks of code
I answer my question because it is always very difficult for me to find the reference to this solution:
# rubocop:disable Metrics/MethodLength
def my_code
..
end
# rubocop:enable Metrics/MethodLength
Same for multiple cops:
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def my_code
..
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
Documentation: https://docs.rubocop.org/rubocop/configuration.html#disabling-cops-within-source-code
Rubocop disable rule in AllCops
It feels to me like you are affected by this issue in Rubocop. I suggest upgrading Rubocop to a newer version.
How to disable 'warning no department given for 'COP' message on running rubocop
A qualified cop name is Department/CopName
. For example, Style/Documentation
is qualified and Documentation
is unqualified.
The documentation indicates that:
Qualifying cop name with its type, e.g.,
Style
, is recommended, but not necessary as long as the cop name is unique across all types.
But they show a warning for unqualified names. That happens here:
# RuboCop::Cop::Registry
def qualified_cop_name(name, path, shall_warn = true)
badge = Badge.parse(name)
if shall_warn && department_missing?(badge, name)
print_warning(name, path)
end
return name if registered?(badge)
potential_badges = qualify_badge(badge)
case potential_badges.size
when 0 then name # No namespace found. Deal with it later in caller.
when 1 then resolve_badge(badge, potential_badges.first, path)
else raise AmbiguousCopName.new(badge, path, potential_badges)
end
end
shall_warn
is only false
when the --auto-correct
option is in use. There isn't a way to disable it currently.
The only way to silence the warning is to include the department for each cop in your config like:
Style/Documentation:
Enabled: false
Style/ClassAndModuleChildren:
Enabled: false
Metrics/LineLength:
Max: 120
Style/GuardClause:
Enabled: false
Style/IfUnlessModifier:
Enabled: false
If a Rubocop rule is disabled in-line does it need to be re-enabled
In-line config is applied to the given file only (just tested it).
Rails: rubocop disable Class has too many lines error
Try
class Xzy # rubocop:disable Metrics/ClassLength
Rubocop command to disable Rails/SkipsModelValidations: avoid using update_all error
You can run the below command, which will generate a .rubocop_todo.yml file which will record and ignore specific offences for offending files:
rubocop --auto-gen-config --exclude-limit 999 --no-offense-counts
--auto-gen-config
generates the yml file, the only non-optional flag to achieve what you are looking for.
--exclude-limit xxx
is a number of offence before rubocop disables the check for the entire application.
--no-offense-counts
does not record a count how many offences there are in the yml file.
Ensure you've thought about the consequences of ignoring linters; it is usually ill advised to not follow the linter advice. Be aware that this will disable checks for all offences in the spec tests.
Update
If you want to disable rubocop checks without generating a file, you can use a comment like so:
Company.where(cms_tag_id: webhook.dig('entity', 'id')).update_all(is_deleted: true) # rubocop:disable Rails/SkipsModelValidations
Related Topics
Rails 404 Error for Stylesheet or JavaScript Files
Why Does Trying to Use Grape with Rails Fail with "Uninitialized Constant API"
Generating Devise Controllers - Rails Devise
Ruby on Rails: Create Confirmation View Before Creating the Object
How to Stop Bundler from Adding Ruby Version to Gemfile.Lock
Ruby Regexp to Replace Equations
Using Class Instance Variable for Mutex in Ruby
Find Out Which Words in a Large List Occur in a Small String
Byebug Fully Supports Windows or Not
Elegant Way to Only Show Records If They Exist in Rails Erb
404 Resource Not Found: Domain with Google Directory API
Brew Install Coreutils Failing 10.9.5 Osx
How to Add Iedriverserver to Path
Can You Have Multiple Versions of a Gem in a Gemfile
Assign to an Array and Replace Emerged Nil Values