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

Fixed false negatives for props in template in vue/no-unused-properties #2435

Merged
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
33 changes: 27 additions & 6 deletions lib/rules/no-unused-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ module.exports = {
const deepData = Boolean(options.deepData)
const ignorePublicMembers = Boolean(options.ignorePublicMembers)
const unreferencedOptions = new Set(options.unreferencedOptions || [])
/** @type {null | Pattern} */
let propsReferencePattern = null

const propertyReferenceExtractor = definePropertyReferenceExtractor(
context,
Expand Down Expand Up @@ -332,8 +334,9 @@ module.exports = {

for (const property of container.properties) {
if (
property.groupName === 'props' &&
propertyReferencesForProps.hasProperty(property.name)
(property.groupName === 'props' &&
propertyReferencesForProps.hasProperty(property.name)) ||
propertyReferences.hasProperty('$props')
) {
// used props
continue
Expand Down Expand Up @@ -369,6 +372,7 @@ module.exports = {
}
continue
}

context.report({
node: property.node,
messageId: 'unused',
Expand Down Expand Up @@ -450,9 +454,9 @@ module.exports = {
return
}

const pattern = target.parent.id
propsReferencePattern = target.parent.id
const propertyReferences =
propertyReferenceExtractor.extractFromPattern(pattern)
propertyReferenceExtractor.extractFromPattern(propsReferencePattern)
container.propertyReferencesForProps.push(propertyReferences)
},
onDefineModelEnter(node, model) {
Expand Down Expand Up @@ -705,9 +709,26 @@ module.exports = {
* @param {VExpressionContainer} node
*/
VExpressionContainer(node) {
templatePropertiesContainer.propertyReferences.push(
const property =
propertyReferenceExtractor.extractFromVExpressionContainer(node)
)

templatePropertiesContainer.propertyReferences.push(property)

if (!propsReferencePattern) {
return
}

// props.prop in template
for (const key of property.allProperties().keys()) {
if (
propsReferencePattern.type === 'Identifier' &&
propsReferencePattern.name === key
) {
templatePropertiesContainer.propertyReferences.push(
property.getNest(key)
)
}
}
},
/**
* @param {VAttribute} node
Expand Down
121 changes: 121 additions & 0 deletions tests/lib/rules/no-unused-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,20 @@ tester.run('no-unused-properties', rule, {
</script>
`
},
// a property used as a template $props expression
{
filename: 'test.vue',
code: `
<template>
<div>{{ $props }}</div>
FloEdelmann marked this conversation as resolved.
Show resolved Hide resolved
</template>
<script>
export default {
props: ['count']
}
</script>
`
},

// properties used in a template expression
{
Expand Down Expand Up @@ -625,6 +639,20 @@ tester.run('no-unused-properties', rule, {
</script>
`
},
// a property used in v-on as $props expression
{
filename: 'test.vue',
code: `
<template>
<button @click="alert($props)" />
</template>
<script>
export default {
props: ['count']
};
</script>
`
},

// data used in a script expression
{
Expand Down Expand Up @@ -2167,6 +2195,48 @@ tester.run('no-unused-properties', rule, {
{{ foo }}
</template>
`
},

// props.prop in template
{
filename: 'test.vue',
code: `
<template>
{{props}}
</template>
<script setup>
const props = defineProps(['a', 'b', 'c'])
</script>`
},
{
filename: 'test.vue',
code: `
<template>
{{props.a}}
</template>
<script setup>
const props = defineProps(['a'])
</script>`
},
{
filename: 'test.vue',
code: `
<template>
{{foo.a}}
</template>
<script setup>
const foo = defineProps(['a'])
</script>`
},
{
code: `
<script setup lang="ts">
const props = defineProps<{ foo: string, bar: string }>()
</script>
<template>
{{ props.foo }}{{ bar }}
</template>`,
...getTypeScriptFixtureTestOptions()
}
],
invalid: [
Expand Down Expand Up @@ -3895,6 +3965,57 @@ tester.run('no-unused-properties', rule, {
line: 6
}
]
},

// a property used as a template $props member expression
{
filename: 'test.vue',
code: `
<template>
<div>{{ $props.foo }}</div>
</template>
<script>
export default {
props: ['foo', 'bar']
}
</script>
`,
errors: ["'bar' of property found, but never used."]
},

// props.prop in template
{
filename: 'test.vue',
code: `
<template>
{{props.a}}
</template>
<script setup>
const props = defineProps(['a', 'b'])
</script>`,
errors: ["'b' of property found, but never used."]
},
{
filename: 'test.vue',
code: `
<template>
{{foo.a}}
</template>
<script setup>
const foo = defineProps(['a', 'b'])
</script>`,
errors: ["'b' of property found, but never used."]
},
{
code: `
<script setup lang="ts">
const props = defineProps<{ foo: string, bar: string, baz: string }>()
</script>
<template>
{{ props.foo }}{{ bar }}
</template>`,
errors: ["'baz' of property found, but never used."],
...getTypeScriptFixtureTestOptions()
}
]
})
Expand Down