-
-
Notifications
You must be signed in to change notification settings - Fork 649
/
eslint-compat.js
56 lines (50 loc) · 1.5 KB
/
eslint-compat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
}