From d6ee9454451806f4a704de4ea7040377a1a7e9a8 Mon Sep 17 00:00:00 2001 From: monnef Date: Mon, 24 Aug 2020 14:47:45 +0200 Subject: [PATCH] [Fix] `forbid-component-props`: Implemented support for "namespaced" components Fixes #2766. --- CHANGELOG.md | 2 ++ lib/rules/forbid-component-props.js | 7 +++++-- tests/lib/rules/forbid-component-props.js | 10 ++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1866e352d8..3bf5fee151 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rules/forbid-component-props.js b/lib/rules/forbid-component-props.js index e5d8185961..f5d2a9fce5 100644 --- a/lib/rules/forbid-component-props.js +++ b/lib/rules/forbid-component-props.js @@ -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. ``. + 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; } diff --git a/tests/lib/rules/forbid-component-props.js b/tests/lib/rules/forbid-component-props.js index 910d2c240f..380e0b788d 100644 --- a/tests/lib/rules/forbid-component-props.js +++ b/tests/lib/rules/forbid-component-props.js @@ -104,6 +104,16 @@ ruleTester.run('forbid-component-props', rule, { options: [{ forbid: [{propName: 'className', allowedFor: ['ReactModal']}] }] + }, { + code: 'const item = ();', + options: [{ + forbid: [{propName: 'className', allowedFor: ['AntdLayout.Content']}] + }] + }, { + code: 'const item = ();', + options: [{ + forbid: [{propName: 'className', allowedFor: ['this.ReactModal']}] + }] }], invalid: [{