@nomiclabs/hardhat-solhint
Version:
Hardhat plugin for solhint
133 lines • 4.88 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const config_1 = require("hardhat/config");
const errors_1 = require("hardhat/internal/core/errors");
const path_1 = require("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) {
throw new errors_1.NomicLabsHardhatPluginError("@nomiclabs/hardhat-solhint", `An error occurred loading the solhint formatter ${formatterName}`, ex);
}
}
async function hasConfigFile(rootDirectory) {
const files = [
".solhint.json",
".solhintrc",
".solhintrc.json",
".solhintrc.yaml",
".solhintrc.yml",
".solhintrc.js",
"solhint.config.js",
];
for (const file of files) {
if (fs.existsSync((0, path_1.join)(rootDirectory, file))) {
return true;
}
}
return false;
}
function readIgnore(rootDirectory) {
try {
return fs
.readFileSync((0, path_1.join)(rootDirectory, ".solhintignore"))
.toString()
.split("\n")
.map((i) => i.trim())
.filter(Boolean);
}
catch (e) {
return [];
}
}
async function getSolhintConfig(rootDirectory) {
let solhintConfig;
const { loadConfig, applyExtends, } = require("solhint/lib/config/config-file");
if (await hasConfigFile(rootDirectory)) {
try {
solhintConfig = await loadConfig();
}
catch (err) {
throw new errors_1.NomicLabsHardhatPluginError("@nomiclabs/hardhat-solhint", "An error occurred when loading your solhint config.", err);
}
}
else {
solhintConfig = getDefaultConfig();
}
try {
solhintConfig = applyExtends(solhintConfig);
}
catch (err) {
throw new errors_1.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) {
const formatter = getFormatter();
console.log(formatter(reports));
}
(0, config_1.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 = (0, path_1.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 = (0, path_1.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));
});
(0, config_1.task)("check", async (_, { run }, runSuper) => {
if (runSuper.isDefined) {
await runSuper();
}
const reports = await run("hardhat-solhint:run-solhint");
printReport(reports);
const errorsCount = reports.reduce((acc, i) => {
return acc + i.errorCount;
}, 0);
if (errorsCount > 0) {
process.exitCode = 1;
return;
}
});
//# sourceMappingURL=index.js.map