Skip to content

Commit

Permalink
Merge pull request #5206 from NomicFoundation/yhuard-load-solhintignore
Browse files Browse the repository at this point in the history
feat(@nomiclabs/hardhat-solhint): load .solhintignore file
  • Loading branch information
schaable authored May 13, 2024
2 parents 839b4d2 + b0c40ab commit 35a2a16
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 5 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
---

Added support for `.solhintignore` files (thanks @yhuard!)
40 changes: 35 additions & 5 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 @@ -83,10 +104,19 @@ 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, "/"),
await getSolhintConfig(config.paths.root)
);

// Create a glob pattern that matches all the .sol files within the sources folder
const solFilesGlob = join(config.paths.sources, "**", "*.sol");

// Make glob pattern relative to Hardhat's root directory
// See https://github.com/kaelzhang/node-ignore/tree/5.2.4#1-pathname-should-be-a-pathrelatived-pathname
const relativeGlob = relative(config.paths.root, solFilesGlob);

// Fix for Windows users: replace back-slashes with forward-slashes
// See https://github.com/isaacs/node-glob/tree/v8.0.3#windows
const normalizedGlob = relativeGlob.replace(/\\/g, "/");

return processPath(normalizedGlob, await getSolhintConfig(config.paths.root));
});

task("check", async (_, { run }, runSuper) => {
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 35a2a16

Please sign in to comment.