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

feat(no-unused-properties): mark $props.<prop> as used #2345

Merged
merged 2 commits into from
Dec 28, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading