Skip to content
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

[Fix] forbid-component-props: Implemented support for "namespaced" components. #2767

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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