-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
index.ts
130 lines (112 loc) · 3.3 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import * as fs from "fs";
import { subtask, task } from "hardhat/config";
import { NomicLabsHardhatPluginError } from "hardhat/internal/core/errors";
import { join, relative } from "path";
function getDefaultConfig() {
return {
extends: ["solhint:default"],
};
}
function getFormatter(formatterName = "stylish") {
try {
const formatterPath = require.resolve(
`solhint/lib/formatters/${formatterName}`
);
return require(formatterPath);
} catch (ex: any) {
throw new NomicLabsHardhatPluginError(
"@nomiclabs/hardhat-solhint",
`An error occurred loading the solhint formatter ${formatterName}`,
ex
);
}
}
async function hasConfigFile(rootDirectory: string) {
const files = [
".solhint.json",
".solhintrc",
".solhintrc.json",
".solhintrc.yaml",
".solhintrc.yml",
".solhintrc.js",
"solhint.config.js",
];
for (const file of files) {
if (fs.existsSync(join(rootDirectory, file))) {
return true;
}
}
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 {
loadConfig,
applyExtends,
} = require("solhint/lib/config/config-file");
if (await hasConfigFile(rootDirectory)) {
try {
solhintConfig = await loadConfig();
} catch (err: any) {
throw new NomicLabsHardhatPluginError(
"@nomiclabs/hardhat-solhint",
"An error occurred when loading your solhint config.",
err
);
}
} else {
solhintConfig = getDefaultConfig();
}
try {
solhintConfig = applyExtends(solhintConfig);
} catch (err: any) {
throw new NomicLabsHardhatPluginError(
"@nomiclabs/hardhat-solhint",
"An error occurred when processing your solhint config.",
err
);
}
const configExcludeFiles = Array.isArray(solhintConfig.excludedFiles)
? solhintConfig.excludedFiles
: [];
solhintConfig.excludedFiles = [
...configExcludeFiles,
...readIgnore(rootDirectory),
];
return solhintConfig;
}
function printReport(reports: any) {
const formatter = getFormatter();
console.log(formatter(reports));
}
subtask("hardhat-solhint:run-solhint", async (_, { config }) => {
const { processPath } = require("solhint/lib/index");
// 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) => {
if (runSuper.isDefined) {
await runSuper();
}
const reports = await run("hardhat-solhint:run-solhint");
printReport(reports);
});