Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
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
95 changes: 95 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>
</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,38 @@ tester.run('no-unused-properties', rule, {
{{ foo }}
</template>
`
},

// props.prop in template
{
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 +3955,41 @@ tester.run('no-unused-properties', rule, {
line: 6
}
]
},

// 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