From 8beb2aae3fbe36dd6f495b72cb20b27c043aff68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20Marohni=C4=87?= Date: Sun, 10 Dec 2017 15:52:25 +0100 Subject: [PATCH 1/4] [Fix] `jsx-key`: Ignore elements inside `React.Children.toArray()` jsx-key rule should always succeed if we're inside React.Children.toArray() because omitting the key there doesn't cause a React warning. Fixes #1574. --- CHANGELOG.md | 2 ++ lib/rules/jsx-key.js | 33 ++++++++++++++++++++++++++++++++- tests/lib/rules/jsx-key.js | 27 +++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b9e559291..9a36c5e320 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,9 +8,11 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange ### Fixed * [`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) [#3436]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3436 [#3337]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3337 +[#1591]: https://github.com/jsx-eslint/eslint-plugin-react/pull/1591 ## [7.31.8] - 2022.09.08 diff --git a/lib/rules/jsx-key.js b/lib/rules/jsx-key.js index a23d843845..b1e0a62a4f 100644 --- a/lib/rules/jsx-key.js +++ b/lib/rules/jsx-key.js @@ -155,10 +155,33 @@ module.exports = { } } + const childrenToArraySelector = `:matches( + CallExpression + [callee.object.object.name=${reactPragma}] + [callee.object.property.name=Children] + [callee.property.name=toArray], + CallExpression + [callee.object.name=Children] + [callee.property.name=toArray] + )`.replace(/\s/g, ''); + let isWithinChildrenToArray = false; + const seen = new WeakSet(); return { + [childrenToArraySelector]() { + isWithinChildrenToArray = true; + }, + + [`${childrenToArraySelector}:exit`]() { + isWithinChildrenToArray = false; + }, + 'ArrayExpression, JSXElement > JSXElement'(node) { + if (isWithinChildrenToArray) { + return; + } + const jsx = (node.type === 'ArrayExpression' ? node.elements : node.parent.children).filter((x) => x && x.type === 'JSXElement'); if (jsx.length === 0) { return; @@ -205,7 +228,7 @@ module.exports = { }, JSXFragment(node) { - if (!checkFragmentShorthand) { + if (!checkFragmentShorthand || isWithinChildrenToArray) { return; } @@ -226,6 +249,10 @@ module.exports = { CallExpression[callee.type="OptionalMemberExpression"][callee.property.name="map"],\ OptionalCallExpression[callee.type="MemberExpression"][callee.property.name="map"],\ OptionalCallExpression[callee.type="OptionalMemberExpression"][callee.property.name="map"]'(node) { + if (isWithinChildrenToArray) { + return; + } + const fn = node.arguments.length > 0 && node.arguments[0]; if (!fn || !astUtil.isFunctionLikeExpression(fn)) { return; @@ -238,6 +265,10 @@ module.exports = { // Array.from 'CallExpression[callee.type="MemberExpression"][callee.property.name="from"]'(node) { + if (isWithinChildrenToArray) { + return; + } + const fn = node.arguments.length > 1 && node.arguments[1]; if (!astUtil.isFunctionLikeExpression(fn)) { return; diff --git a/tests/lib/rules/jsx-key.js b/tests/lib/rules/jsx-key.js index 451bcb0d57..15b89c19b7 100644 --- a/tests/lib/rules/jsx-key.js +++ b/tests/lib/rules/jsx-key.js @@ -176,6 +176,33 @@ ruleTester.run('jsx-key', rule, { `, features: ['types', 'no-babel-old'], }, + { code: 'React.Children.toArray([1, 2 ,3].map(x => ));' }, + { + code: ` + import { Children } from "react"; + Children.toArray([1, 2 ,3].map(x => )); + `, + }, + { + // TODO: uncomment the commented lines below + code: ` + import Act from 'react'; + import { Children as ReactChildren } from 'react'; + + const { Children } = Act; + const { toArray } = Children; + + Act.Children.toArray([1, 2 ,3].map(x => )); + Act.Children.toArray(Array.from([1, 2 ,3], x => )); + Children.toArray([1, 2 ,3].map(x => )); + Children.toArray(Array.from([1, 2 ,3], x => )); + // ReactChildren.toArray([1, 2 ,3].map(x => )); + // ReactChildren.toArray(Array.from([1, 2 ,3], x => )); + // toArray([1, 2 ,3].map(x => )); + // toArray(Array.from([1, 2 ,3], x => )); + `, + settings, + }, ]), invalid: parsers.all([ { From 645966a9afcb239ce7c3e2e5bd39f770da6deefb Mon Sep 17 00:00:00 2001 From: Kristoffer Date: Sun, 2 Oct 2022 09:04:49 +0200 Subject: [PATCH 2/4] [Docs] `no-unknown-property`: fix typo in link --- CHANGELOG.md | 4 ++++ docs/rules/no-unknown-property.md | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a36c5e320..829887c3bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,10 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange * [`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) +### Changed +* [Docs] [`no-unknown-property`]: fix typo in link ([#3445][] @denkristoffer) + +[#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 [#1591]: https://github.com/jsx-eslint/eslint-plugin-react/pull/1591 diff --git a/docs/rules/no-unknown-property.md b/docs/rules/no-unknown-property.md index d142def850..e885474f20 100644 --- a/docs/rules/no-unknown-property.md +++ b/docs/rules/no-unknown-property.md @@ -58,7 +58,7 @@ var AtomPanel = ; If you are using a library that passes something as a prop to JSX elements, it is recommended to add those props to the ignored properties. -For example, if you use [emotion](https://emotion.sh/docs/introduction) and its [`css` prop](https://emotion.sh/docs/css-prop)), +For example, if you use [emotion](https://emotion.sh/docs/introduction) and its [`css` prop](https://emotion.sh/docs/css-prop), add the following to your `.eslintrc` config file: ```js From d0da6bf6ba32164962f01eb841325be67d181deb Mon Sep 17 00:00:00 2001 From: Chiawen Chen Date: Mon, 3 Oct 2022 17:42:48 +0800 Subject: [PATCH 3/4] [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([ { From 5baa3e0fb7cf4e7602c48e8dd2ac0ed0091f03cf Mon Sep 17 00:00:00 2001 From: Chiawen Chen Date: Thu, 6 Oct 2022 17:20:32 +0800 Subject: [PATCH 4/4] [Perf] component detection: improve performance by optimizing getId --- CHANGELOG.md | 2 ++ lib/util/Components.js | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d39be4a9df..f5975115a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange ### Changed * [Docs] [`no-unknown-property`]: fix typo in link ([#3445][] @denkristoffer) +* [Perf] component detection: improve performance by optimizing getId ([#3451][] @golopot) +[#3451]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3451 [#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 diff --git a/lib/util/Components.js b/lib/util/Components.js index 7b931c2234..b112923f63 100644 --- a/lib/util/Components.js +++ b/lib/util/Components.js @@ -21,7 +21,7 @@ const isFirstLetterCapitalized = require('./isFirstLetterCapitalized'); const isDestructuredFromPragmaImport = require('./isDestructuredFromPragmaImport'); function getId(node) { - return node && node.range.join(':'); + return node ? `${node.range[0]}:${node.range[1]}` : ''; } function usedPropTypesAreEquivalent(propA, propB) {