Skip to content

Commit d6ee945

Browse files
monnefljharb
authored andcommitted
[Fix] forbid-component-props: Implemented support for "namespaced" components
Fixes #2766.
1 parent 9eb81bc commit d6ee945

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
1111

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

1516
[#2771]: https://github.com/yannickcr/eslint-plugin-react/pull/2771
17+
[#2767]: https://github.com/yannickcr/eslint-plugin-react/pull/2767
1618
[#2761]: https://github.com/yannickcr/eslint-plugin-react/pull/2761
1719
[#2748]: https://github.com/yannickcr/eslint-plugin-react/pull/2748
1820

lib/rules/forbid-component-props.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,11 @@ module.exports = {
7878

7979
return {
8080
JSXAttribute(node) {
81-
const tag = node.parent.name.name;
82-
if (tag && tag[0] !== tag[0].toUpperCase()) {
81+
const parentName = node.parent.name;
82+
// Extract a component name when using a "namespace", e.g. `<AntdLayout.Content />`.
83+
const tag = parentName.name || `${parentName.object.name}.${parentName.property.name}`;
84+
const componentName = parentName.name || parentName.property.name;
85+
if (componentName && componentName[0] !== componentName[0].toUpperCase()) {
8386
// This is a DOM node, not a Component, so exit.
8487
return;
8588
}

tests/lib/rules/forbid-component-props.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ ruleTester.run('forbid-component-props', rule, {
104104
options: [{
105105
forbid: [{propName: 'className', allowedFor: ['ReactModal']}]
106106
}]
107+
}, {
108+
code: 'const item = (<AntdLayout.Content className="antdFoo" />);',
109+
options: [{
110+
forbid: [{propName: 'className', allowedFor: ['AntdLayout.Content']}]
111+
}]
112+
}, {
113+
code: 'const item = (<this.ReactModal className="foo" />);',
114+
options: [{
115+
forbid: [{propName: 'className', allowedFor: ['this.ReactModal']}]
116+
}]
107117
}],
108118

109119
invalid: [{

0 commit comments

Comments
 (0)