-
-
Notifications
You must be signed in to change notification settings - Fork 680
feat: add prefer-use-template-ref rule #2554
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
21dfb40
feat: add prefer-use-template-ref rule
Thomasan1999 d8f72ba
Merge branch 'vuejs:master' into master
Thomasan1999 dc47a6f
Update lib/rules/prefer-use-template-ref.js
Thomasan1999 4e04930
Update docs/rules/prefer-use-template-ref.md
Thomasan1999 defad15
Update docs/rules/prefer-use-template-ref.md
Thomasan1999 3468fa3
Update docs/rules/prefer-use-template-ref.md
Thomasan1999 efebb3c
update description
Thomasan1999 a7f22bd
remove empty lines
Thomasan1999 8e54afd
add 'setup' to script tags
Thomasan1999 8e1196a
fix imports in tests
Thomasan1999 329e9ac
remove fix function
Thomasan1999 297f2e9
add tests
Thomasan1999 1deb14e
add template ref function to docs
Thomasan1999 414ee6c
support refs in data function in Options API
Thomasan1999 16824b6
Merge branch 'vuejs:master' into master
Thomasan1999 a08a273
fix wrong column number in invalid test case
Thomasan1999 12e706a
improve docs
Thomasan1999 476c18a
align code blocks pushing to 'scriptRefs'
Thomasan1999 b598f30
remove extra trailing space
Thomasan1999 b56d81e
revert traversing data function, it does not use assigning template r…
Thomasan1999 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/prefer-use-template-ref | ||
description: require using `useTemplateRef` over `ref` for template refs | ||
--- | ||
|
||
# vue/prefer-use-template-ref | ||
|
||
> require using `useTemplateRef` over `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> | ||
- :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 | ||
the [useTemplateRef()](https://vuejs.org/guide/essentials/template-refs.html#accessing-the-refs) API. | ||
Thomasan1999 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
This rule enforces using the new `useTemplateRef` function over `ref` for template refs. | ||
Thomasan1999 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
<eslint-code-block fix :rules="{'vue/prefer-use-template-ref': ['error']}"> | ||
|
||
```vue | ||
|
||
<template> | ||
<div ref="divRef"></div> | ||
<button ref="submitter">Submit</button> | ||
</template> | ||
|
||
<script> | ||
import { ref, useTemplateRef } from 'vue'; | ||
|
||
/* ✓ GOOD */ | ||
const divRef = useTemplateRef('divRef'); | ||
const div = useTemplateRef('divRef'); | ||
const loremIpsum = useTemplateRef('divRef'); | ||
const submitButton = useTemplateRef('submitter'); | ||
/* ✗ BAD */ | ||
const divRef = ref(); | ||
const submitter = ref(); | ||
</script> | ||
``` | ||
Thomasan1999 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
</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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* @author Thomasan1999 | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
|
||
/** @type {import("eslint").Rule.RuleModule} */ | ||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: | ||
'require using `useTemplateRef` over `ref` for template refs', | ||
Thomasan1999 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/prefer-use-template-ref.html' | ||
}, | ||
fixable: 'code', | ||
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) { | ||
// @ts-ignore | ||
if (declarator.init?.callee?.name !== 'ref') { | ||
return | ||
} | ||
|
||
scriptRefs.push({ | ||
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', | ||
fix(fixer) { | ||
return fixer.replaceText( | ||
scriptRef.node, | ||
`useTemplateRef('${scriptRef.ref}')` | ||
FloEdelmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
) | ||
} | ||
} |
FloEdelmann marked this conversation as resolved.
Show resolved
Hide resolved
FloEdelmann marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,232 @@ | ||
/** | ||
* @author Thomasan1999 | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const RuleTester = require('../../eslint-compat').RuleTester | ||
const rule = require('../../../lib/rules/prefer-use-template-ref') | ||
|
||
const tester = new RuleTester({ | ||
languageOptions: { | ||
parser: require('vue-eslint-parser'), | ||
ecmaVersion: 2020, | ||
sourceType: 'module' | ||
} | ||
}) | ||
|
||
tester.run('prefer-use-template-ref', rule, { | ||
valid: [ | ||
{ | ||
filename: 'single-use-template-ref.vue', | ||
code: ` | ||
<template> | ||
<div ref="root" /> | ||
</template> | ||
|
||
<script> | ||
FloEdelmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import { useTemplateRef } from 'vue'; | ||
|
||
const root = useTemplateRef('root'); | ||
</script> | ||
` | ||
}, | ||
{ | ||
filename: 'multiple-use-template-refs.vue', | ||
code: ` | ||
<template> | ||
<button ref="button">Content</button> | ||
<a href="" ref="link">Link</a> | ||
</template> | ||
|
||
<script> | ||
import { useTemplateRef } from 'vue'; | ||
|
||
const buttonRef = useTemplateRef('button'); | ||
const link = useTemplateRef('link'); | ||
</script> | ||
` | ||
}, | ||
{ | ||
filename: 'use-template-ref-in-block.vue', | ||
code: ` | ||
<template> | ||
<div> | ||
<ul> | ||
<li ref="firstListItem">Morning</li> | ||
<li>Afternoon</li> | ||
<li>Evening</li> | ||
</ul> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { useTemplateRef } from 'vue'; | ||
|
||
function getFirstListItemElement() { | ||
const firstListItem = useTemplateRef('firstListItem'); | ||
} | ||
</script> | ||
` | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
filename: 'single-ref.vue', | ||
code: ` | ||
<template> | ||
<div ref="root"/> | ||
</template> | ||
|
||
<script> | ||
import { ref } from 'vue'; | ||
|
||
const root = ref(); | ||
</script> | ||
FloEdelmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
`, | ||
output: ` | ||
<template> | ||
<div ref="root"/> | ||
</template> | ||
|
||
<script> | ||
import { ref } from 'vue'; | ||
|
||
const root = useTemplateRef('root'); | ||
</script> | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'preferUseTemplateRef', | ||
line: 9, | ||
column: 22 | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'one-ref-unused-in-script.vue', | ||
code: ` | ||
<template> | ||
<button ref="button">Content</button> | ||
<a href="" ref="link">Link</a> | ||
</template> | ||
|
||
<script> | ||
import { ref } from 'vue'; | ||
|
||
const buttonRef = ref(); | ||
const link = ref(); | ||
</script> | ||
`, | ||
output: ` | ||
<template> | ||
<button ref="button">Content</button> | ||
<a href="" ref="link">Link</a> | ||
</template> | ||
|
||
<script> | ||
import { ref } from 'vue'; | ||
|
||
const buttonRef = ref(); | ||
const link = useTemplateRef('link'); | ||
</script> | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'preferUseTemplateRef', | ||
line: 11, | ||
column: 22 | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'multiple-refs.vue', | ||
code: ` | ||
<template> | ||
<h1 ref="heading">Heading</h1> | ||
<a href="" ref="link">Link</a> | ||
</template> | ||
|
||
<script> | ||
import { ref } from 'vue'; | ||
|
||
const heading = ref(); | ||
const link = ref(); | ||
</script> | ||
`, | ||
output: ` | ||
<template> | ||
<h1 ref="heading">Heading</h1> | ||
<a href="" ref="link">Link</a> | ||
</template> | ||
|
||
<script> | ||
import { ref } from 'vue'; | ||
|
||
const heading = useTemplateRef('heading'); | ||
const link = useTemplateRef('link'); | ||
</script> | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'preferUseTemplateRef', | ||
line: 10, | ||
column: 25 | ||
}, | ||
{ | ||
messageId: 'preferUseTemplateRef', | ||
line: 11, | ||
column: 22 | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'ref-in-block.vue', | ||
code: ` | ||
<template> | ||
<div> | ||
<ul> | ||
<li ref="firstListItem">Morning</li> | ||
<li>Afternoon</li> | ||
<li>Evening</li> | ||
</ul> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { ref } from 'vue'; | ||
|
||
function getFirstListItemElement() { | ||
const firstListItem = ref(); | ||
} | ||
</script> | ||
`, | ||
output: ` | ||
<template> | ||
<div> | ||
<ul> | ||
<li ref="firstListItem">Morning</li> | ||
<li>Afternoon</li> | ||
<li>Evening</li> | ||
</ul> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { ref } from 'vue'; | ||
|
||
function getFirstListItemElement() { | ||
const firstListItem = useTemplateRef('firstListItem'); | ||
} | ||
</script> | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'preferUseTemplateRef', | ||
line: 16, | ||
column: 33 | ||
} | ||
] | ||
} | ||
] | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.