|
| 1 | +// @ts-check |
| 2 | +const eslint = require('eslint') |
| 3 | + |
| 4 | +module.exports = { |
| 5 | + ESLint: eslint.ESLint || getESLintClassForV6(), |
| 6 | + RuleTester: eslint.RuleTester |
| 7 | +} |
| 8 | + |
| 9 | +/** @returns {typeof eslint.ESLint} */ |
| 10 | +function getESLintClassForV6() { |
| 11 | + class ESLintForV6 { |
| 12 | + static get version() { |
| 13 | + return eslint.CLIEngine.version |
| 14 | + } |
| 15 | + |
| 16 | + /** @param {eslint.ESLint.Options} options */ |
| 17 | + constructor(options) { |
| 18 | + const { |
| 19 | + overrideConfig: { plugins, globals, ...overrideConfig }, |
| 20 | + fix, |
| 21 | + reportUnusedDisableDirectives, |
| 22 | + plugins: pluginsMap, |
| 23 | + ...otherOptions |
| 24 | + } = options |
| 25 | + this.engine = new eslint.CLIEngine({ |
| 26 | + fix: Boolean(fix), |
| 27 | + globals: globals |
| 28 | + ? Object.keys(globals).filter((n) => globals[n]) |
| 29 | + : undefined, |
| 30 | + ...otherOptions, |
| 31 | + ...overrideConfig, |
| 32 | + plugins: plugins || [], |
| 33 | + reportUnusedDisableDirectives: reportUnusedDisableDirectives |
| 34 | + ? reportUnusedDisableDirectives !== 'off' |
| 35 | + : undefined |
| 36 | + }) |
| 37 | + |
| 38 | + for (const [name, plugin] of Object.entries(pluginsMap || {})) { |
| 39 | + this.engine.addPlugin(name, plugin) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @param {Parameters<eslint.ESLint['lintText']>} params |
| 45 | + * @returns {ReturnType<eslint.ESLint['lintText']>} |
| 46 | + */ |
| 47 | + async lintText(...params) { |
| 48 | + const result = this.engine.executeOnText(params[0], params[1].filePath) |
| 49 | + return result.results |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /** @type {typeof eslint.ESLint} */ |
| 54 | + const eslintClass = /** @type {any} */ (ESLintForV6) |
| 55 | + return eslintClass |
| 56 | +} |
0 commit comments