eslint-plugin-safeguard
Version:
A custom ESLint plugin that provides multiple rules to enforce best practices and code safety.
48 lines (46 loc) • 1.2 kB
JavaScript
const meta = {
docs: {
description: "Ensure that await expressions are inside try-catch blocks",
recommended: true
},
schema: [],
type: "problem"
};
const create = (context) => {
const isInTryBlock = (awaitNode, tryBlock) => {
let currentNode = awaitNode;
while (currentNode) {
if (currentNode === tryBlock) {
return true;
}
currentNode = currentNode.parent;
}
return false;
};
return {
AwaitExpression(node) {
let currentNode = node;
while (currentNode) {
const parent = currentNode.parent;
if (parent && parent.type === "TryStatement") {
const tryStatement = parent;
if (isInTryBlock(node, tryStatement.block)) {
if (tryStatement.handler) return;
context.report({
message: "Await expressions should be inside a try-catch block.",
node
});
return;
}
}
currentNode = parent;
}
context.report({
message: "Await expressions should be inside a try-catch block.",
node
});
}
};
};
const rule = { create, meta };
export { rule as default };