Skip to content

Commit 8f9ab8e

Browse files
committed
fix(prefer-use-template-ref): add support for shallowRef
1 parent 16c8778 commit 8f9ab8e

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

docs/rules/prefer-use-template-ref.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,32 @@
22
pageClass: rule-details
33
sidebarDepth: 0
44
title: vue/prefer-use-template-ref
5-
description: require using `useTemplateRef` instead of `ref` for template refs
5+
description: require using `useTemplateRef` instead of `ref/shallowRef` for template refs
66
since: v9.31.0
77
---
88

99
# vue/prefer-use-template-ref
1010

11-
> require using `useTemplateRef` instead of `ref` for template refs
11+
> require using `useTemplateRef` instead of `ref/shallowRef` for template refs
1212
1313
## :book: Rule Details
1414

1515
Vue 3.5 introduced a new way of obtaining template refs via
1616
the [`useTemplateRef()`](https://vuejs.org/guide/essentials/template-refs.html#accessing-the-refs) API.
1717

18-
This rule enforces using the new `useTemplateRef` function instead of `ref` for template refs.
18+
This rule enforces using the new `useTemplateRef` function instead of `ref/shallowRef` for template refs.
1919

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

2222
```vue
2323
<template>
2424
<button ref="submitRef">Submit</button>
25+
<button ref="cancelRef">Cancel</button>
2526
<button ref="closeRef">Close</button>
2627
</template>
2728
2829
<script setup>
29-
import { ref, useTemplateRef } from 'vue';
30+
import { ref, shallowRef, useTemplateRef } from 'vue';
3031
3132
/* ✓ GOOD */
3233
const submitRef = useTemplateRef('submitRef');
@@ -35,6 +36,7 @@ This rule enforces using the new `useTemplateRef` function instead of `ref` for
3536
3637
/* ✗ BAD */
3738
const closeRef = ref();
39+
const cancelRef = shallowRef();
3840
</script>
3941
```
4042

@@ -47,14 +49,16 @@ This rule skips `ref` template function refs as these should be used to allow cu
4749

4850
```vue
4951
<template>
50-
<button :ref="ref => button = ref">Content</button>
52+
<button :ref="ref => submitRef = ref">Submit</button>
53+
<button :ref="ref => cancelRef = ref">Cancel</button>
5154
</template>
5255
5356
<script setup>
54-
import { ref } from 'vue';
57+
import { ref, shallowRef } from 'vue';
5558
5659
/* ✓ GOOD */
57-
const button = ref();
60+
const submitRef = ref();
61+
const cancelRef = shallowRef();
5862
</script>
5963
```
6064

lib/rules/prefer-use-template-ref.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ const utils = require('../utils')
88

99
/** @param expression {Expression | null} */
1010
function expressionIsRef(expression) {
11-
// @ts-ignore
12-
return expression?.callee?.name === 'ref'
11+
return (
12+
// @ts-ignore
13+
expression?.callee?.name === 'ref' ||
14+
// @ts-ignore
15+
expression?.callee?.name === 'shallowRef'
16+
)
1317
}
1418

1519
/** @type {import("eslint").Rule.RuleModule} */
@@ -18,13 +22,13 @@ module.exports = {
1822
type: 'suggestion',
1923
docs: {
2024
description:
21-
'require using `useTemplateRef` instead of `ref` for template refs',
25+
'require using `useTemplateRef` instead of `ref/shallowRef` for template refs',
2226
categories: undefined,
2327
url: 'https://eslint.vuejs.org/rules/prefer-use-template-ref.html'
2428
},
2529
schema: [],
2630
messages: {
27-
preferUseTemplateRef: "Replace 'ref' with 'useTemplateRef'."
31+
preferUseTemplateRef: "Replace 'ref/shallowRef' with 'useTemplateRef'."
2832
}
2933
},
3034
/** @param {RuleContext} context */
@@ -46,7 +50,10 @@ module.exports = {
4650
context,
4751
{
4852
'VAttribute[directive=false]'(node) {
49-
if (node.key.name === 'ref' && node.value?.value) {
53+
if (
54+
(node.key.name === 'ref' || node.key.name === 'shallowRef') &&
55+
node.value?.value
56+
) {
5057
templateRefs.add(node.value.value)
5158
}
5259
}

tests/lib/rules/prefer-use-template-ref.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,25 @@ tester.run('prefer-use-template-ref', rule, {
318318
column: 28
319319
}
320320
]
321+
},
322+
{
323+
filename: 'single-shallowRef.vue',
324+
code: `
325+
<template>
326+
<div ref="root"/>
327+
</template>
328+
<script setup>
329+
import { shallowRef } from 'vue';
330+
const root = shallowRef();
331+
</script>
332+
`,
333+
errors: [
334+
{
335+
messageId: 'preferUseTemplateRef',
336+
line: 7,
337+
column: 22
338+
}
339+
]
321340
}
322341
]
323342
})

0 commit comments

Comments
 (0)