Skip to content

Commit 1b5e450

Browse files
committed
[Fix] jsx-no-constructed-context-values: avoid a crash with as X TS code
Fixes #2894
1 parent 3481a49 commit 1b5e450

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
55

66
## Unreleased
77

8+
### Fixed
9+
* [`jsx-no-constructed-context-values`]: avoid a crash with `as X` TS code ([#2894][] @ljharb)
10+
11+
[#2894]: https://github.com/yannickcr/eslint-plugin-react/issues/2894
12+
813
## [7.22.0] - 2020.12.29
914

1015
### Added

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function isConstruction(node, callScope) {
9898
case 'JSXElement':
9999
return {type: 'JSX element', node};
100100
case 'AssignmentExpression': {
101-
const construct = isConstruction(node.right);
101+
const construct = isConstruction(node.right, callScope);
102102
if (construct != null) {
103103
return {
104104
type: 'assignment expression',
@@ -110,7 +110,7 @@ function isConstruction(node, callScope) {
110110
}
111111
case 'TypeCastExpression':
112112
case 'TSAsExpression':
113-
return isConstruction(node.expression);
113+
return isConstruction(node.expression, callScope);
114114
default:
115115
return null;
116116
}

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

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const parserOptions = {
2929

3030
const ruleTester = new RuleTester({parserOptions});
3131
ruleTester.run('react-no-constructed-context-values', rule, {
32-
valid: [
32+
valid: [].concat(
3333
{
3434
code: '<Context.Provider value={props}></Context.Provider>'
3535
},
@@ -105,8 +105,44 @@ ruleTester.run('react-no-constructed-context-values', rule, {
105105
return (<Context.Provider value={a}></Context.Provider>);
106106
}
107107
`
108-
}
109-
],
108+
},
109+
parsers.TS(
110+
{
111+
code: `
112+
import React from 'react';
113+
import MyContext from './MyContext';
114+
115+
const value = '';
116+
117+
function ContextProvider(props) {
118+
return (
119+
<MyContext.Provider value={value as any}>
120+
{props.children}
121+
</MyContext.Provider>
122+
)
123+
}
124+
`,
125+
parser: parsers.TYPESCRIPT_ESLINT
126+
},
127+
{
128+
code: `
129+
import React from 'react';
130+
import MyContext from './MyContext';
131+
132+
const value = '';
133+
134+
function ContextProvider(props) {
135+
return (
136+
<MyContext.Provider value={value as any}>
137+
{props.children}
138+
</MyContext.Provider>
139+
)
140+
}
141+
`,
142+
parser: parsers['@TYPESCRIPT_ESLINT']
143+
}
144+
)
145+
),
110146
invalid: [
111147
{
112148
// Invalid because object construction creates a new identity

0 commit comments

Comments
 (0)