diff --git a/.changeset/wise-bugs-peel.md b/.changeset/wise-bugs-peel.md new file mode 100644 index 000000000..168531e14 --- /dev/null +++ b/.changeset/wise-bugs-peel.md @@ -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. diff --git a/lib/rules/require-default-prop.js b/lib/rules/require-default-prop.js index acceacf08..775295499 100644 --- a/lib/rules/require-default-prop.js +++ b/lib/rules/require-default-prop.js @@ -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 diff --git a/tests/lib/rules/require-default-prop.js b/tests/lib/rules/require-default-prop.js index 43160dfda..2430ade0d 100644 --- a/tests/lib/rules/require-default-prop.js +++ b/tests/lib/rules/require-default-prop.js @@ -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: ` + + `, + languageOptions: { + parser: require('vue-eslint-parser'), + ...languageOptions, + parserOptions: { parser: require.resolve('@typescript-eslint/parser') } + } + }, + { + filename: 'test.vue', + code: ` + + `, + languageOptions: { + parser: require('vue-eslint-parser'), + ...languageOptions, + parserOptions: { parser: require.resolve('@typescript-eslint/parser') } + } } ], @@ -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: ` + `, + 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 + } + ] } ] })