UNPKG

eslint-plugin-unicorn

Version:
52 lines (41 loc) 1.09 kB
import {forEachFixOrProblem} from './utilities.js'; /** @import * as ESLint from 'eslint'; */ class FixAbortError extends Error { name = 'FixAbortError'; } const fixOptions = { abort() { throw new FixAbortError('Fix aborted.'); }, }; /** @typedef {ESLint.Rule.ReportFixer | undefined} EslintReportFixer @typedef {EslintReportFixer | IterableIterator<EslintReportFixer>} UnicornReportFixer @typedef {(fixer: ESLint.Rule.RuleFixer, options: typeof fixOptions) => UnicornReportFixer} UnicornRuleFixer */ /** Convert Unicorn style fix function to ESLint style fix function @param {UnicornRuleFixer} fix @returns {ESLint.Rule.RuleFixer} */ export default function toEslintRuleFixer(fix) { /** @param {UnicornReportFixer} fixer */ return fixer => { const unicornReport = fix(fixer, fixOptions); const eslintReport = []; try { forEachFixOrProblem(unicornReport, eslintFix => { eslintReport.push(eslintFix); }); return eslintReport; } catch (error) { if (error instanceof FixAbortError) { return; } /* c8 ignore next */ throw error; } }; }