Skip to content

Commit 7d036cf

Browse files
golopotljharb
authored andcommitted
[New]: prop-types: handle nested destructuring
1 parent 44e568a commit 7d036cf

File tree

3 files changed

+65
-9
lines changed

3 files changed

+65
-9
lines changed

lib/util/usedPropTypes.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -313,21 +313,21 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
313313
}
314314
const propName = ast.getKeyValue(context, properties[k]);
315315

316-
// Get parent names in the right hand side of `const {foo} = props.a.b`
317-
let currentNode = (node.parent && node.parent.init) || {};
318-
allNames = [];
319-
while (currentNode.property && currentNode.property.name !== 'props') {
320-
allNames.unshift(currentNode.property.name);
321-
currentNode = currentNode.object;
322-
}
323-
allNames.push(propName);
324316
if (propName) {
325317
usedPropTypes.push({
326-
allNames,
318+
allNames: parentNames.concat([propName]),
327319
name: propName,
328320
node: properties[k]
329321
});
330322
}
323+
324+
if (
325+
propName &&
326+
properties[k].type === 'Property' &&
327+
properties[k].value.type === 'ObjectPattern'
328+
) {
329+
markPropTypesAsUsed(properties[k].value, parentNames.concat([propName]));
330+
}
331331
}
332332
break;
333333
}

tests/lib/rules/no-unused-prop-types.js

+15
Original file line numberDiff line numberDiff line change
@@ -2457,6 +2457,21 @@ ruleTester.run('no-unused-prop-types', rule, {
24572457
'};'
24582458
].join('\n'),
24592459
parser: parsers.BABEL_ESLINT
2460+
}, {
2461+
code: `
2462+
class Hello extends React.Component {
2463+
render() {
2464+
const {foo: {bar}} = this.props;
2465+
return <div>{bar}</div>;
2466+
}
2467+
}
2468+
Hello.propTypes = {
2469+
foo: PropTypes.shape({
2470+
bar: PropTypes.string,
2471+
})
2472+
};
2473+
`,
2474+
options: [{skipShapeProps: false}]
24602475
}, {
24612476
// issue #933
24622477
code: [

tests/lib/rules/prop-types.js

+41
Original file line numberDiff line numberDiff line change
@@ -2412,6 +2412,47 @@ ruleTester.run('prop-types', rule, {
24122412
column: 35,
24132413
type: 'Identifier'
24142414
}]
2415+
}, {
2416+
code: `
2417+
class Hello extends React.Component {
2418+
render() {
2419+
const { foo: { bar } } = this.props;
2420+
return <p>{bar}</p>
2421+
}
2422+
}
2423+
`,
2424+
errors: [
2425+
{message: "'foo' is missing in props validation"},
2426+
{message: "'foo.bar' is missing in props validation"}
2427+
]
2428+
}, {
2429+
code: `
2430+
class Hello extends React.Component {
2431+
render() {
2432+
const { foo: { bar } } = this.props;
2433+
return <p>{bar}</p>
2434+
}
2435+
}
2436+
2437+
Hello.propTypes = {
2438+
foo: PropTypes.shape({
2439+
_: PropTypes.string,
2440+
})
2441+
}
2442+
`,
2443+
errors: [
2444+
{message: "'foo.bar' is missing in props validation"}
2445+
]
2446+
}, {
2447+
code: `
2448+
function Foo({ foo: { bar } }) {
2449+
return <p>{bar}</p>
2450+
}
2451+
`,
2452+
errors: [
2453+
{message: "'foo' is missing in props validation"},
2454+
{message: "'foo.bar' is missing in props validation"}
2455+
]
24152456
}, {
24162457
code: [
24172458
'class Hello extends React.Component {',

0 commit comments

Comments
 (0)