|
| 1 | +// @ts-check |
| 2 | +import * as eslint from "eslint" |
| 3 | + |
| 4 | +// eslint-disable-next-line @typescript-eslint/no-namespace -- ignore |
| 5 | +export namespace ESLint { |
| 6 | + export type LintResult = eslint.ESLint.LintResult |
| 7 | +} |
| 8 | +// eslint-disable-next-line @typescript-eslint/naming-convention -- ignore |
| 9 | +export const ESLint = eslint.ESLint || getESLintClassForV6() |
| 10 | +// export const ESLint = getESLintClassForV6() |
| 11 | + |
| 12 | +/** Build the ESLint class that ESLint v6 compatible. */ |
| 13 | +function getESLintClassForV6(): typeof eslint.ESLint { |
| 14 | + // eslint-disable-next-line @typescript-eslint/naming-convention -- ignore |
| 15 | + const CLIEngine = eslint.CLIEngine |
| 16 | + class ESLintForV6 { |
| 17 | + private readonly engine: eslint.CLIEngine |
| 18 | + |
| 19 | + public static get version() { |
| 20 | + return CLIEngine.version |
| 21 | + } |
| 22 | + |
| 23 | + public constructor(options?: eslint.ESLint.Options) { |
| 24 | + const { |
| 25 | + overrideConfig: { |
| 26 | + plugins, |
| 27 | + globals, |
| 28 | + rules, |
| 29 | + ...overrideConfig |
| 30 | + } = { |
| 31 | + plugins: [], |
| 32 | + globals: {}, |
| 33 | + rules: {}, |
| 34 | + }, |
| 35 | + fix, |
| 36 | + reportUnusedDisableDirectives, |
| 37 | + plugins: pluginsMap, |
| 38 | + ...otherOptions |
| 39 | + } = options || {} |
| 40 | + const newOptions: eslint.CLIEngine.Options = { |
| 41 | + fix: Boolean(fix), |
| 42 | + reportUnusedDisableDirectives: reportUnusedDisableDirectives |
| 43 | + ? reportUnusedDisableDirectives !== "off" |
| 44 | + : undefined, |
| 45 | + ...otherOptions, |
| 46 | + |
| 47 | + globals: globals |
| 48 | + ? Object.keys(globals).filter((n) => globals[n]) |
| 49 | + : undefined, |
| 50 | + plugins: plugins || [], |
| 51 | + rules: rules |
| 52 | + ? Object.entries(rules).reduce((o, [ruleId, opt]) => { |
| 53 | + if (opt) { |
| 54 | + o[ruleId] = opt |
| 55 | + } |
| 56 | + return o |
| 57 | + }, {} as NonNullable<eslint.CLIEngine.Options["rules"]>) |
| 58 | + : undefined, |
| 59 | + ...overrideConfig, |
| 60 | + } |
| 61 | + this.engine = new CLIEngine(newOptions) |
| 62 | + |
| 63 | + for (const [name, plugin] of Object.entries(pluginsMap || {})) { |
| 64 | + this.engine.addPlugin(name, plugin) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // eslint-disable-next-line @typescript-eslint/require-await -- ignore |
| 69 | + public async lintText( |
| 70 | + ...params: Parameters<eslint.ESLint["lintText"]> |
| 71 | + ): ReturnType<eslint.ESLint["lintText"]> { |
| 72 | + const result = this.engine.executeOnText( |
| 73 | + params[0], |
| 74 | + params[1]?.filePath, |
| 75 | + ) |
| 76 | + return result.results |
| 77 | + } |
| 78 | + |
| 79 | + // eslint-disable-next-line @typescript-eslint/require-await -- ignore |
| 80 | + public async lintFiles( |
| 81 | + ...params: Parameters<eslint.ESLint["lintFiles"]> |
| 82 | + ): ReturnType<eslint.ESLint["lintFiles"]> { |
| 83 | + const result = this.engine.executeOnFiles( |
| 84 | + Array.isArray(params[0]) ? params[0] : [params[0]], |
| 85 | + ) |
| 86 | + return result.results |
| 87 | + } |
| 88 | + |
| 89 | + // eslint-disable-next-line @typescript-eslint/require-await -- ignore |
| 90 | + public static async outputFixes( |
| 91 | + ...params: Parameters<typeof eslint.ESLint["outputFixes"]> |
| 92 | + ): ReturnType<typeof eslint.ESLint["outputFixes"]> { |
| 93 | + return CLIEngine.outputFixes({ |
| 94 | + results: params[0], |
| 95 | + } as any) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + const eslintClass = ESLintForV6 as never |
| 100 | + return eslintClass |
| 101 | +} |
0 commit comments