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

Update vue/no-unused-refs rule to support setup() and <script setup> #1556

Merged
merged 1 commit into from Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 54 additions & 15 deletions lib/rules/no-unused-refs.js
Expand Up @@ -180,25 +180,64 @@ module.exports = {
reportUnusedRefs()
}
},
{
Identifier(id) {
if (hasUnknown) {
return
}
if (id.name !== '$refs') {
return
utils.compositingVisitors(
utils.isScriptSetup(context)
? {
Program() {
const globalScope =
context.getSourceCode().scopeManager.globalScope
if (!globalScope) {
return
}
for (const variable of globalScope.variables) {
if (variable.defs.length > 0) {
usedRefs.add(variable.name)
}
}
const moduleScope = globalScope.childScopes.find(
(scope) => scope.type === 'module'
)
if (!moduleScope) {
return
}
for (const variable of moduleScope.variables) {
if (variable.defs.length > 0) {
usedRefs.add(variable.name)
}
}
}
}
: {},
utils.defineVueVisitor(context, {
onVueObjectEnter(node) {
for (const prop of utils.iterateProperties(
node,
new Set(['setup'])
)) {
usedRefs.add(prop.name)
}
}
/** @type {Identifier | MemberExpression} */
let refsNode = id
if (id.parent.type === 'MemberExpression') {
if (id.parent.property === id) {
// `this.$refs.foo`
refsNode = id.parent
}),
{
Identifier(id) {
if (hasUnknown) {
return
}
if (id.name !== '$refs') {
return
}
/** @type {Identifier | MemberExpression} */
let refsNode = id
if (id.parent.type === 'MemberExpression') {
if (id.parent.property === id) {
// `this.$refs.foo`
refsNode = id.parent
}
}
extractUsedForPattern(refsNode)
}
extractUsedForPattern(refsNode)
}
}
)
)
}
}
31 changes: 31 additions & 0 deletions tests/lib/rules/no-unused-refs.js
Expand Up @@ -284,6 +284,37 @@ tester.run('no-unused-refs', rule, {
}
</script>
`
},
{
filename: 'test.vue',
code: `
<template>
<input ref="x" />
</template>
<script>
import {ref} from 'vue'
export default {
setup() {
const x = ref(null)
return {
x
}
}
}
</script>
`
},
{
filename: 'test.vue',
code: `
<template>
<input ref="x" />
</template>
<script setup>
import {ref} from 'vue'
const x = ref(null)
</script>
`
}
],

Expand Down