UNPKG

@morev/eslint-disable-autofix

Version:

Utility to disable autofix for specific ESLint rules

111 lines (107 loc) 4.49 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { createDisableAutofix: () => createDisableAutofix, disableAutofix: () => disableAutofix }); module.exports = __toCommonJS(index_exports); var import_utils = require("@morev/utils"); var import_use_at_your_own_risk = __toESM(require("eslint/use-at-your-own-risk"), 1); // src/utils.ts var import_eslint_rule_composer = __toESM(require("eslint-rule-composer"), 1); var createNonFixableRule = (rule) => { if (!rule) return null; return import_eslint_rule_composer.default.mapReports( Object.create(rule), (problem) => { problem.fix = null; return problem; } ); }; var omitMutable = (source, keys) => { keys.forEach((key) => delete source[key]); }; // src/index.ts var internalDisableAutofix = (options) => { const { configurations, prefix } = options; const allNoAutofixRules = configurations.reduce((acc, configuration) => { if ((0, import_utils.isEmpty)(configuration.rules)) return acc; const rulesToDisableAutofix = import_utils.tsObject.fromEntries( import_utils.tsObject.entries(configuration.rules).filter(([ruleName, ruleValue]) => ruleName.startsWith(prefix)) ); if ((0, import_utils.isEmpty)(rulesToDisableAutofix)) return acc; const cleanRules = import_utils.tsObject.fromEntries( import_utils.tsObject.entries(rulesToDisableAutofix).map(([ruleName, ruleValue]) => [ ruleName.replace(`${prefix}/`, ""), ruleValue ]) ); omitMutable(configuration.rules, import_utils.tsObject.keys(rulesToDisableAutofix)); Object.assign(configuration.rules, cleanRules); acc.push(...import_utils.tsObject.keys(cleanRules)); return acc; }, []); if ((0, import_utils.isEmpty)(allNoAutofixRules)) return configurations; const allPlugins = configurations.reduce((acc, configuration) => { import_utils.tsObject.entries(configuration.plugins ?? {}).forEach(([pluginMappingName, pluginObject]) => { acc[pluginMappingName] ??= pluginObject; }); return acc; }, {}); const eslintRules = import_use_at_your_own_risk.default.builtinRules; eslintRules._set = Map.prototype.set.bind(eslintRules); allNoAutofixRules.forEach((fullRuleName) => { const [_, pluginName, ruleName] = fullRuleName.match(/(?:(.*?)\/)?(.*)/); if (pluginName && allPlugins[pluginName]?.rules?.[ruleName]) { allPlugins[pluginName].rules[ruleName] = createNonFixableRule(allPlugins[pluginName].rules[ruleName]); return; } if (eslintRules.has(fullRuleName)) { const originalRule = eslintRules.get(fullRuleName); eslintRules._set(fullRuleName, () => createNonFixableRule(originalRule)); } }); return configurations; }; var createDisableAutofix = (options) => { const { prefix = "no-autofix" } = options ?? {}; return (configurations) => internalDisableAutofix({ configurations, prefix }); }; var disableAutofix = createDisableAutofix(); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createDisableAutofix, disableAutofix });