Skip to content

Commit

Permalink
chore: updates to ajel-require-error-handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Handfish committed Jan 13, 2024
1 parent 422e7e9 commit 7705935
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/ajel-core/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-ajel/package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { isUsedVariable } from '../utils/isUsed';
type Options = [
{
ajelAlias?: string;
}
},
];
type MessageIds = 'requireErrorHandling';

Expand Down Expand Up @@ -42,6 +42,28 @@ const rule = createRule<Options, MessageIds>({
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)) {
Expand All @@ -54,26 +76,11 @@ const rule = createRule<Options, MessageIds>({
}
}
},
'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();
},
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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' }],
},
],
});

0 comments on commit 7705935

Please sign in to comment.