Skip to content

Commit

Permalink
[Docs] jsx-boolean-value: add jsdoc types for helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
caroline223 authored and ljharb committed Jun 29, 2022
1 parent ab72e48 commit b714407
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -24,9 +24,11 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
* [Refactor] `boolean-prop-naming`, `jsx-indent`: avoid assigning to arguments ([#3316][] @caroline223)
* [Docs] [`sort-comp`]: add class component examples ([#3339][] @maurer2)
* [Docs] [`jsx-no-useless-fragment`]: add more examples of correct code ([#3349][] @karlhorky)
* [Docs] [`jsx-boolean-value`]: add jsdoc types for helper functions ([#3344][] @caroline223)

[#3350]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3350
[#3349]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3349
[#3344]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3344
[#3339]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3339
[#3335]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3335
[#3331]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3331
Expand Down
30 changes: 26 additions & 4 deletions lib/rules/jsx-boolean-value.js
Expand Up @@ -22,6 +22,10 @@ const ALWAYS = 'always';
const NEVER = 'never';

const errorData = new WeakMap();
/**
* @param {object} exceptions
* @returns {object}
*/
function getErrorData(exceptions) {
if (!errorData.has(exceptions)) {
const exceptionProps = Array.from(exceptions, (name) => `\`${name}\``).join(', ');
Expand All @@ -30,15 +34,25 @@ function getErrorData(exceptions) {
}
return errorData.get(exceptions);
}

/**
* @param {string} configuration
* @param {Set<string>} exceptions
* @param {string} propName
* @returns {boolean} propName
*/
function isAlways(configuration, exceptions, propName) {
const isException = exceptions.has(propName);
if (configuration === ALWAYS) {
return !isException;
}
return isException;
}

/**
* @param {string} configuration
* @param {Set<string>} exceptions
* @param {string} propName
* @returns {boolean} propName
*/
function isNever(configuration, exceptions, propName) {
const isException = exceptions.has(propName);
if (configuration === NEVER) {
Expand Down Expand Up @@ -109,7 +123,10 @@ module.exports = {
const propName = node.name && node.name.name;
const value = node.value;

if (isAlways(configuration, exceptions, propName) && value === null) {
if (
isAlways(configuration, exceptions, propName)
&& value === null
) {
const data = getErrorData(exceptions);
const messageId = data.exceptionsMessage ? 'setBoolean' : 'setBoolean_noMessage';
report(context, messages[messageId], messageId, {
Expand All @@ -120,7 +137,12 @@ module.exports = {
},
});
}
if (isNever(configuration, exceptions, propName) && value && value.type === 'JSXExpressionContainer' && value.expression.value === true) {
if (
isNever(configuration, exceptions, propName)
&& value
&& value.type === 'JSXExpressionContainer'
&& value.expression.value === true
) {
const data = getErrorData(exceptions);
const messageId = data.exceptionsMessage ? 'omitBoolean' : 'omitBoolean_noMessage';
report(context, messages[messageId], messageId, {
Expand Down

0 comments on commit b714407

Please sign in to comment.