UNPKG

@plugjs/eslint

Version:

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

123 lines (121 loc) 5.08 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // eslint.ts var eslint_exports = {}; __export(eslint_exports, { ESLint: () => ESLint }); module.exports = __toCommonJS(eslint_exports); var import_plug = require("@plugjs/plug"); var import_asserts = require("@plugjs/plug/asserts"); var import_fs = require("@plugjs/plug/fs"); var import_logging = require("@plugjs/plug/logging"); var import_paths = require("@plugjs/plug/paths"); var import_eslint = require("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) : (0, import_paths.getCurrentWorkingDirectory)(); (0, import_plug.assert)((0, import_paths.resolveDirectory)(cwd), `ESLint directory ${(0, import_logging.$p)(cwd)} does not exist`); const overrideConfigFile = configFile ? context.resolve(configFile) : void 0; if (overrideConfigFile) { (0, import_plug.assert)((0, import_paths.resolveFile)(overrideConfigFile), `ESLint configuration ${(0, import_logging.$p)(overrideConfigFile)} does not exist`); } const eslint = new import_eslint.ESLint({ 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 (0, import_fs.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", (0, import_logging.$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 import_asserts.BuildFailure.fail(); const report = context.log.report("ESLint Report"); const deprecated = {}; for (const result of results) { const { filePath, source, messages } = result; const file = (0, import_paths.resolveAbsolutePath)((0, import_paths.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 ? import_logging.WARN : import_logging.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(import_logging.$grn); replacements.unshift(""); const repl = replacements.join(` ${(0, import_logging.$gry)("*")} `); report.add({ level: import_logging.WARN, message: `Rule ${(0, import_logging.$ylw)(rule)} was deprecated and replaced by ${repl}` }); } else { report.add({ level: import_logging.WARN, message: `Rule ${(0, import_logging.$ylw)(rule)} was deprecated without replacement` }); } } } report.done(this._options.showSources); context.log.notice("ESLint processed", files.length, "files"); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ESLint }); //# sourceMappingURL=eslint.cjs.map