Skip to content

Commit

Permalink
[Fix] jsx-no-constructed-context-values: fix false positive for usa…
Browse files Browse the repository at this point in the history
…ge in non-components

Fixes #3295
  • Loading branch information
golopot authored and ljharb committed Oct 3, 2022
1 parent 645966a commit d0da6bf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions lib/rules/jsx-no-constructed-context-values.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

'use strict';

const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');
const report = require('../util/report');

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -184,6 +186,10 @@ module.exports = {
return;
}

if (!utils.getParentComponent(node)) {
return;
}

// Report found error
const constructType = constructInfo.type;
const constructNode = constructInfo.node;
Expand Down Expand Up @@ -214,5 +220,5 @@ module.exports = {
});
},
};
},
}),
};
16 changes: 13 additions & 3 deletions tests/lib/rules/jsx-no-constructed-context-values.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ const ruleTester = new RuleTester({ parserOptions });
ruleTester.run('react-no-constructed-context-values', rule, {
valid: parsers.all([
{
code: '<Context.Provider value={props}></Context.Provider>',
code: 'const Component = () => <Context.Provider value={props}></Context.Provider>',
},
{
code: '<Context.Provider value={100}></Context.Provider>',
code: 'const Component = () => <Context.Provider value={100}></Context.Provider>',
},
{
code: '<Context.Provider value="Some string"></Context.Provider>',
code: 'const Component = () => <Context.Provider value="Some string"></Context.Provider>',
},
{
code: 'function Component() { const foo = useMemo(() => { return {} }, []); return (<Context.Provider value={foo}></Context.Provider>)}',
Expand Down Expand Up @@ -137,6 +137,16 @@ ruleTester.run('react-no-constructed-context-values', rule, {
}
`,
},
{
code: `
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<AppContext.Provider value={{}}>
<AppView />
</AppContext.Provider>
);
`,
},
]),
invalid: parsers.all([
{
Expand Down

0 comments on commit d0da6bf

Please sign in to comment.