diff --git a/.changeset/cyan-knives-study.md b/.changeset/cyan-knives-study.md new file mode 100644 index 0000000000..54c0799ae2 --- /dev/null +++ b/.changeset/cyan-knives-study.md @@ -0,0 +1,5 @@ +--- +"@nomiclabs/eslint-plugin-hardhat-internal-rules": patch +--- + +add eslint rule for hardhat plugin errors diff --git a/config/eslint/eslintrc.js b/config/eslint/eslintrc.js index 9b563bae71..0d563e5981 100644 --- a/config/eslint/eslintrc.js +++ b/config/eslint/eslintrc.js @@ -91,7 +91,7 @@ module.exports = { }, { selector: ["objectLiteralProperty"], - format: null + format: null, }, { selector: ["objectLiteralMethod"], @@ -137,9 +137,12 @@ module.exports = { "@typescript-eslint/prefer-function-type": "error", "@typescript-eslint/prefer-namespace-keyword": "error", "@typescript-eslint/restrict-plus-operands": "error", - "@typescript-eslint/restrict-template-expressions": ["error", { - allowAny: true, - }], + "@typescript-eslint/restrict-template-expressions": [ + "error", + { + allowAny: true, + }, + ], "@typescript-eslint/strict-boolean-expressions": [ "error", { @@ -219,8 +222,11 @@ module.exports = { }, ], "use-isnan": "error", - "no-restricted-imports": ["error", { - patterns: ["hardhat/src", "@nomiclabs/*/src"] - }], + "no-restricted-imports": [ + "error", + { + patterns: ["hardhat/src", "@nomiclabs/*/src"], + }, + ], }, }; 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..e6aa912980 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 HardhatPluginError 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 }