UNPKG

@slippy-lint/slippy

Version:

A simple but powerful linter for Solidity

56 lines 1.93 kB
import path from "node:path"; import fs from "node:fs/promises"; import { createConfigLoader, findSlippyConfigPath } from "../config.js"; import { Linter } from "../linter.js"; import { SlippyError, SlippyFileNotFoundError, } from "../errors.js"; import workerpool from "workerpool"; export default async function runLinter(sourceId, fix, customConfigPath) { try { let configPath = customConfigPath; if (configPath === undefined) { configPath = await findSlippyConfigPath(process.cwd()); } return await internalRunLinter(sourceId, configPath, fix); } catch (error) { if (SlippyError.isSlippyError(error)) { return { code: error.code, message: error.message, hint: error.hint, }; } throw error; } } async function internalRunLinter(sourceId, configPath, fix) { const configLoader = await createConfigLoader(configPath); const sourceIdToAbsolutePath = {}; const runner = new Linter(configLoader); try { const absolutePath = path.resolve(process.cwd(), sourceId); sourceIdToAbsolutePath[sourceId] = absolutePath; const file = { filePath: sourceId, content: await fs.readFile(absolutePath, "utf8"), }; const [lintResult] = await runner.lintFiles([file], { fix }); if (fix && lintResult.fixedContent !== undefined) { await fs.writeFile(absolutePath, lintResult.fixedContent); } return { diagnostics: lintResult.diagnostics, sourceIdToAbsolutePath, }; } catch (error) { if (error instanceof Error && "code" in error && error.code === "ENOENT") { throw new SlippyFileNotFoundError(sourceId); } throw error; } } workerpool.worker({ runLinter, }); //# sourceMappingURL=worker.js.map