forked from vuejs/eslint-plugin-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheslint-compat.js
70 lines (63 loc) · 1.88 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// @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, rules, ...overrideConfig } = {
plugins: [],
globals: {},
rules: {}
},
fix,
reportUnusedDisableDirectives,
plugins: pluginsMap,
...otherOptions
} = options || {}
/** @type {eslint.CLIEngine.Options} */
const newOptions = {
fix: Boolean(fix),
reportUnusedDisableDirectives: reportUnusedDisableDirectives
? reportUnusedDisableDirectives !== 'off'
: undefined,
...otherOptions,
globals: globals
? Object.keys(globals).filter((n) => globals[n])
: undefined,
plugins: plugins || [],
rules: rules
? Object.fromEntries(
Object.entries(rules).flatMap(([ruleId, opt]) =>
opt ? [[ruleId, opt]] : []
)
)
: undefined,
...overrideConfig
}
this.engine = new eslint.CLIEngine(newOptions)
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
}