Skip to content

Commit d0da6bf

Browse files
golopotljharb
authored andcommitted
[Fix] jsx-no-constructed-context-values: fix false positive for usage in non-components
Fixes #3295
1 parent 645966a commit d0da6bf

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
99
* [`no-unknown-property`]: add `dialog` attributes ([#3436][] @ljharb)
1010
* [`no-arrow-function-lifecycle`]: when converting from an arrow, remove the semi and wrapping parens ([#3337][] @ljharb)
1111
* [`jsx-key`]: Ignore elements inside `React.Children.toArray()` ([#1591][] @silvenon)
12+
* [`jsx-no-constructed-context-values`]: fix false positive for usage in non-components ([#3448][] @golopot)
1213

1314
### Changed
1415
* [Docs] [`no-unknown-property`]: fix typo in link ([#3445][] @denkristoffer)
1516

17+
[#3448]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3448
1618
[#3445]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3445
1719
[#3436]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3436
1820
[#3337]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3337

lib/rules/jsx-no-constructed-context-values.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
'use strict';
88

9+
const Components = require('../util/Components');
910
const docsUrl = require('../util/docsUrl');
1011
const report = require('../util/report');
1112

@@ -139,7 +140,8 @@ module.exports = {
139140
messages,
140141
},
141142

142-
create(context) {
143+
// eslint-disable-next-line arrow-body-style
144+
create: Components.detect((context, components, utils) => {
143145
return {
144146
JSXOpeningElement(node) {
145147
const openingElementName = node.name;
@@ -184,6 +186,10 @@ module.exports = {
184186
return;
185187
}
186188

189+
if (!utils.getParentComponent(node)) {
190+
return;
191+
}
192+
187193
// Report found error
188194
const constructType = constructInfo.type;
189195
const constructNode = constructInfo.node;
@@ -214,5 +220,5 @@ module.exports = {
214220
});
215221
},
216222
};
217-
},
223+
}),
218224
};

tests/lib/rules/jsx-no-constructed-context-values.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ const ruleTester = new RuleTester({ parserOptions });
3131
ruleTester.run('react-no-constructed-context-values', rule, {
3232
valid: parsers.all([
3333
{
34-
code: '<Context.Provider value={props}></Context.Provider>',
34+
code: 'const Component = () => <Context.Provider value={props}></Context.Provider>',
3535
},
3636
{
37-
code: '<Context.Provider value={100}></Context.Provider>',
37+
code: 'const Component = () => <Context.Provider value={100}></Context.Provider>',
3838
},
3939
{
40-
code: '<Context.Provider value="Some string"></Context.Provider>',
40+
code: 'const Component = () => <Context.Provider value="Some string"></Context.Provider>',
4141
},
4242
{
4343
code: 'function Component() { const foo = useMemo(() => { return {} }, []); return (<Context.Provider value={foo}></Context.Provider>)}',
@@ -137,6 +137,16 @@ ruleTester.run('react-no-constructed-context-values', rule, {
137137
}
138138
`,
139139
},
140+
{
141+
code: `
142+
const root = ReactDOM.createRoot(document.getElementById('root'));
143+
root.render(
144+
<AppContext.Provider value={{}}>
145+
<AppView />
146+
</AppContext.Provider>
147+
);
148+
`,
149+
},
140150
]),
141151
invalid: parsers.all([
142152
{

0 commit comments

Comments
 (0)