Add configurable vue-mixin-pairing ESLint rule
What does this MR do and why?
This MR replaces three "ad hoc" ESLint rules with one configurable rule that covers nine mixins. Three of them are the ones the old rules guarded. Six had no guard at all so it was easy to make a mistake.
With this change we can cover 9 different mixins with one rule (and a more complicated config entry).
From...
Each time the Vue 3 migration hit a mixin that had to be declared where it is used, we wrote a rule for that one mixin:
vue3-gl-slots-mixin-pairingforglSlotsMixinvue3-gl-listeners-mixin-pairingforglListenersMixingl-toast-mixinforGlToastMixin
Each rule solved the same problem again: walk the script and the template, find the usages, find the registration, report when one side is missing. Two of the three had no autofix. The one that did knew a single import and a single name. Nothing guarded glFeatureFlagsMixin, glAbilitiesMixin, glLicensedFeaturesMixin, timeagoMixin, InternalEvents.mixin() or Tracking.mixin(), and together those are registered in about 750 component files.
...to
One rule, local-rules/vue-mixin-pairing, driven by a table in eslint.config.mjs. The table is the structured statement of how we use mixins in this codebase: which module supplies the mixin, how it is registered, and which instance members it provides.
{ source: '~/vue_shared/mixins/gl_feature_flags_mixin', imported: 'default', localName: 'glFeatureFlagsMixin', factory: true, members: ['glFeatures'] }For every entry the rule enforces, in the same component file:
- a member used in the template or script has a registration of a mixin that supplies it, or the file fails lint;
- a registration whose members are never used is reported as unused.
The rule handles what the codebase actually does rather than what a single rule needed:
- Mixins are matched by import, so
glFeatureFlagMixin,TimeagoMixinand a relative../mixins/timeagoresolve to the same entry. There are 75 such registrations. - A registration is a bare identifier, a factory call, a
Name.mixin()call, or a module-scopeconst trackingMixin = InternalEvents.mixin(). 57 files use the alias form. - A member the component declares itself under
props,computed,methods,injectordata()is not a mixin usage.inject: ['glFeatures']stays valid, and a localtrackEventmethod does not demandInternalEvents. - Two entries can supply the same member.
InternalEventsis importable from two paths.
Configured entries: GlToastMixin, glSlotsMixin, glListenersMixin, glFeatureFlagsMixin, glAbilitiesMixin, glLicensedFeaturesMixin, timeagoMixin, InternalEvents.mixin() (both import paths), Tracking.mixin().
A companion MR, !256699 (closed), applies the same idea to directives. It is stacked on this one because it shares the small options-object helper added here. The two rules are otherwise independent.
Why now?
The Vue 3 migration. Today if a component forgets adding GlToastMixin we can still access this.$toast because some other bundle on the page ran Vue.use(GlToastPlugin) on the shared Vue 2 constructor. Vue 3 apps are isolated, so the same component would throw under Vue 3.
Jest can't find these gaps because specs inject the missing pieces. Example: MR 255388 is one of these found by a failing feature spec, and finding them one at a time does not scale.
Rollout: missing half now, unused half after the cleanups
Running the rule over app/ and ee/ found no missing registration and about 110 files with an unused one. The missing half is what prevents Vue 3 breakage, so it ships now for every entry. The unused half is a code-quality concern and waits:
-
reportUnused: falseis set on the entries that still have unused registrations (glFeatureFlagsMixin,glAbilitiesMixin,timeagoMixin,InternalEvents,Tracking). The four entries with zero unused registrations keep both halves on, so nothing the old rules enforced is lost. -
The cleanup MRs below remove the existing unused registrations. They were produced with an earlier autofix of this rule, since removed to keep the rule small, and can merge in any order.
-
A last MR removes the
reportUnused: falseflags once the cleanups are in. No todo list is added anywhere. -
!256666 (merged) (Remove unused glFeatureFlagsMixin registrations in CE)
-
!256677 (merged) (Remove unused glFeatureFlagsMixin registrations in EE)
-
!256669 (closed) (Remove unused mixin and directive registrations)
What moves where
- Deleted
vue3-gl-slots-mixin-pairing,vue3-gl-listeners-mixin-pairingandgl-toast-mixinwith their specs. Their behaviour is now three table entries. - The
$root.$toastcheck fromgl-toast-mixinmoves to a newlocal-rules/no-root-toastrule, unchanged. It is a different concern from pairing. - The rule has no autofix. The
gl-toast-mixinfixer is dropped rather than generalised: the cleanup below was generated with an autofix once, and from here on a violation is one import and one array entry to edit by hand. Shared helpers live intooling/eslint-config/eslint-local-rules/utils/component_options.mjs.
Known limit
The rule only sees same-file evidence. A mixin arriving through another mixin or extends, or a member reached through $options or this[name], needs an eslint-disable with a comment. No such case exists in app/ or ee/ today (which is a good thing! That would be a lot of spaghetti!).
How to set up and validate locally
-
Lint the frontend. No
local-rules/vue-mixin-pairingerror is reported:./node_modules/.bin/eslint 'app/assets/javascripts/**/*.vue' 'ee/app/assets/javascripts/**/*.vue' -
Remove
mixins: [GlToastMixin]from any component that callsthis.$toastand lint the file. The rule reports$toast. Put it back and the error goes away.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.
References
- Resolves #629073 (closed)
- Companion directive rule, stacked on this MR: !256699 (closed)
- Motivating bug, a missing registration hidden by a Jest mock: !255388 (merged)
- Line-count justification for replacing three rules with one: !256664 (comment 3877391319)
- Cleanup MRs that remove the existing unused mixin registrations, produced with an earlier autofix of this rule:
- !256666 (merged) (Remove unused glFeatureFlagsMixin registrations in CE)
- !256677 (merged) (Remove unused glFeatureFlagsMixin registrations in EE)
- !256669 (closed) (Remove unused mixin and directive registrations)