UNPKG

@plugjs/eslint

Version:

ESLint Plugin for the PlugJS Build System =========================================

98 lines (97 loc) 3.81 kB
// eslint.ts import { assert } from "@plugjs/plug"; import { BuildFailure } from "@plugjs/plug/asserts"; import { readFile } from "@plugjs/plug/fs"; import { $p, $grn, $ylw, ERROR, WARN, $gry } from "@plugjs/plug/logging"; import { getCurrentWorkingDirectory, resolveAbsolutePath, resolveDirectory, resolveFile } from "@plugjs/plug/paths"; import { ESLint as RealESLint } from "eslint"; var ESLint = class { _options; constructor(arg = {}) { this._options = typeof arg === "string" ? { configFile: arg } : arg; } async pipe(files, context) { const { directory, configFile, ingoreDeprecatedRules, warnIgnored } = this._options; const cwd = directory ? context.resolve(directory) : getCurrentWorkingDirectory(); assert(resolveDirectory(cwd), `ESLint directory ${$p(cwd)} does not exist`); const overrideConfigFile = configFile ? context.resolve(configFile) : void 0; if (overrideConfigFile) { assert(resolveFile(overrideConfigFile), `ESLint configuration ${$p(overrideConfigFile)} does not exist`); } const eslint = new RealESLint({ globInputPaths: false, // we already have all globs resolved overrideConfigFile, // if any override config file was supplied... cwd // current working directory for eslint (where everything starts) }); const paths = [...files.absolutePaths()]; const promises = paths.map(async (filePath) => { const code = await readFile(filePath, "utf-8"); return eslint.lintText(code, { filePath, warnIgnored: !!warnIgnored }); }); const settlements = await Promise.allSettled(promises); const summary = settlements.reduce((summary2, settlement, i) => { if (settlement.status === "rejected") { context.log.error("Error linting", $p(paths[i]), settlement.reason); summary2.failures++; return summary2; } summary2.results.push(...settlement.value); return summary2; }, { results: [], failures: 0 }); const { results, failures } = summary; if (failures) throw BuildFailure.fail(); const report = context.log.report("ESLint Report"); const deprecated = {}; for (const result of results) { const { filePath, source, messages } = result; const file = resolveAbsolutePath(getCurrentWorkingDirectory(), filePath); for (const deprecation of result.usedDeprecatedRules) { deprecated[deprecation.ruleId] = deprecation.replacedBy; } for (const record of messages) { const { severity, message: msg, ruleId: tags, suggestions = [], line, column, endLine = line, endColumn = column + 1 } = record; const message = [msg]; for (const suggestion of suggestions) { message.push(`- ${suggestion.desc}`); } const level = severity < 2 ? WARN : ERROR; const length = endLine === line ? endColumn - column : -1; report.add({ level, message, tags, line, column, length, file, source }); } } if (!ingoreDeprecatedRules) { for (const [rule, replacedBy] of Object.entries(deprecated)) { if (replacedBy.length) { const replacements = replacedBy.map($grn); replacements.unshift(""); const repl = replacements.join(` ${$gry("*")} `); report.add({ level: WARN, message: `Rule ${$ylw(rule)} was deprecated and replaced by ${repl}` }); } else { report.add({ level: WARN, message: `Rule ${$ylw(rule)} was deprecated without replacement` }); } } } report.done(this._options.showSources); context.log.notice("ESLint processed", files.length, "files"); } }; export { ESLint }; //# sourceMappingURL=eslint.mjs.map