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: false with only rubocop-minimal.yml: delete such entries.
  • rubocop:disable comments for cops that are no longer loaded become Lint/RedundantCopDisableDirective offenses.
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, RedirectToParamsUpdate and SendFileParams only apply to Rails code. They stay in rubocop-security.yml with the rest of the security cops, because they only match Rails calls.
  • lib/gitlab/styles/rubocop.rb still 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.yml 45 / 42 30 / 27
    rubocop-default.yml 45 / 39 44 / 38

    The one cop fewer in rubocop-default.yml is RSpec/Base (see below).

  • Who's affected in gitlab-org: a code search found 44 projects that configure CodeReuse/ActiveRecord.

    • 32 inherit rubocop-default.yml and are unaffected. So are the ipynbdiff gems, 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/dri and gitlab-org/quality/upgrade-tester. After upgrading, they need to delete that entry.
  • This gem's own lint had the same workaround. This MR removes its .rubocop.yml block that turned off six Rails cops "auto-enabled as part of gitlab-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.yml doesn't load our Rails, code reuse, migration, internal affairs or Tailwind cops. This fails on master.
  • rubocop-default.yml loads 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.

Merge request reports

Loading
Loading