Skip to content

Commit

Permalink
Merge pull request #3658 from NomicFoundation/plugin-error-eslint
Browse files Browse the repository at this point in the history
add eslint rule for hardhat plugin errors
  • Loading branch information
alcuadrado committed Feb 9, 2023
2 parents 0edcf75 + 5a9b9a3 commit ccf8841
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-knives-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomiclabs/eslint-plugin-hardhat-internal-rules": patch
---

add eslint rule for hardhat plugin errors
20 changes: 13 additions & 7 deletions config/eslint/eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ module.exports = {
},
{
selector: ["objectLiteralProperty"],
format: null
format: null,
},
{
selector: ["objectLiteralMethod"],
Expand Down Expand Up @@ -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",
{
Expand Down Expand Up @@ -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"],
},
],
},
};
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 HardhatPluginError 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 ccf8841

Please sign in to comment.