Skip to content

Commit 39f5a64

Browse files
committed
fix(prefer-use-template-ref): should check only root-level variables (rebase to vuejs#2608)
1 parent a4e2a2e commit 39f5a64

File tree

2 files changed

+6
-36
lines changed

2 files changed

+6
-36
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ function getScriptRefsFromSetupFunction(body) {
3636
const variableDeclarators = variableDeclarations.map(
3737
(declaration) => declaration.declarations[0]
3838
)
39-
const refDeclarators = variableDeclarators.filter(
39+
const refDeclarators = variableDeclarators.filter((declarator) =>
4040
// @ts-ignore
41-
(declarator) => declarator.init?.callee?.name === 'ref'
41+
['ref', 'shallowRef'].includes(declarator.init?.callee?.name)
4242
)
4343

4444
return refDeclarators.map(convertDeclaratorToScriptRef)

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

+4-34
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,11 @@ tester.run('prefer-use-template-ref', rule, {
211211
</div>
212212
</template>
213213
<script setup>
214-
import { ref } from 'vue';
214+
import { ref, shallowRef } from 'vue';
215215
function getFirstListItemElement() {
216216
const firstListItem = ref();
217217
const nestedCallback = () => {
218-
const second = ref();
218+
const second = shallowRef();
219219
console.log(second);
220220
}
221221
nestedCallback();
@@ -236,12 +236,12 @@ tester.run('prefer-use-template-ref', rule, {
236236
</div>
237237
</template>
238238
<script>
239-
import { ref } from 'vue';
239+
import { ref, shallowRef } from 'vue';
240240
export default {
241241
name: 'ComponentWithRefInBlock',
242242
setup() {
243243
function getFirstListItemElement() {
244-
const firstListItem = ref();
244+
const firstListItem = shallowRef();
245245
const nestedCallback = () => {
246246
const second = ref();
247247
console.log(second);
@@ -333,36 +333,6 @@ tester.run('prefer-use-template-ref', rule, {
333333
}
334334
]
335335
},
336-
{
337-
filename: 'ref-in-block.vue',
338-
code: `
339-
<template>
340-
<div>
341-
<ul>
342-
<li ref="firstListItem">Morning</li>
343-
<li>Afternoon</li>
344-
<li>Evening</li>
345-
</ul>
346-
</div>
347-
</template>
348-
<script setup>
349-
import { ref } from 'vue';
350-
function getFirstListItemElement() {
351-
const firstListItem = ref();
352-
}
353-
</script>
354-
`,
355-
errors: [
356-
{
357-
messageId: 'preferUseTemplateRef',
358-
data: {
359-
name: 'ref'
360-
},
361-
line: 14,
362-
column: 33
363-
}
364-
]
365-
},
366336
{
367337
filename: 'setup-function-only-refs.vue',
368338
code: `

0 commit comments

Comments
 (0)