Skip to content

Commit

Permalink
Chores: Replace CLIEngine with ESLint class. (#1608)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Aug 14, 2021
1 parent 1fc3485 commit 730645e
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 138 deletions.
56 changes: 56 additions & 0 deletions tests/eslint-compat.js
@@ -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
}

0 comments on commit 730645e

Please sign in to comment.