From 42560688fefe5b13bd3797e4ec9ed59bef704018 Mon Sep 17 00:00:00 2001 From: zoeyTM Date: Wed, 8 Feb 2023 23:47:17 -0500 Subject: [PATCH 1/4] add eslint rule for hardhat plugin errors --- config/eslint/eslintrc.js | 1 + packages/eslint-plugin/index.js | 12 ++++++++- .../eslint-plugin/onlyHardhatErrorRule.js | 26 ++++++++++++++++++- 3 files changed, 37 insertions(+), 2 deletions(-) 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 } From 337456b8a0adb837a4da50fbcdec015251e67328 Mon Sep 17 00:00:00 2001 From: Zoey Date: Thu, 9 Feb 2023 00:04:13 -0500 Subject: [PATCH 2/4] Create cyan-knives-study.md --- .changeset/cyan-knives-study.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/cyan-knives-study.md 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 From 471a70f2b31bb7813cd3825b1fc5b10fdb31fff0 Mon Sep 17 00:00:00 2001 From: Zoey Date: Thu, 9 Feb 2023 10:50:59 -0500 Subject: [PATCH 3/4] Update packages/eslint-plugin/onlyHardhatErrorRule.js Co-authored-by: Patricio Palladino --- packages/eslint-plugin/onlyHardhatErrorRule.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/eslint-plugin/onlyHardhatErrorRule.js b/packages/eslint-plugin/onlyHardhatErrorRule.js index 188bfe2940..e6aa912980 100644 --- a/packages/eslint-plugin/onlyHardhatErrorRule.js +++ b/packages/eslint-plugin/onlyHardhatErrorRule.js @@ -33,7 +33,7 @@ function onlyHardhatPluginErrorRule(context) { context.report({ node, - message: `Only HardhatError must be thrown, ${exceptionName} found.`, + message: `Only HardhatPluginError must be thrown, ${exceptionName} found.`, }); } }, From 5a9b9a33b00844ec4957b851c1f7ef47bf61cedc Mon Sep 17 00:00:00 2001 From: zoeyTM Date: Thu, 9 Feb 2023 10:57:36 -0500 Subject: [PATCH 4/4] remove unnecessary line --- config/eslint/eslintrc.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/config/eslint/eslintrc.js b/config/eslint/eslintrc.js index aec56414ae..0d563e5981 100644 --- a/config/eslint/eslintrc.js +++ b/config/eslint/eslintrc.js @@ -13,7 +13,6 @@ module.exports = { "@typescript-eslint", ], rules: { - "@nomiclabs/only-hardhat-plugin-error": "off", "@typescript-eslint/adjacent-overload-signatures": "error", "@typescript-eslint/array-type": [ "error", @@ -92,7 +91,7 @@ module.exports = { }, { selector: ["objectLiteralProperty"], - format: null + format: null, }, { selector: ["objectLiteralMethod"], @@ -138,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", { @@ -220,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"], + }, + ], }, };