Skip to content

Commit

Permalink
[Fix] jsx-no-constructed-context-values: avoid a crash with boolean…
Browse files Browse the repository at this point in the history
… shorthand

Fixes #2895
  • Loading branch information
ljharb committed Jan 5, 2021
1 parent 1b5e450 commit 4a6827d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,7 +7,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel

### Fixed
* [`jsx-no-constructed-context-values`]: avoid a crash with `as X` TS code ([#2894][] @ljharb)
* [`jsx-no-constructed-context-values`]: avoid a crash with boolean shorthand ([#2895][] @ljharb)

[#2895]: https://github.com/yannickcr/eslint-plugin-react/issues/2895
[#2894]: https://github.com/yannickcr/eslint-plugin-react/issues/2894

## [7.22.0] - 2020.12.29
Expand Down
4 changes: 4 additions & 0 deletions lib/rules/jsx-no-constructed-context-values.js
Expand Up @@ -167,6 +167,10 @@ module.exports = {
}

const valueNode = jsxValueAttribute.value;
if (!valueNode) {
// attribute is a boolean shorthand
return;
}
if (valueNode.type !== 'JSXExpressionContainer') {
// value could be a literal
return;
Expand Down
16 changes: 15 additions & 1 deletion tests/lib/rules/jsx-no-constructed-context-values.js
Expand Up @@ -141,7 +141,21 @@ ruleTester.run('react-no-constructed-context-values', rule, {
`,
parser: parsers['@TYPESCRIPT_ESLINT']
}
)
),
{
code: `
import React from 'react';
import BooleanContext from './BooleanContext';
function ContextProvider(props) {
return (
<BooleanContext.Provider value>
{props.children}
</BooleanContext.Provider>
)
}
`
}
),
invalid: [
{
Expand Down

0 comments on commit 4a6827d

Please sign in to comment.