Skip to content

Commit

Permalink
feat(@nomiclabs/hardhat-solhint): load .solhintignore file
Browse files Browse the repository at this point in the history
  • Loading branch information
yhuard authored and schaable committed May 13, 2024
1 parent 839b4d2 commit bcb688f
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/dull-spies-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomiclabs/hardhat-solhint": minor
---

Load .solhintignore file.
28 changes: 26 additions & 2 deletions packages/hardhat-solhint/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from "fs";
import { subtask, task } from "hardhat/config";
import { NomicLabsHardhatPluginError } from "hardhat/internal/core/errors";
import { join } from "path";
import { join, relative } from "path";

function getDefaultConfig() {
return {
Expand Down Expand Up @@ -43,6 +43,19 @@ async function hasConfigFile(rootDirectory: string) {
return false;
}

function readIgnore(rootDirectory: string) {
try {
return fs
.readFileSync(join(rootDirectory, ".solhintignore"))
.toString()
.split("\n")
.map((i) => i.trim())
.filter(Boolean);
} catch (e) {
return [];
}
}

async function getSolhintConfig(rootDirectory: string) {
let solhintConfig;
const {
Expand Down Expand Up @@ -73,6 +86,14 @@ async function getSolhintConfig(rootDirectory: string) {
);
}

const configExcludeFiles = Array.isArray(solhintConfig.excludedFiles)
? solhintConfig.excludedFiles
: [];
solhintConfig.excludedFiles = [
...configExcludeFiles,
...readIgnore(rootDirectory),
];

return solhintConfig;
}

Expand All @@ -84,7 +105,10 @@ function printReport(reports: any) {
subtask("hardhat-solhint:run-solhint", async (_, { config }) => {
const { processPath } = require("solhint/lib/index");
return processPath(
join(config.paths.sources, "**", "*.sol").replace(/\\/g, "/"),
relative(
config.paths.root,
join(config.paths.sources, "**", "*.sol")
).replace(/\\/g, "/"),
await getSolhintConfig(config.paths.root)
);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "solhint:all",
"excludedFiles": ["contracts/Greeter3.sol"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
contracts/Greeter2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pragma solidity ^0.5.1;


contract Greeter {

string greeting;
string bad;
constructor(string memory _greeting) public {
greeting = _greeting;
bad = "baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaad";
}

function greet() public view returns (string memory) {
return greeting;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pragma solidity ^0.5.1;

contract Greeter2 {
string greeting;
string bad;

constructor(string memory _greeting) public {
greeting = _greeting;
bad = "baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaad";
}

function greet() public view returns (string memory) {
return greeting;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pragma solidity ^0.5.1;

contract Greeter3 {
string greeting;
string bad;

constructor(string memory _greeting) public {
greeting = _greeting;
bad = "baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaad";
}

function greet() public view returns (string memory) {
return greeting;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require("../../../src/index");

module.exports = {
solidity: "0.5.15",
};
26 changes: 26 additions & 0 deletions packages/hardhat-solhint/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,32 @@ describe("Solhint plugin", function () {
});
});

describe("Project with .solhintignore file", function () {
useEnvironment("solhintignore-project");

it("should not return a report for the ignored files", async function () {
const reports = await this.env.run("hardhat-solhint:run-solhint");
// Greeter.sol is not ignored, Solhint should return a report
assert.isTrue(
reports.some((report: any) =>
report.file.includes("contracts/Greeter.sol")
)
);
// Greeter2.sol is ignored in the .solhintignore file, Solhint should not return a report
assert.isFalse(
reports.some((report: any) =>
report.file.includes("contracts/Greeter2.sol")
)
);
// Greeter3.sol is ignored in the .solhint.json file, Solhint should not return a report
assert.isFalse(
reports.some((report: any) =>
report.file.includes("contracts/Greeter2.sol")
)
);
});
});

describe("Project with no solhint config", function () {
useEnvironment("no-config-project");

Expand Down

0 comments on commit bcb688f

Please sign in to comment.