Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add prefer-use-template-ref rule #2554

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +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` for template refs | | :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: |
Expand Down
71 changes: 71 additions & 0 deletions docs/rules/prefer-use-template-ref.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/prefer-use-template-ref
description: require using `useTemplateRef` instead of `ref` for template refs
---

# vue/prefer-use-template-ref

> require using `useTemplateRef` instead of `ref` for template refs

- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> _**This rule has not been released yet.**_ </badge>

## :book: Rule Details

Vue 3.5 introduced a new way of obtaining template refs via
FloEdelmann marked this conversation as resolved.
Show resolved Hide resolved
the [`useTemplateRef()`](https://vuejs.org/guide/essentials/template-refs.html#accessing-the-refs) API.

This rule enforces using the new `useTemplateRef` function instead of `ref` for template refs.

<eslint-code-block :rules="{'vue/prefer-use-template-ref': ['error']}">

```vue
<template>
<button ref="submitRef">Submit</button>
<button ref="closeRef">Close</button>
</template>

<script setup>
import { ref, useTemplateRef } from 'vue';

/* ✓ GOOD */
const submitRef = useTemplateRef('submitRef');
const submitButton = useTemplateRef('submitRef');
const closeButton = useTemplateRef('closeRef');

/* ✗ BAD */
const closeRef = ref();
</script>
```

</eslint-code-block>

This rule skips `ref` template function refs as these should be used to allow custom implementation of storing `ref`. If you prefer
`useTemplateRef`, you have to change the value of the template `ref` to a string.

<eslint-code-block :rules="{'vue/prefer-use-template-ref': ['error']}">

```vue
<template>
<button :ref="ref => button = ref">Content</button>
</template>

<script setup>
import { ref } from 'vue';

/* ✓ GOOD */
const button = ref();
</script>
```

</eslint-code-block>

## :wrench: Options

Nothing.

## :mag: Implementation

- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/prefer-use-template-ref.js)
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/prefer-use-template-ref.js)
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ const plugin = {
'prefer-separate-static-class': require('./rules/prefer-separate-static-class'),
'prefer-template': require('./rules/prefer-template'),
'prefer-true-attribute-shorthand': require('./rules/prefer-true-attribute-shorthand'),
'prefer-use-template-ref': require('./rules/prefer-use-template-ref'),
'prop-name-casing': require('./rules/prop-name-casing'),
'quote-props': require('./rules/quote-props'),
'require-component-is': require('./rules/require-component-is'),
Expand Down
89 changes: 89 additions & 0 deletions lib/rules/prefer-use-template-ref.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* @author Thomasan1999
* See LICENSE file in root directory for full license.
*/
'use strict'

const utils = require('../utils')

/** @param expression {Expression | null} */
function expressionIsRef(expression) {
// @ts-ignore
return expression?.callee?.name === 'ref'
}

/** @type {import("eslint").Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description:
'require using `useTemplateRef` instead of `ref` for template refs',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/prefer-use-template-ref.html'
},
schema: [],
messages: {
preferUseTemplateRef: "Replace 'ref' with 'useTemplateRef'."
}
},
/** @param {RuleContext} context */
create(context) {
/** @type Set<string> */
const templateRefs = new Set()

/**
* @typedef ScriptRef
* @type {{node: Expression, ref: string}}
*/

/**
* @type ScriptRef[] */
const scriptRefs = []

return utils.compositingVisitors(
utils.defineTemplateBodyVisitor(
context,
{
'VAttribute[directive=false]'(node) {
if (node.key.name === 'ref' && node.value?.value) {
templateRefs.add(node.value.value)
}
}
},
{
VariableDeclarator(declarator) {
if (!expressionIsRef(declarator.init)) {
return
}

scriptRefs.push({
// @ts-ignore
node: declarator.init,
// @ts-ignore
ref: declarator.id.name
})
}
}
),
{
'Program:exit'() {
for (const templateRef of templateRefs) {
const scriptRef = scriptRefs.find(
(scriptRef) => scriptRef.ref === templateRef
)

if (!scriptRef) {
continue
}

context.report({
node: scriptRef.node,
messageId: 'preferUseTemplateRef'
})
}
}
}
)
}
}
Loading