Skip to content

[New]: prop-types: handle nested destructuring #2300

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

Merged
merged 1 commit into from
Jun 6, 2019
Merged
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
18 changes: 9 additions & 9 deletions lib/util/usedPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,21 +313,21 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
}
const propName = ast.getKeyValue(context, properties[k]);

// Get parent names in the right hand side of `const {foo} = props.a.b`
let currentNode = (node.parent && node.parent.init) || {};
allNames = [];
while (currentNode.property && currentNode.property.name !== 'props') {
allNames.unshift(currentNode.property.name);
currentNode = currentNode.object;
}
allNames.push(propName);
if (propName) {
usedPropTypes.push({
allNames,
allNames: parentNames.concat([propName]),
name: propName,
node: properties[k]
});
}

if (
propName &&
properties[k].type === 'Property' &&
properties[k].value.type === 'ObjectPattern'
) {
markPropTypesAsUsed(properties[k].value, parentNames.concat([propName]));
}
}
break;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/lib/rules/no-unused-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -2457,6 +2457,21 @@ ruleTester.run('no-unused-prop-types', rule, {
'};'
].join('\n'),
parser: parsers.BABEL_ESLINT
}, {
code: `
class Hello extends React.Component {
render() {
const {foo: {bar}} = this.props;
return <div>{bar}</div>;
}
}
Hello.propTypes = {
foo: PropTypes.shape({
bar: PropTypes.string,
})
};
`,
options: [{skipShapeProps: false}]
}, {
// issue #933
code: [
Expand Down
41 changes: 41 additions & 0 deletions tests/lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -2412,6 +2412,47 @@ ruleTester.run('prop-types', rule, {
column: 35,
type: 'Identifier'
}]
}, {
code: `
class Hello extends React.Component {
render() {
const { foo: { bar } } = this.props;
return <p>{bar}</p>
}
}
`,
errors: [
{message: "'foo' is missing in props validation"},
{message: "'foo.bar' is missing in props validation"}
]
}, {
code: `
class Hello extends React.Component {
render() {
const { foo: { bar } } = this.props;
return <p>{bar}</p>
}
}

Hello.propTypes = {
foo: PropTypes.shape({
_: PropTypes.string,
})
}
`,
errors: [
{message: "'foo.bar' is missing in props validation"}
]
}, {
code: `
function Foo({ foo: { bar } }) {
return <p>{bar}</p>
}
`,
errors: [
{message: "'foo' is missing in props validation"},
{message: "'foo.bar' is missing in props validation"}
]
}, {
code: [
'class Hello extends React.Component {',
Expand Down