Skip to content

Commit

Permalink
[Fix] forbid-component-props: Implemented support for "namespaced" …
Browse files Browse the repository at this point in the history
…components
  • Loading branch information
monnef authored and ljharb committed Aug 24, 2020
1 parent 9eb81bc commit 3924844
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -11,8 +11,10 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel

### Fixed
* [`function-component-definition`]: ignore object properties ([#2771][] @stefan-wullems)
* [`forbid-component-props`]: Implemented support for "namespaced" components ([#2767][] @mnn)

[#2771]: https://github.com/yannickcr/eslint-plugin-react/pull/2771
[#2767]: https://github.com/yannickcr/eslint-plugin-react/pull/2767
[#2761]: https://github.com/yannickcr/eslint-plugin-react/pull/2761
[#2748]: https://github.com/yannickcr/eslint-plugin-react/pull/2748

Expand Down
7 changes: 6 additions & 1 deletion lib/rules/forbid-component-props.js
Expand Up @@ -78,7 +78,12 @@ module.exports = {

return {
JSXAttribute(node) {
const tag = node.parent.name.name;
const parentName = node.parent.name;
let tag = parentName.name;
if (!tag && parentName.object.name !== 'this') {
// Extract a component name when using a "namespace", e.g. `<AntdLayout.Content />`.
tag = `${parentName.object.name}.${parentName.property.name}`;
}
if (tag && tag[0] !== tag[0].toUpperCase()) {
// This is a DOM node, not a Component, so exit.
return;
Expand Down
5 changes: 5 additions & 0 deletions tests/lib/rules/forbid-component-props.js
Expand Up @@ -104,6 +104,11 @@ ruleTester.run('forbid-component-props', rule, {
options: [{
forbid: [{propName: 'className', allowedFor: ['ReactModal']}]
}]
}, {
code: 'const item = (<AntdLayout.Content className="antdFoo" />);',
options: [{
forbid: [{propName: 'className', allowedFor: ['AntdLayout.Content']}]
}]
}],

invalid: [{
Expand Down

0 comments on commit 3924844

Please sign in to comment.