Draft: Add vue-directive-pairing ESLint rule
What does this MR do and why?
Adds a local ESLint rule, local-rules/vue-directive-pairing, that keeps every custom directive used in a Vue template paired with a registration in the component's directives option, both ways.
Uses the small options-object helper added in !256664 (merged).
Why now
!255388 (merged) fixed a component that used v-gl-tooltip without registering GlTooltipDirective. Vue 2 logged Failed to resolve directive and the tooltip never rendered. The Jest spec passed because it used createMockDirective. Under Vue 3, where apps do not share global registrations, every such gap is a runtime failure. The rule turns it into a lint error on every MR.
The rule
- Needs no table for detection. Every custom
v-xin the template must match adirectiveskey that Vue resolves to it (x, camelCase or PascalCase), and every key must be used. - Covers local directives such as
SafeHtmlandvalidationthe same way as@gitlab/uiones. - No options beyond
reportUnused. Nothing inapp/oree/registers a directive globally, spreads adirectivesobject or uses a computed key, so the rule does not handle those cases.vue/no-undef-directivesin eslint-plugin-vue 10.7 takes the same approach. - No autofix. The cleanup below was generated with an autofix once; from here on a violation is one import and one
directiveskey to edit by hand.
Rollout: missing half now, unused half after the cleanups
Running the rule over app/ and ee/ found no missing registration and about 130 files with an unused one. Cleaning those up takes longer and is not critical, so this MR does not wait for it:
-
reportUnused: falseis set on the rule. The missing half is on. -
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: falseflag once the cleanups are in. No todo list is added anywhere. -
!256665 (closed) (Remove unused GlTooltip directive registrations)
-
!256667 (merged) (Remove unused SafeHtml directive registrations)
-
!256668 (merged) (Remove unused GlModal directive registrations)
-
!256669 (closed) (Remove unused mixin and directive registrations)
Same-file evidence only
The rule only sees the component file. Two DAST forms use v-validation, which dastProfileFormMixin() registered. The registration moves out of the mixin and into each form's directives option, so the rule sees it and no eslint-disable is needed.
How to set up and validate locally
-
Run the rule specs:
yarn vitest:eslint --run -
Lint the frontend. No
local-rules/vue-directive-pairingerror is reported:./node_modules/.bin/eslint 'app/assets/javascripts/**/*.vue' 'ee/app/assets/javascripts/**/*.vue' -
Reproduce the motivating bug. Remove the
directivesoption fromapp/assets/javascripts/personal_access_tokens/components/personal_access_token_status_badge.vueand lint the file. The rule reportsv-gl-tooltip. 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)
- Uses the options-object helper from !256664 (merged) (merged)
- Upstream equivalent of the missing half,
vue/no-undef-directives(eslint-plugin-vue 10.7): https://eslint.vuejs.org/rules/no-undef-directives - eslint-plugin-vue 10 comes to the monolith with the
@gitlab/eslint-pluginflat-config chain, published at stage 4: gitlab-org/frontend/eslint-plugin!172 (merged) (stage 3, open), gitlab-org/frontend/eslint-plugin!173 (merged) (stage 4, cuts the release); tracked in gitlab-org/frontend/eslint-plugin#95 (closed) - Motivating bug: !255388 (merged)
- Cleanup MRs that remove the existing unused directive registrations, produced with an earlier autofix of this rule:
- !256665 (closed) (Remove unused GlTooltip directive registrations)
- !256667 (merged) (Remove unused SafeHtml directive registrations)
- !256668 (merged) (Remove unused GlModal directive registrations)
- !256669 (closed) (Remove unused mixin and directive registrations)