diff --git a/rules/better-boolean-variable-names.js b/rules/better-boolean-variable-names.js index 5395fe8e55..9fec5814ea 100644 --- a/rules/better-boolean-variable-names.js +++ b/rules/better-boolean-variable-names.js @@ -6,7 +6,7 @@ const {isBooleanExpression, isBooleanTypeAnnotation, isFunctionReturnBoolean} = const MESSAGE_ID_ERROR = 'better-boolean-variable-names/error'; const MESSAGE_ID_SUGGESTION = 'better-boolean-variable-names/suggestion'; const messages = { - [MESSAGE_ID_ERROR]: 'Prefer readable Boolean variable names.', + [MESSAGE_ID_ERROR]: 'Prefer readable boolean variable names.', [MESSAGE_ID_SUGGESTION]: 'Replace `{{value}}` with `{{replacement}}`.', }; @@ -111,17 +111,6 @@ const create = context => { if (node.id.type === 'Identifier') { const variableName = node.id.name; - // Const foo = (): boolean => {} - // const foo = function () {} - if ( - ['FunctionExpression', 'ArrowFunctionExpression'].includes(node.init?.type) - && isFunctionReturnBoolean(context, node.init) - && !isValidBooleanVariableName(variableName) - ) { - report(context, node.id, variableName); - return; - } - if ( (isBooleanExpression(context, node.init) || isBooleanTypeAnnotation(node.id.typeAnnotation)) && !isValidBooleanVariableName(variableName) @@ -134,15 +123,6 @@ const create = context => { @param {import('estree').Function} node */ 'FunctionDeclaration, FunctionExpression, ArrowFunctionExpression'(node) { - // Validate function name - if (node.id?.type === 'Identifier') { - const variableName = node.id.name; - - if (isFunctionReturnBoolean(context, node) && !isValidBooleanVariableName(variableName)) { - report(context, node.id, variableName); - } - } - // Validate params for (const parameter of node.params) { if (parameter.type === 'Identifier') { diff --git a/rules/utils/is-boolean.js b/rules/utils/is-boolean.js index 9e18c7c0e4..61f51ea6b8 100644 --- a/rules/utils/is-boolean.js +++ b/rules/utils/is-boolean.js @@ -6,6 +6,7 @@ Determines whether a node is a Boolean Expression. @param {import('eslint').Rule.RuleContext} context @param {import('estree').Expression} node @param {number} [depth] - The current recursion depth. Users do not need to pass this parameter. +@returns {boolean} */ function isBooleanExpression(context, node, depth = 0) { if (!node) { @@ -81,8 +82,7 @@ function isBooleanExpression(context, node, depth = 0) { // Const isAdult = age >= 18 ? true : false; case 'ConditionalExpression': { return ( - isBooleanExpression(context, node.consequent, depth + 1) - && isBooleanExpression(context, node.alternate, depth + 1) + isBooleanExpression(context, node.consequent, depth + 1) && isBooleanExpression(context, node.alternate, depth + 1) ); } diff --git a/rules/utils/rename-variable.js b/rules/utils/rename-variable.js index 566d1e8fcf..c3716ff7bd 100644 --- a/rules/utils/rename-variable.js +++ b/rules/utils/rename-variable.js @@ -200,7 +200,7 @@ function * renameVariable(sourceCode, scope, fixer, identifier, variableName) { export { foo } ``` - + After: ```js const bar = 1 diff --git a/test/better-boolean-variable-names.mjs b/test/better-boolean-variable-names.mjs index 6f6770824e..c6fb6bc5fc 100644 --- a/test/better-boolean-variable-names.mjs +++ b/test/better-boolean-variable-names.mjs @@ -105,14 +105,6 @@ test.snapshot({ `, languageOptions: {parser: parsers.typescript}, }, - { - code: 'function completed(): boolean {}', - languageOptions: {parser: parsers.typescript}, - }, - { - code: 'const completed = (): boolean => {}', - languageOptions: {parser: parsers.typescript}, - }, { code: 'function download(url: string, showProgress: boolean) {}', languageOptions: {parser: parsers.typescript}, diff --git a/test/snapshots/better-boolean-variable-names.mjs.md b/test/snapshots/better-boolean-variable-names.mjs.md index 49eea1a307..73bfb0233e 100644 --- a/test/snapshots/better-boolean-variable-names.mjs.md +++ b/test/snapshots/better-boolean-variable-names.mjs.md @@ -907,77 +907,7 @@ Generated by [AVA](https://avajs.dev). 2 | const shouldDownloaded = isCompleted()␊ ` -## invalid(6): function completed(): boolean {} - -> Input - - `␊ - 1 | function completed(): boolean {}␊ - ` - -> Error 1/1 - - `␊ - > 1 | function completed(): boolean {}␊ - | ^^^^^^^^^ Prefer readable Boolean variable names.␊ - ␊ - --------------------------------------------------------------------------------␊ - Suggestion 1/5: Replace \`completed\` with \`isCompleted\`.␊ - 1 | function isCompleted(): boolean {}␊ - ␊ - --------------------------------------------------------------------------------␊ - Suggestion 2/5: Replace \`completed\` with \`wasCompleted\`.␊ - 1 | function wasCompleted(): boolean {}␊ - ␊ - --------------------------------------------------------------------------------␊ - Suggestion 3/5: Replace \`completed\` with \`hasCompleted\`.␊ - 1 | function hasCompleted(): boolean {}␊ - ␊ - --------------------------------------------------------------------------------␊ - Suggestion 4/5: Replace \`completed\` with \`canCompleted\`.␊ - 1 | function canCompleted(): boolean {}␊ - ␊ - --------------------------------------------------------------------------------␊ - Suggestion 5/5: Replace \`completed\` with \`shouldCompleted\`.␊ - 1 | function shouldCompleted(): boolean {}␊ - ` - -## invalid(7): const completed = (): boolean => {} - -> Input - - `␊ - 1 | const completed = (): boolean => {}␊ - ` - -> Error 1/1 - - `␊ - > 1 | const completed = (): boolean => {}␊ - | ^^^^^^^^^ Prefer readable Boolean variable names.␊ - ␊ - --------------------------------------------------------------------------------␊ - Suggestion 1/5: Replace \`completed\` with \`isCompleted\`.␊ - 1 | const isCompleted = (): boolean => {}␊ - ␊ - --------------------------------------------------------------------------------␊ - Suggestion 2/5: Replace \`completed\` with \`wasCompleted\`.␊ - 1 | const wasCompleted = (): boolean => {}␊ - ␊ - --------------------------------------------------------------------------------␊ - Suggestion 3/5: Replace \`completed\` with \`hasCompleted\`.␊ - 1 | const hasCompleted = (): boolean => {}␊ - ␊ - --------------------------------------------------------------------------------␊ - Suggestion 4/5: Replace \`completed\` with \`canCompleted\`.␊ - 1 | const canCompleted = (): boolean => {}␊ - ␊ - --------------------------------------------------------------------------------␊ - Suggestion 5/5: Replace \`completed\` with \`shouldCompleted\`.␊ - 1 | const shouldCompleted = (): boolean => {}␊ - ` - -## invalid(8): function download(url: string, showProgress: boolean) {} +## invalid(6): function download(url: string, showProgress: boolean) {} > Input diff --git a/test/snapshots/better-boolean-variable-names.mjs.snap b/test/snapshots/better-boolean-variable-names.mjs.snap index 15d43efb4e..c6d305acc7 100644 Binary files a/test/snapshots/better-boolean-variable-names.mjs.snap and b/test/snapshots/better-boolean-variable-names.mjs.snap differ