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

Fixes #2766.
  • Loading branch information
monnef authored and ljharb committed Aug 24, 2020
1 parent 9eb81bc commit 6a34b8e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
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: 5 additions & 2 deletions lib/rules/forbid-component-props.js
Expand Up @@ -78,8 +78,11 @@ module.exports = {

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

invalid: [{
Expand Down

0 comments on commit 6a34b8e

Please sign in to comment.