UNPKG

@elsikora/eslint-config

Version:

ESLint configuration vision of ElsiKora

63 lines (60 loc) 2.72 kB
import { PLUGIN_MAP } from '../constant/utility/plugin-map.constant.js'; /** * Formats ESLint configurations by remapping plugin names and rule prefixes according to PLUGIN_MAP. * @param {Array<Linter.Config>} configs - Array of ESLint flat configurations to process * @returns {Array<Linter.Config>} Array of formatted ESLint configurations with updated plugin names and rule prefixes */ function formatConfig(configs) { const formattedConfigs = []; for (const config of configs) { // eslint-disable-next-line @elsikora/sonar/no-unused-vars,@elsikora/typescript/naming-convention const { plugins: __, rules: _, ...restConfig } = config; const newConfig = { ...restConfig }; if (config.plugins) { const newPlugins = {}; for (const [oldName, newName] of Object.entries(PLUGIN_MAP).sort((a, b) => b[0].length - a[0].length)) { if (config.plugins[oldName]) { newPlugins[newName] = config.plugins[oldName]; } } for (const [key, value] of Object.entries(config.plugins)) { if (!Object.keys(PLUGIN_MAP).includes(key)) { newPlugins[key] = value; } } newConfig.plugins = newPlugins; } if (config.rules) { const newRules = {}; for (const rule of Object.keys(config.rules)) { let isReplaced = false; for (const [oldName, newName] of Object.entries(PLUGIN_MAP).sort((a, b) => b[0].length - a[0].length)) { const oldPrefix = `${oldName}/`; if (rule.startsWith(oldPrefix) && !isReplaced) { const newRule = rule.replace(oldPrefix, `${newName}/`); newRules[newRule] = config.rules[rule]; isReplaced = true; break; } } if (!isReplaced) { newRules[rule] = config.rules[rule]; } } newConfig.rules = newRules; } if (config.language) { for (const [oldName, newName] of Object.entries(PLUGIN_MAP).sort((a, b) => b[0].length - a[0].length)) { const oldPrefix = `${oldName}/`; if (config.language.startsWith(oldPrefix)) { newConfig.language = config.language.replace(oldPrefix, `${newName}/`); break; } } } formattedConfigs.push(newConfig); } return formattedConfigs; } export { formatConfig }; //# sourceMappingURL=format-config.utility.js.map