Skip to content

Commit

Permalink
remove validate function name
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Sep 9, 2024
1 parent a1b4f7d commit d412470
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 103 deletions.
22 changes: 1 addition & 21 deletions rules/better-boolean-variable-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}}`.',
};

Expand Down Expand Up @@ -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)
Expand All @@ -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') {
Expand Down
4 changes: 2 additions & 2 deletions rules/utils/is-boolean.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
);
}

Expand Down
2 changes: 1 addition & 1 deletion rules/utils/rename-variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ function * renameVariable(sourceCode, scope, fixer, identifier, variableName) {
export { foo }
```
After:
```js
const bar = 1
Expand Down
8 changes: 0 additions & 8 deletions test/better-boolean-variable-names.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
72 changes: 1 addition & 71 deletions test/snapshots/better-boolean-variable-names.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file modified test/snapshots/better-boolean-variable-names.mjs.snap
Binary file not shown.

0 comments on commit d412470

Please sign in to comment.