Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chores: Replace CLIEngine with ESLint class. (#1608)
- Loading branch information
Showing
5 changed files
with
220 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// @ts-check | ||
const eslint = require('eslint') | ||
|
||
module.exports = { | ||
ESLint: eslint.ESLint || getESLintClassForV6(), | ||
RuleTester: eslint.RuleTester | ||
} | ||
|
||
/** @returns {typeof eslint.ESLint} */ | ||
function getESLintClassForV6() { | ||
class ESLintForV6 { | ||
static get version() { | ||
return eslint.CLIEngine.version | ||
} | ||
|
||
/** @param {eslint.ESLint.Options} options */ | ||
constructor(options) { | ||
const { | ||
overrideConfig: { plugins, globals, ...overrideConfig }, | ||
fix, | ||
reportUnusedDisableDirectives, | ||
plugins: pluginsMap, | ||
...otherOptions | ||
} = options | ||
this.engine = new eslint.CLIEngine({ | ||
fix: Boolean(fix), | ||
globals: globals | ||
? Object.keys(globals).filter((n) => globals[n]) | ||
: undefined, | ||
...otherOptions, | ||
...overrideConfig, | ||
plugins: plugins || [], | ||
reportUnusedDisableDirectives: reportUnusedDisableDirectives | ||
? reportUnusedDisableDirectives !== 'off' | ||
: undefined | ||
}) | ||
|
||
for (const [name, plugin] of Object.entries(pluginsMap || {})) { | ||
this.engine.addPlugin(name, plugin) | ||
} | ||
} | ||
|
||
/** | ||
* @param {Parameters<eslint.ESLint['lintText']>} params | ||
* @returns {ReturnType<eslint.ESLint['lintText']>} | ||
*/ | ||
async lintText(...params) { | ||
const result = this.engine.executeOnText(params[0], params[1].filePath) | ||
return result.results | ||
} | ||
} | ||
|
||
/** @type {typeof eslint.ESLint} */ | ||
const eslintClass = /** @type {any} */ (ESLintForV6) | ||
return eslintClass | ||
} |
Oops, something went wrong.