Skip to content

Commit

Permalink
feat(no-unused-properties): mark $props.<prop> as used (#2345)
Browse files Browse the repository at this point in the history
  • Loading branch information
levchak0910 authored Dec 28, 2023
1 parent d99f37b commit 63a17bd
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 2 deletions.
28 changes: 26 additions & 2 deletions lib/utils/property-references.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,22 @@ function definePropertyReferenceExtractor(
if (parent.object === node) {
// `arg.foo`
const name = utils.getStaticPropertyName(parent)
if (name) {

if (
name === '$props' &&
parent.parent.type === 'MemberExpression'
) {
// `$props.arg`
const propName = utils.getStaticPropertyName(parent.parent)

if (!propName) return unknownMemberAsUnreferenced ? NEVER : ANY

return new PropertyReferencesForMember(
parent.parent,
propName,
withInTemplate
)
} else if (name) {
return new PropertyReferencesForMember(
parent,
name,
Expand Down Expand Up @@ -655,8 +670,17 @@ function definePropertyReferenceExtractor(
references.push(extractFromExpression(id, true))
}
} else {
const referenceId =
id.name === '$props' &&
id.parent.type === 'MemberExpression' &&
id.parent.property.type === 'Identifier'
? id.parent.property
: id

references.push(
extractFromName(id.name, id, () => extractFromExpression(id, true))
extractFromName(referenceId.name, referenceId, () =>
extractFromExpression(referenceId, true)
)
)
}
}
Expand Down
158 changes: 158 additions & 0 deletions tests/lib/rules/no-unused-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ tester.run('no-unused-properties', rule, {
</script>
`
},
{
filename: 'test.vue',
code: `
<script>
export default {
props: ['count'],
created() {
alert(this.$props.count + 1)
}
};
</script>
`
},
// default options
{
filename: 'test.vue',
Expand Down Expand Up @@ -139,6 +152,20 @@ tester.run('no-unused-properties', rule, {
</script>
`
},
// a property used as a template $props member expression
{
filename: 'test.vue',
code: `
<template>
<div>{{ $props.count }}</div>
</template>
<script>
export default {
props: ['count']
}
</script>
`
},

// properties used in a template expression
{
Expand All @@ -154,6 +181,20 @@ tester.run('no-unused-properties', rule, {
</script>
`
},
// properties used in a template expression as $props member expression
{
filename: 'test.vue',
code: `
<template>
<div>{{ $props.count1 + $props.count2 }}</div>
</template>
<script>
export default {
props: ['count1', 'count2']
};
</script>
`
},

// a property used in v-if
{
Expand All @@ -173,6 +214,24 @@ tester.run('no-unused-properties', rule, {
</script>
`
},
// a property used in v-if as $props member expression
{
filename: 'test.vue',
code: `
<template>
<div v-if="$props.count > 0"></div>
</template>
<script>
export default {
props: {
count: {
type: Number
}
}
};
</script>
`
},

// a property used in v-for
{
Expand All @@ -193,6 +252,25 @@ tester.run('no-unused-properties', rule, {
</script>
`
},
// a property used in v-for as $props member expression
{
filename: 'test.vue',
code: `
<template>
<div v-for="color in $props.colors">{{ color }}</div>
</template>
<script>
export default {
props: {
colors: {
type: Array,
default: () => []
}
}
};
</script>
`
},

// a property used in v-html
{
Expand All @@ -208,6 +286,20 @@ tester.run('no-unused-properties', rule, {
</script>
`
},
// a property used in v-html as $props member expression
{
filename: 'test.vue',
code: `
<template>
<div v-html="$props.message" />
</template>
<script>
export default {
props: ['message']
};
</script>
`
},

// a property passed in a component
{
Expand All @@ -223,6 +315,20 @@ tester.run('no-unused-properties', rule, {
</script>
`
},
// a property passed in a component as $props member expression
{
filename: 'test.vue',
code: `
<template>
<counter :count="$props.count" />
</template>
<script>
export default {
props: ['count']
};
</script>
`
},

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

// data used in a script expression
{
Expand Down Expand Up @@ -1196,6 +1316,21 @@ tester.run('no-unused-properties', rule, {
</script>
`
},
{
filename: 'test.vue',
code: `
<script>
export default {
props: ['x'],
computed: {
y: {
get: () => this.$props.x * 2
}
}
};
</script>
`
},

// deep data
{
Expand Down Expand Up @@ -1587,6 +1722,29 @@ tester.run('no-unused-properties', rule, {
}
]
},
{
filename: 'test.vue',
code: `
<template>
</template>
<script>
export default {
props: {
a: String,
},
computed: {
/** @public */
b () { return this.$props.a },
},
}
</script>`,
options: [
{
groups: ['props', 'computed'],
ignorePublicMembers: true
}
]
},

// expose
{
Expand Down

0 comments on commit 63a17bd

Please sign in to comment.