Skip to content

Commit

Permalink
add eslint rule for hardhat plugin errors
Browse files Browse the repository at this point in the history
  • Loading branch information
zoeyTM committed Feb 9, 2023
1 parent 95328cc commit 4256068
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions config/eslint/eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
"@typescript-eslint",
],
rules: {
"@nomiclabs/only-hardhat-plugin-error": "off",
"@typescript-eslint/adjacent-overload-signatures": "error",
"@typescript-eslint/array-type": [
"error",
Expand Down
12 changes: 11 additions & 1 deletion packages/eslint-plugin/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { onlyHardhatErrorRule } = require("./onlyHardhatErrorRule");
const { onlyHardhatErrorRule, onlyHardhatPluginErrorRule } = require("./onlyHardhatErrorRule");

const rules = {
"only-hardhat-error": {
Expand All @@ -11,6 +11,16 @@ const rules = {
},
},
},
"only-hardhat-plugin-error": {
create: onlyHardhatPluginErrorRule,
meta: {
type: "problem",
schema: [],
docs: {
description: "Enforces that only HardhatPluginError is thrown.",
},
},
}
};

module.exports = { rules };
26 changes: 25 additions & 1 deletion packages/eslint-plugin/onlyHardhatErrorRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ function onlyHardhatErrorRule(context) {
};
}

function onlyHardhatPluginErrorRule(context) {
const parserServices = ESLintUtils.getParserServices(context)
const checker = parserServices.program.getTypeChecker();

return {
ThrowStatement(node) {
const expression = parserServices.esTreeNodeToTSNodeMap.get(node.argument);

if (!isHardhatPluginError(expression, checker)) {
const exceptionName = getExpressionClassName(expression, checker);

context.report({
node,
message: `Only HardhatError must be thrown, ${exceptionName} found.`,
});
}
},
};
}

function getExpressionClassName(expression, tc) {
const exceptionType = tc.getTypeAtLocation(expression);

Expand All @@ -34,4 +54,8 @@ function isHardhatError(expression, tc) {
return getExpressionClassName(expression, tc) === "HardhatError";
}

module.exports = { onlyHardhatErrorRule }
function isHardhatPluginError(expression, tc) {
return getExpressionClassName(expression, tc) === "HardhatPluginError";
}

module.exports = { onlyHardhatErrorRule, onlyHardhatPluginErrorRule }

0 comments on commit 4256068

Please sign in to comment.