diff --git a/CHANGELOG.md b/CHANGELOG.md index 876c629a8e..a620d64ebc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,8 +19,10 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel * [`no-typos`]: avoid crash with computed method name ([#2870][] @ljharb, @AriPerkkio) * [`jsx-max-depth`]: avoid crash with childless jsx child ([#2869][] @ljharb, @AriPerkkio) * [`no-unknown-property`]: avoid crash with prop named with Object.prototype key ([#2879][] @ljharb, @AriPerkkio) +* [`prop-types`]: default argument does not count as props-types declaration ([#2877][] @golopot) [#2879]: https://github.com/yannickcr/eslint-plugin-react/issues/2879 +[#2877]: https://github.com/yannickcr/eslint-plugin-react/pull/2877 [#2875]: https://github.com/yannickcr/eslint-plugin-react/issues/2875 [#2871]: https://github.com/yannickcr/eslint-plugin-react/issues/2871 [#2870]: https://github.com/yannickcr/eslint-plugin-react/issues/2870 diff --git a/lib/rules/prop-types.js b/lib/rules/prop-types.js index 2cc1416115..0166ca9ea2 100644 --- a/lib/rules/prop-types.js +++ b/lib/rules/prop-types.js @@ -137,37 +137,6 @@ module.exports = { return true; } - /** - * Checks if the prop is declared in destructured params - * @param {Object[]} params List of destructured param among props without declaredPropTypes - * @returns {Boolean} True if the prop is declared, false if not. - */ - function isDeclaredInDestructuredParam(params) { - let result = true; - params.forEach((param) => { - if (!param.properties) { - result = false; - return; - } - param.properties.forEach((property) => { - if (property.type === 'RestElement' || property.type === 'ExperimentalRestProperty') { - return; - } - const type = property.value.type; - const right = property.value.right; - if (type !== 'AssignmentPattern') { - result = false; - return; - } - if (type === 'AssignmentPattern' && right && right.expression && right.expression.type && right.expression.type !== 'Literal') { - result = false; - } - }); - }); - - return result; - } - /** * Checks if the prop is declared * @param {ASTNode} node The AST node being checked. @@ -185,9 +154,6 @@ module.exports = { return true; } - if (component && !isDeclared && !component.declaredPropTypes && component.node.params && (component.node.type === 'FunctionDeclaration' || component.node.type === 'FunctionExpression' || component.node.type === 'ArrowFunctionExpression')) { - return isDeclaredInDestructuredParam(component.node.params); - } node = node.parent; } return false; diff --git a/tests/lib/rules/prop-types.js b/tests/lib/rules/prop-types.js index b117901173..647eb975a5 100644 --- a/tests/lib/rules/prop-types.js +++ b/tests/lib/rules/prop-types.js @@ -2987,46 +2987,6 @@ ruleTester.run('prop-types', rule, { `, parser: parsers['@TYPESCRIPT_ESLINT'] }, - { - code: ` - function Foo({ foo = "" }): JSX.Element { - return
{foo}
; - } - `, - parser: parsers['@TYPESCRIPT_ESLINT'] - }, - { - code: ` - function Foo({ bar = "" as string }): JSX.Element { - return
{bar}
; - } - `, - parser: parsers['@TYPESCRIPT_ESLINT'] - }, - { - code: ` - export default function ({ value = 'World' }) { - return

Hello {value}

- } - `, - parser: parsers['@TYPESCRIPT_ESLINT'] - }, - { - code: ` - const Foo: JSX.Element = ({ bar = "" }) => { - return
{bar}
; - } - `, - parser: parsers['@TYPESCRIPT_ESLINT'] - }, - { - code: ` - const Foo: JSX.Element = function foo ({ bar = "" }) { - return
{bar}
; - } - `, - parser: parsers['@TYPESCRIPT_ESLINT'] - }, // Issue: #2795 { code: ` @@ -4364,7 +4324,19 @@ ruleTester.run('prop-types', rule, { errors: [ {message: '\'name.constructor.firstname\' is missing in props validation'} ] - }, { + }, + { + code: ` + function Hello({ foo = '' }) { + return

{foo}

+ } + `, + errors: [ + {message: '\'foo\' is missing in props validation'} + ], + parser: parsers.BABEL_ESLINT + }, + { code: [ 'function SomeComponent({bar}) {', ' function f({foo}) {}', @@ -6216,6 +6188,50 @@ ruleTester.run('prop-types', rule, { errors: [{ message: "'type' is missing in props validation" }] + }, + { + code: ` + const Foo: JSX.Element = ({ bar = "" }) => { + return
{bar}
; + } + `, + errors: [ + {message: '\'bar\' is missing in props validation'} + ], + parser: parsers['@TYPESCRIPT_ESLINT'] + }, + { + code: ` + function Foo({ foo = "" }): JSX.Element { + return
{foo}
; + } + `, + errors: [ + {message: '\'foo\' is missing in props validation'} + ], + parser: parsers['@TYPESCRIPT_ESLINT'] + }, + { + code: ` + const Foo: JSX.Element = function foo ({ bar = "" }) { + return
{bar}
; + } + `, + errors: [ + {message: '\'bar\' is missing in props validation'} + ], + parser: parsers['@TYPESCRIPT_ESLINT'] + }, + { + code: ` + function Foo({ bar = "" as string }): JSX.Element { + return
{bar}
; + } + `, + errors: [ + {message: '\'bar\' is missing in props validation'} + ], + parser: parsers['@TYPESCRIPT_ESLINT'] } ]) )