diff --git a/config/eslint/eslintrc.js b/config/eslint/eslintrc.js index 9b563bae71..aec56414ae 100644 --- a/config/eslint/eslintrc.js +++ b/config/eslint/eslintrc.js @@ -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", diff --git a/packages/eslint-plugin/index.js b/packages/eslint-plugin/index.js index 8d75155512..26cc501e2a 100644 --- a/packages/eslint-plugin/index.js +++ b/packages/eslint-plugin/index.js @@ -1,4 +1,4 @@ -const { onlyHardhatErrorRule } = require("./onlyHardhatErrorRule"); +const { onlyHardhatErrorRule, onlyHardhatPluginErrorRule } = require("./onlyHardhatErrorRule"); const rules = { "only-hardhat-error": { @@ -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 }; diff --git a/packages/eslint-plugin/onlyHardhatErrorRule.js b/packages/eslint-plugin/onlyHardhatErrorRule.js index ea90694928..188bfe2940 100644 --- a/packages/eslint-plugin/onlyHardhatErrorRule.js +++ b/packages/eslint-plugin/onlyHardhatErrorRule.js @@ -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); @@ -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 }