From ac5aa922cdc358acb9c8664d94cd91965f89efce Mon Sep 17 00:00:00 2001 From: Thomasan1999 Date: Sat, 30 Nov 2024 12:04:25 +0100 Subject: [PATCH 1/7] feat(prefer-use-template-ref): add support for fix option --- docs/rules/index.md | 2 +- docs/rules/prefer-use-template-ref.md | 6 +- lib/rules/prefer-use-template-ref.js | 82 +++++++++++ tests/lib/rules/prefer-use-template-ref.js | 152 +++++++++++++++++++++ 4 files changed, 239 insertions(+), 3 deletions(-) diff --git a/docs/rules/index.md b/docs/rules/index.md index 074f4bd46..727654f63 100644 --- a/docs/rules/index.md +++ b/docs/rules/index.md @@ -270,7 +270,7 @@ For example: | [vue/prefer-prop-type-boolean-first](./prefer-prop-type-boolean-first.md) | enforce `Boolean` comes first in component prop types | :bulb: | :warning: | | [vue/prefer-separate-static-class](./prefer-separate-static-class.md) | require static class names in template to be in a separate `class` attribute | :wrench: | :hammer: | | [vue/prefer-true-attribute-shorthand](./prefer-true-attribute-shorthand.md) | require shorthand form attribute when `v-bind` value is `true` | :bulb: | :hammer: | -| [vue/prefer-use-template-ref](./prefer-use-template-ref.md) | require using `useTemplateRef` instead of `ref`/`shallowRef` for template refs | | :hammer: | +| [vue/prefer-use-template-ref](./prefer-use-template-ref.md) | require using `useTemplateRef` instead of `ref`/`shallowRef` for template refs | :wrench: | :hammer: | | [vue/require-default-export](./require-default-export.md) | require components to be the default export | | :warning: | | [vue/require-direct-export](./require-direct-export.md) | require the component to be directly exported | | :hammer: | | [vue/require-emit-validator](./require-emit-validator.md) | require type definitions in emits | :bulb: | :hammer: | diff --git a/docs/rules/prefer-use-template-ref.md b/docs/rules/prefer-use-template-ref.md index 1b1b40385..ed5699ee8 100644 --- a/docs/rules/prefer-use-template-ref.md +++ b/docs/rules/prefer-use-template-ref.md @@ -10,6 +10,8 @@ since: v9.31.0 > require using `useTemplateRef` instead of `ref`/`shallowRef` for template refs +- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule. + ## :book: Rule Details Vue 3.5 introduced a new way of obtaining template refs via @@ -17,7 +19,7 @@ the [`useTemplateRef()`](https://vuejs.org/guide/essentials/template-refs.html#a This rule enforces using the new `useTemplateRef` function instead of `ref`/`shallowRef` for template refs. - + ```vue @@ -385,7 +385,7 @@ tester.run('prefer-use-template-ref', rule, { Link @@ -433,7 +433,7 @@ tester.run('prefer-use-template-ref', rule, { `, @@ -500,6 +500,19 @@ tester.run('prefer-use-template-ref', rule, { } `, + output: ` + + + `, errors: [ { messageId: 'preferUseTemplateRef', @@ -523,6 +536,20 @@ tester.run('prefer-use-template-ref', rule, { const root = ref() + + `, + output: ` + + + + @@ -554,6 +581,20 @@ tester.run('prefer-use-template-ref', rule, { const root = ref() `, + output: ` + + + + + + `, errors: [ { messageId: 'preferUseTemplateRef', @@ -593,7 +634,7 @@ tester.run('prefer-use-template-ref', rule, { + `, + output: ` + + + `, + languageOptions: { + parserOptions: { + parser: require.resolve('@typescript-eslint/parser') + } + }, + errors: [ + { + messageId: 'preferUseTemplateRef', + data: { + name: 'ref' + }, + line: 11, + column: 28 + } + ] } ] }) From e1ea60ff584c680e3c982921bc87581399c03d30 Mon Sep 17 00:00:00 2001 From: Thomasan1999 Date: Tue, 10 Jun 2025 20:24:46 +0200 Subject: [PATCH 6/7] incorporating PR feedback #2 --- docs/rules/index.md | 164 ++++++++++----------- docs/rules/prefer-use-template-ref.md | 2 +- lib/rules/prefer-use-template-ref.js | 17 ++- tests/lib/rules/prefer-use-template-ref.js | 2 +- 4 files changed, 98 insertions(+), 87 deletions(-) diff --git a/docs/rules/index.md b/docs/rules/index.md index 456d31007..70691eae7 100644 --- a/docs/rules/index.md +++ b/docs/rules/index.md @@ -207,90 +207,90 @@ For example: -| Rule ID | Description | | | -|:--------|:------------|:--------------:|:--:| -| [vue/block-lang] | disallow use other than available `lang` | | :hammer: | -| [vue/block-tag-newline] | enforce line breaks after opening and before closing block-level tags | :wrench: | :lipstick: | -| [vue/component-api-style] | enforce component API style | | :hammer: | -| [vue/component-name-in-template-casing] | enforce specific casing for the component naming style in template | :wrench: | :hammer: | +| Rule ID | Description | | | +|:--------|:------------|:--:|:--:| +| [vue/block-lang] | disallow use other than available `lang` | | :hammer: | +| [vue/block-tag-newline] | enforce line breaks after opening and before closing block-level tags | :wrench: | :lipstick: | +| [vue/component-api-style] | enforce component API style | | :hammer: | +| [vue/component-name-in-template-casing] | enforce specific casing for the component naming style in template | :wrench: | :hammer: | | [vue/component-options-name-casing] | enforce the casing of component name in `components` options | :wrench::bulb: | :hammer: | -| [vue/custom-event-name-casing] | enforce specific casing for custom event name | | :hammer: | -| [vue/define-emits-declaration] | enforce declaration style of `defineEmits` | | :hammer: | +| [vue/custom-event-name-casing] | enforce specific casing for custom event name | | :hammer: | +| [vue/define-emits-declaration] | enforce declaration style of `defineEmits` | | :hammer: | | [vue/define-macros-order] | enforce order of compiler macros (`defineProps`, `defineEmits`, etc.) | :wrench::bulb: | :lipstick: | -| [vue/define-props-declaration] | enforce declaration style of `defineProps` | | :hammer: | -| [vue/define-props-destructuring] | enforce consistent style for props destructuring | | :hammer: | -| [vue/enforce-style-attribute] | enforce or forbid the use of the `scoped` and `module` attributes in SFC top level style tags | | :hammer: | -| [vue/html-button-has-type] | disallow usage of button without an explicit type attribute | | :hammer: | -| [vue/html-comment-content-newline] | enforce unified line break in HTML comments | :wrench: | :lipstick: | -| [vue/html-comment-content-spacing] | enforce unified spacing in HTML comments | :wrench: | :lipstick: | -| [vue/html-comment-indent] | enforce consistent indentation in HTML comments | :wrench: | :lipstick: | -| [vue/match-component-file-name] | require component name property to match its file name | :bulb: | :hammer: | -| [vue/match-component-import-name] | require the registered component name to match the imported component name | | :warning: | -| [vue/max-lines-per-block] | enforce maximum number of lines in Vue SFC blocks | | :warning: | -| [vue/max-props] | enforce maximum number of props in Vue component | | :warning: | -| [vue/max-template-depth] | enforce maximum depth of template | | :warning: | -| [vue/new-line-between-multi-line-property] | enforce new lines between multi-line properties in Vue components | :wrench: | :lipstick: | -| [vue/next-tick-style] | enforce Promise or callback style in `nextTick` | :wrench: | :hammer: | -| [vue/no-bare-strings-in-template] | disallow the use of bare strings in `