Load only the cops each configuration needs
What does this MR do?
Makes each rubocop-<type>.yml load only the custom cops it configures, instead of every cop in the gem. rubocop-minimal.yml, meant for plain Ruby projects, no longer enables Rails-only cops such as CodeReuse/ActiveRecord. rubocop-default.yml still loads every cop, because it inherits every configuration.
Closes #76 (closed)
Breaking change:
- Projects that inherit only some configurations stop running the custom cops those configurations don't configure.
- Configuring a custom cop without inheriting the configuration that loads it fails with
Error: unrecognized cop or department(exit code 2). For example,CodeReuse/ActiveRecord: Enabled: falsewith onlyrubocop-minimal.yml: delete such entries. rubocop:disablecomments for cops that are no longer loaded becomeLint/RedundantCopDisableDirectiveoffenses.
Where each cop is loaded now
Each configuration requires the cops it configures, the way rubocop-bundler.yml already required gem_fetcher. The cops that no configuration set up went where they fit.
| Configuration | Cops |
|---|---|
rubocop-bundler.yml |
Cop/GemFetcher (unchanged) |
rubocop-code_reuse.yml |
CodeReuse/ActiveRecord |
rubocop-fips.yml |
Fips/* |
rubocop-internal-affairs.yml |
InternalAffairs/* |
rubocop-layout.yml |
Layout/LineBreakAfterFinalMixin, Cop/LineBreakAroundConditionalBlock |
rubocop-migrations.yml |
Migration/UpdateLargeTable |
rubocop-performance.yml |
Performance/Rubyzip |
rubocop-rails.yml |
Cop/ActiveRecordDependent, Cop/ActiveRecordSerialize, Cop/InBatches, Cop/PolymorphicAssociations, Cop/RedirectWithStatus, Rails/IncludeUrlHelper |
rubocop-rspec.yml |
RSpec/* through lib/gitlab/styles/rubocop/rspec.rb (unchanged) |
rubocop-security.yml |
GitlabSecurity/* |
rubocop-style.yml |
Style/*, Cop/AvoidReturnFromBlocks, Cop/CustomErrorClass |
rubocop-tailwind.yml |
Tailwind/* |
GitlabSecurity/DeepMunge,RedirectToParamsUpdateandSendFileParamsonly apply to Rails code. They stay inrubocop-security.ymlwith the rest of the security cops, because they only match Rails calls.lib/gitlab/styles/rubocop.rbstill loads every non-RSpec cop, for anyone who requires it from Ruby. No configuration uses it any more.
Effect and who's affected
-
Before and after:
Configuration Custom cops before (loaded / enabled) After rubocop-minimal.yml45 / 42 30 / 27 rubocop-default.yml45 / 39 44 / 38 The one cop fewer in
rubocop-default.ymlisRSpec/Base(see below). -
Who's affected in
gitlab-org: a code search found 44 projects that configureCodeReuse/ActiveRecord.- 32 inherit
rubocop-default.ymland are unaffected. So are theipynbdiffgems, which inherit the monolith's configuration. - 3 pick individual configurations and turned the cop off by hand:
gitlab-org/quality/performance,gitlab-org/ruby/gems/driandgitlab-org/quality/upgrade-tester. After upgrading, they need to delete that entry.
- 32 inherit
-
This gem's own lint had the same workaround. This MR removes its
.rubocop.ymlblock that turned off six Rails cops "auto-enabled as part ofgitlab-styles", and a Tailwind disable/enable pair in a spec.
Also in this MR
RSpec/Base, the abstract base class of our RSpec cops, was registered and enabled as a cop. It now calls exclude_from_registry, like rubocop-rspec's own base class. All 10 custom RSpec cops are still registered.
Specs
New examples in spec/yml_spec.rb:
rubocop-minimal.ymldoesn't load our Rails, code reuse, migration, internal affairs or Tailwind cops. This fails on master.rubocop-default.ymlloads every cop in the gem. It fails, naming the missing cop, if a cop file isn't required by any configuration.
The existing "runs successfully" examples check that no configuration sets up a cop it doesn't load.