Skip to content

Commit 2cca260

Browse files
committed
[Fix] forbid-prop-types: properly report name in error message; check undestructured arguments
Fixes #2945
1 parent 0218e61 commit 2cca260

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
2727
* [`function-component-definition`]: do not break on dollar signs ([#3207][] @ljharb)
2828
* [`prefer-stateless-function`]: avoid a crash inside `doctrine` ([#2596][] @ljharb)
2929
* [`prop-types`]: catch infinite loop ([#2861][] @ljharb)
30+
* [`forbid-prop-types`]: properly report name in error message; check undestructured arguments ([#2945][] @ljharb)
3031

3132
### Changed
3233
* [readme] change [`jsx-runtime`] link from branch to sha ([#3160][] @tatsushitoji)
@@ -56,6 +57,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
5657
[#3163]: https://github.com/yannickcr/eslint-plugin-react/pull/3163
5758
[#3160]: https://github.com/yannickcr/eslint-plugin-react/pull/3160
5859
[#3133]: https://github.com/yannickcr/eslint-plugin-react/pull/3133
60+
[#2945]: https://github.com/yannickcr/eslint-plugin-react/issues/2945
5961
[#2921]: https://github.com/yannickcr/eslint-plugin-react/pull/2921
6062
[#2861]: https://github.com/yannickcr/eslint-plugin-react/issues/2861
6163
[#2813]: https://github.com/yannickcr/eslint-plugin-react/pull/2813

lib/rules/forbid-prop-types.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ module.exports = {
114114
}
115115
if (value.type === 'CallExpression') {
116116
value.arguments.forEach((arg) => {
117-
reportIfForbidden(arg.name, declaration, target);
117+
const name = arg.type === 'MemberExpression' ? arg.property.name : arg.name;
118+
reportIfForbidden(name, declaration, name);
118119
});
119120
value = value.callee;
120121
}

tests/lib/rules/forbid-prop-types.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,5 +1719,55 @@ ruleTester.run('forbid-prop-types', rule, {
17191719
`,
17201720
errors: 1,
17211721
},
1722+
{
1723+
code: `
1724+
import React from './React';
1725+
1726+
import { arrayOf, object } from 'prop-types';
1727+
1728+
const App = ({ foo }) => (
1729+
<div>
1730+
Hello world {foo}
1731+
</div>
1732+
);
1733+
1734+
App.propTypes = {
1735+
foo: arrayOf(object)
1736+
}
1737+
1738+
export default App;
1739+
`,
1740+
errors: [
1741+
{
1742+
messageId: 'forbiddenPropType',
1743+
data: { target: 'object' },
1744+
},
1745+
],
1746+
},
1747+
{
1748+
code: `
1749+
import React from './React';
1750+
1751+
import PropTypes, { arrayOf } from 'prop-types';
1752+
1753+
const App = ({ foo }) => (
1754+
<div>
1755+
Hello world {foo}
1756+
</div>
1757+
);
1758+
1759+
App.propTypes = {
1760+
foo: arrayOf(PropTypes.object)
1761+
}
1762+
1763+
export default App;
1764+
`,
1765+
errors: [
1766+
{
1767+
messageId: 'forbiddenPropType',
1768+
data: { target: 'object' },
1769+
},
1770+
],
1771+
},
17221772
]),
17231773
});

0 commit comments

Comments
 (0)