Skip to content

Commit 861fdef

Browse files
chiawendtljharb
authored andcommitted
[fix] prop-types: fix case with destructuring and defualt param
Fixes #2241
1 parent 9fbd037 commit 861fdef

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

lib/util/usedPropTypes.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,16 +316,17 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
316316
break;
317317
case 'ArrowFunctionExpression':
318318
case 'FunctionDeclaration':
319-
case 'FunctionExpression':
319+
case 'FunctionExpression': {
320320
if (node.params.length === 0) {
321321
break;
322322
}
323323
type = 'destructuring';
324-
properties = node.params[0].properties;
325-
if (inSetStateUpdater()) {
326-
properties = node.params[1].properties;
327-
}
324+
const propParam = inSetStateUpdater() ? node.params[1] : node.params[0];
325+
properties = propParam.type === 'AssignmentPattern'
326+
? propParam.left.properties
327+
: propParam.properties;
328328
break;
329+
}
329330
case 'VariableDeclarator':
330331
for (let i = 0, j = node.id.properties.length; i < j; i++) {
331332
// let {props: {firstname}} = this
@@ -421,7 +422,13 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
421422
* FunctionDeclaration, or FunctionExpression
422423
*/
423424
function markDestructuredFunctionArgumentsAsUsed(node) {
424-
const destructuring = node.params && node.params[0] && node.params[0].type === 'ObjectPattern';
425+
const param = node.params && inSetStateUpdater() ? node.params[1] : node.params[0];
426+
427+
const destructuring = param && (
428+
param.type === 'ObjectPattern' ||
429+
param.type === 'AssignmentPattern' && param.left.type === 'ObjectPattern'
430+
);
431+
425432
if (destructuring && (components.get(node) || components.get(node.parent))) {
426433
markPropTypesAsUsed(node);
427434
}

tests/lib/rules/prop-types.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3393,6 +3393,16 @@ ruleTester.run('prop-types', rule, {
33933393
errors: [
33943394
{message: '\'bar\' is missing in props validation'}
33953395
]
3396+
}, {
3397+
code: [
3398+
'function SomeComponent({bar} = baz) {',
3399+
' function f({foo}) {}',
3400+
' return <div className={f()}>{bar}</div>;',
3401+
'}'
3402+
].join('\n'),
3403+
errors: [
3404+
{message: '\'bar\' is missing in props validation'}
3405+
]
33963406
}, {
33973407
code: [
33983408
'class Hello extends React.PureComponent {',
@@ -4226,6 +4236,21 @@ ruleTester.run('prop-types', rule, {
42264236
message: '\'bar\' is missing in props validation'
42274237
}]
42284238
},
4239+
{
4240+
code: `
4241+
class Foo extends React.Component {
4242+
setZoo() {
4243+
this.setState((state, {zoo}) => ({ zoo }));
4244+
}
4245+
render() {
4246+
return <div />;
4247+
}
4248+
}
4249+
`,
4250+
errors: [{
4251+
message: '\'zoo\' is missing in props validation'
4252+
}]
4253+
},
42294254
{
42304255
code: `
42314256
class Foo extends React.Component {

0 commit comments

Comments
 (0)