diff --git a/packages/ajel-core/package.json b/packages/ajel-core/package.json index 34251ff..2bbf48d 100644 --- a/packages/ajel-core/package.json +++ b/packages/ajel-core/package.json @@ -1,7 +1,7 @@ { "name": "ajel", "description": "Ajel allows you to handle errors similarly to Golang.", - "version": "0.0.5", + "version": "0.0.6", "main": "./dist/index.js", "module": "./dist/index.mjs", "types": "./dist/index.d.ts", diff --git a/packages/eslint-plugin-ajel/package.json b/packages/eslint-plugin-ajel/package.json index 24a1379..bb84094 100644 --- a/packages/eslint-plugin-ajel/package.json +++ b/packages/eslint-plugin-ajel/package.json @@ -1,6 +1,6 @@ { "name": "eslint-plugin-ajel", - "version": "0.0.5", + "version": "0.0.6", "description": "Eslint rules for Ajel, the library that allows you to handle errors similarly to Golang", "engines": { "node": ">=8.10.0" diff --git a/packages/eslint-plugin-ajel/src/rules/ajel-require-error-handling.ts b/packages/eslint-plugin-ajel/src/rules/ajel-require-error-handling.ts index 5baa8e2..c5a3fbc 100644 --- a/packages/eslint-plugin-ajel/src/rules/ajel-require-error-handling.ts +++ b/packages/eslint-plugin-ajel/src/rules/ajel-require-error-handling.ts @@ -6,7 +6,7 @@ import { isUsedVariable } from '../utils/isUsed'; type Options = [ { ajelAlias?: string; - } + }, ]; type MessageIds = 'requireErrorHandling'; @@ -42,6 +42,28 @@ const rule = createRule({ create: (context, [{ ajelAlias }]) => { let errorVariable: TSESTree.Identifier | null = null; + function reportIfErrorVariableUnused() { + if (errorVariable) { + // Get the scope of the identifier + const scope = context.getScope(); + + // Get the variable associated with the identifier + const variable = scope.variables.find((v) => + v.identifiers.includes(errorVariable as TSESTree.Identifier) + ); + + if (variable && !isUsedVariable(variable)) { + context.report({ + node: errorVariable, + messageId: 'requireErrorHandling', + data: { + ajelAlias, + }, + }); + } + } + } + return { VariableDeclaration(node: TSESTree.VariableDeclaration): void { if (hasAjelCallExpressionChild(node, ajelAlias)) { @@ -54,26 +76,11 @@ const rule = createRule({ } } }, + 'FunctionDeclaration:exit'(): void { + reportIfErrorVariableUnused(); + }, 'Program:exit'(): void { - if (errorVariable) { - // Get the scope of the identifier - const scope = context.getScope(); - - // Get the variable associated with the identifier - const variable = scope.variables.find((v) => - v.identifiers.includes(errorVariable as TSESTree.Identifier) - ); - - if (variable && !isUsedVariable(variable)) { - context.report({ - node: errorVariable, - messageId: 'requireErrorHandling', - data: { - ajelAlias, - }, - }); - } - } + reportIfErrorVariableUnused(); }, }; }, diff --git a/packages/eslint-plugin-ajel/tests/rules/ajel-require-error-handling.ts b/packages/eslint-plugin-ajel/tests/rules/ajel-require-error-handling.ts index 07b9892..6a968f0 100644 --- a/packages/eslint-plugin-ajel/tests/rules/ajel-require-error-handling.ts +++ b/packages/eslint-plugin-ajel/tests/rules/ajel-require-error-handling.ts @@ -16,11 +16,21 @@ tester.run('ajel-require-error-handling', rule, { { code: ` const [res, _err] = await ajel(Promise.resolve(1)); - if (_err) { + if (err) { return _err; } `, }, + { + code: ` + async function test() { + const [res, _err] = await ajel(Promise.resolve(1)); + if (err) { + return _err; + } + } + `, + }, { code: ` const [res, err] = await blimpy(Promise.resolve(1)); @@ -37,5 +47,18 @@ tester.run('ajel-require-error-handling', rule, { `, errors: [{ messageId: 'requireErrorHandling' }], }, + { + code: ` + async function test() { + const [res, _err] = await ajel(Promise.resolve(1)); + } + `, + errors: [{ messageId: 'requireErrorHandling' }], + }, + { + code: `let [res, _err] = await ajel(Promise.resolve(1)); + `, + errors: [{ messageId: 'requireErrorHandling' }], + }, ], });