Skip to content

fix(require-default-prop): avoid requiring defaults for optional props not included in destructuring #2768

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/wise-bugs-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-vue': patch
---

[vue/require-default-prop](https://eslint.vuejs.org/rules/require-default-prop.html) now ignores optional props that are not included in Vue 3.5 props destructuring pattern.
13 changes: 13 additions & 0 deletions lib/rules/require-default-prop.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,19 @@ module.exports = {
if (defaultsByWithDefaults[prop.propName]) {
return true
}
// If using props destructure and this is an optional prop that is NOT included
// in the destructure pattern, exclude it from the report
if (
isUsingPropsDestructure &&
!prop.required &&
prop.propName != null
) {
const destructuredProps = utils.getPropsDestructure(node)
if (!destructuredProps[prop.propName]) {
// Optional prop is not destructured, so no default value is needed
return true
}
}
}
if (!isUsingPropsDestructure) {
return false
Expand Down
59 changes: 59 additions & 0 deletions tests/lib/rules/require-default-prop.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,40 @@ ruleTester.run('require-default-prop', rule, {
...languageOptions,
parserOptions: { parser: require.resolve('@typescript-eslint/parser') }
}
},
// Optional props that are not destructured should not require default values
{
filename: 'test.vue',
code: `
<script setup lang="ts">
const { bar } = defineProps<{
foo?: string;
bar: string;
}>()
</script>
`,
languageOptions: {
parser: require('vue-eslint-parser'),
...languageOptions,
parserOptions: { parser: require.resolve('@typescript-eslint/parser') }
}
},
{
filename: 'test.vue',
code: `
<script setup lang="ts">
const { bar } = defineProps<{
foo?: string;
bar: string;
baz?: number;
}>()
</script>
`,
languageOptions: {
parser: require('vue-eslint-parser'),
...languageOptions,
parserOptions: { parser: require.resolve('@typescript-eslint/parser') }
}
}
],

Expand Down Expand Up @@ -728,6 +762,7 @@ ruleTester.run('require-default-prop', rule, {
},
{
// https://github.com/vuejs/eslint-plugin-vue/issues/2725
// Optional props that ARE destructured should still require default values
filename: 'type-with-props-destructure.vue',
code: `
<script setup lang="ts">
Expand All @@ -745,6 +780,30 @@ ruleTester.run('require-default-prop', rule, {
line: 3
}
]
},
{
// Optional props that are NOT destructured should still cause errors if they are used in destructuring without defaults
filename: 'mixed-optional-destructure.vue',
code: `
<script setup lang="ts">
const {foo, bar, baz} = defineProps<{foo?: number; bar: number; baz?: string}>()
</script>
`,
languageOptions: {
parser: require('vue-eslint-parser'),
...languageOptions,
parserOptions: { parser: require.resolve('@typescript-eslint/parser') }
},
errors: [
{
message: "Prop 'foo' requires default value to be set.",
line: 3
},
{
message: "Prop 'baz' requires default value to be set.",
line: 3
}
]
}
]
})