diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cbdf71480..bde511e3fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rules/jsx-no-constructed-context-values.js b/lib/rules/jsx-no-constructed-context-values.js index 59010a7c95..9c6186cf7d 100644 --- a/lib/rules/jsx-no-constructed-context-values.js +++ b/lib/rules/jsx-no-constructed-context-values.js @@ -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; diff --git a/tests/lib/rules/jsx-no-constructed-context-values.js b/tests/lib/rules/jsx-no-constructed-context-values.js index ea2609d57d..efb90e79ff 100644 --- a/tests/lib/rules/jsx-no-constructed-context-values.js +++ b/tests/lib/rules/jsx-no-constructed-context-values.js @@ -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 ( + + {props.children} + + ) + } + ` + } ), invalid: [ {