From d0da6bf6ba32164962f01eb841325be67d181deb Mon Sep 17 00:00:00 2001 From: Chiawen Chen Date: Mon, 3 Oct 2022 17:42:48 +0800 Subject: [PATCH] [Fix] `jsx-no-constructed-context-values`: fix false positive for usage in non-components Fixes #3295 --- CHANGELOG.md | 2 ++ lib/rules/jsx-no-constructed-context-values.js | 10 ++++++++-- .../rules/jsx-no-constructed-context-values.js | 16 +++++++++++++--- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 829887c3bc..d39be4a9df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,10 +9,12 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange * [`no-unknown-property`]: add `dialog` attributes ([#3436][] @ljharb) * [`no-arrow-function-lifecycle`]: when converting from an arrow, remove the semi and wrapping parens ([#3337][] @ljharb) * [`jsx-key`]: Ignore elements inside `React.Children.toArray()` ([#1591][] @silvenon) +* [`jsx-no-constructed-context-values`]: fix false positive for usage in non-components ([#3448][] @golopot) ### Changed * [Docs] [`no-unknown-property`]: fix typo in link ([#3445][] @denkristoffer) +[#3448]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3448 [#3445]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3445 [#3436]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3436 [#3337]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3337 diff --git a/lib/rules/jsx-no-constructed-context-values.js b/lib/rules/jsx-no-constructed-context-values.js index af7c9575df..ac34ead85e 100644 --- a/lib/rules/jsx-no-constructed-context-values.js +++ b/lib/rules/jsx-no-constructed-context-values.js @@ -6,6 +6,7 @@ 'use strict'; +const Components = require('../util/Components'); const docsUrl = require('../util/docsUrl'); const report = require('../util/report'); @@ -139,7 +140,8 @@ module.exports = { messages, }, - create(context) { + // eslint-disable-next-line arrow-body-style + create: Components.detect((context, components, utils) => { return { JSXOpeningElement(node) { const openingElementName = node.name; @@ -184,6 +186,10 @@ module.exports = { return; } + if (!utils.getParentComponent(node)) { + return; + } + // Report found error const constructType = constructInfo.type; const constructNode = constructInfo.node; @@ -214,5 +220,5 @@ module.exports = { }); }, }; - }, + }), }; diff --git a/tests/lib/rules/jsx-no-constructed-context-values.js b/tests/lib/rules/jsx-no-constructed-context-values.js index 1ec0592382..c8c9dee995 100644 --- a/tests/lib/rules/jsx-no-constructed-context-values.js +++ b/tests/lib/rules/jsx-no-constructed-context-values.js @@ -31,13 +31,13 @@ const ruleTester = new RuleTester({ parserOptions }); ruleTester.run('react-no-constructed-context-values', rule, { valid: parsers.all([ { - code: '', + code: 'const Component = () => ', }, { - code: '', + code: 'const Component = () => ', }, { - code: '', + code: 'const Component = () => ', }, { code: 'function Component() { const foo = useMemo(() => { return {} }, []); return ()}', @@ -137,6 +137,16 @@ ruleTester.run('react-no-constructed-context-values', rule, { } `, }, + { + code: ` + const root = ReactDOM.createRoot(document.getElementById('root')); + root.render( + + + + ); + `, + }, ]), invalid: parsers.all([ {