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

Merged
merged 20 commits into from
Nov 11, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add tests
  • Loading branch information
Thomasan1999 committed Nov 2, 2024
commit 297f2e9e2ce3131f0da216716cf7891062129bd9
170 changes: 127 additions & 43 deletions tests/lib/rules/prefer-use-template-ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,81 @@ tester.run('prefer-use-template-ref', rule, {
}
</script>
`
},
{
filename: 'non-template-ref.vue',
code: `
<template>
<div>
<ul>
<li v-for="food in foods" :key="food">{{food}}</li>
</ul>
</div>
</template>
<script setup>
import { ref } from 'vue';
const foods = ref(['Spaghetti', 'Pizza', 'Cake']);
</script>
`
},
{
filename: 'counter.js',
code: `
import { ref } from 'vue';
const counter = ref(0);
const names = ref(new Set());
function incrementCounter() {
counter.value++;
return counter.value;
}
function storeName(name) {
names.value.add(name)
}
`
},
{
filename: 'setup-function.vue',
code: `
<template>
<p>Button clicked {{counter}} times.</p>
<button ref="button" @click="counter++">Click</button>
</template>
<script>
import { ref, useTemplateRef } from 'vue';
export default {
name: 'Counter',
setup() {
const counter = ref(0);
const button = useTemplateRef('button');
}
}
</script>
`
},
{
filename: 'options-api.vue',
code: `
<template>
<label ref="label">
Name:
<input v-model="name" />
</label>
<p ref="textRef">Text</p>
<button
</template>
<script>
import { useTemplateRef } from 'vue';
export default {
name: 'NameRow',
data() {
return {
label: useTemplateRef('label'),
name: ref('')
}
}
}
</script>
`
}
],
invalid: [
Expand All @@ -76,15 +151,6 @@ tester.run('prefer-use-template-ref', rule, {
const root = ref();
</script>
`,
output: `
<template>
<div ref="root"/>
</template>
<script setup>
import { useTemplateRef } from 'vue';
const root = useTemplateRef('root');
</script>
`,
errors: [
{
messageId: 'preferUseTemplateRef',
Expand All @@ -106,17 +172,6 @@ tester.run('prefer-use-template-ref', rule, {
const link = ref();
</script>
`,
output: `
<template>
<button ref="button">Content</button>
<a href="" ref="link">Link</a>
</template>
<script setup>
import { ref } from 'vue';
const buttonRef = ref();
const link = useTemplateRef('link');
</script>
`,
errors: [
{
messageId: 'preferUseTemplateRef',
Expand All @@ -138,17 +193,6 @@ tester.run('prefer-use-template-ref', rule, {
const link = ref();
</script>
`,
output: `
<template>
<h1 ref="heading">Heading</h1>
<a href="" ref="link">Link</a>
</template>
<script setup>
import { useTemplateRef } from 'vue';
const heading = useTemplateRef('heading');
const link = useTemplateRef('link');
</script>
`,
errors: [
{
messageId: 'preferUseTemplateRef',
Expand Down Expand Up @@ -181,20 +225,60 @@ tester.run('prefer-use-template-ref', rule, {
}
</script>
`,
output: `
errors: [
{
messageId: 'preferUseTemplateRef',
line: 14,
column: 33
}
]
},
{
filename: 'setup-function-only-refs.vue',
code: `
<template>
<div>
<ul>
<li ref="firstListItem">Morning</li>
<li>Afternoon</li>
<li>Evening</li>
</ul>
</div>
<p>Button clicked {{counter}} times.</p>
<button ref="button">Click</button>
</template>
<script setup>
import { useTemplateRef } from 'vue';
function getFirstListItemElement() {
const firstListItem = useTemplateRef('firstListItem');
<script>
import { ref } from 'vue';
export default {
name: 'Counter',
setup() {
const counter = ref(0);
const button = ref();
}
}
</script>
`,
errors: [
{
messageId: 'preferUseTemplateRef',
line: 12,
column: 28
}
]
},
{
filename: 'options-api-only-refs.vue',
code: `
<template>
<label ref="label">
Name:
<input v-model="name" />
</label>
<button
</template>
<script>
import { ref } from 'vue';
export default {
name: 'NameRow',
data() {
return {
label: ref(),
name: ref('')
}
}
}
</script>
`,
Expand Down