Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsx-boolean-value.js documentation #3344

Merged
merged 1 commit into from Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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