Skip to content

Commit be06c3b

Browse files
authored
Replace ESLint CLIEngine with @eslint/eslintrc (#206)
1 parent cb1c30c commit be06c3b

File tree

7 files changed

+81
-23
lines changed

7 files changed

+81
-23
lines changed

Diff for: docs/.vuepress/config.js

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ module.exports = {
1818
module: require.resolve('./shim/module'),
1919
glob: require.resolve('./shim/glob'),
2020
eslint: path.resolve(__dirname, './shim/eslint'),
21+
'@eslint/eslintrc$': path.resolve(
22+
__dirname,
23+
'./shim/@eslint/eslintrc'
24+
),
2125
fs: require.resolve('./shim/fs'),
2226
[path.resolve(
2327
__dirname,

Diff for: docs/.vuepress/shim/@eslint/eslintrc/index.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class CascadingConfigArrayFactory {
2+
getConfigArrayForFile() {
3+
return this
4+
}
5+
extractConfig() {
6+
return this
7+
}
8+
toCompatibleObjectAsConfigFileContent() {
9+
return {
10+
parser: 'vue-eslint-parser',
11+
parserOptions: {
12+
sourceType: 'module'
13+
}
14+
}
15+
}
16+
}
17+
export const Legacy = {
18+
CascadingConfigArrayFactory
19+
}

Diff for: docs/.vuepress/shim/eslint/index.js

-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,2 @@
11
import { SourceCode } from '../../../../node_modules/eslint/lib/source-code'
22
export { SourceCode }
3-
export class CLIEngine {
4-
addPlugin() {}
5-
getConfigForFile() {
6-
return {
7-
parser: 'vue-eslint-parser',
8-
parserOptions: {
9-
sourceType: 'module'
10-
}
11-
}
12-
}
13-
}

Diff for: files/empty.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

Diff for: lib/utils/collect-keys.ts

+27-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @fileoverview Collect localization keys
33
* @author kazuya kawaguchi (a.k.a. kazupon)
44
*/
5-
import { CLIEngine } from 'eslint'
5+
import type { Linter } from 'eslint'
66
import { parseForESLint, AST as VAST } from 'vue-eslint-parser'
77
import { readFileSync } from 'fs'
88
import { resolve, extname } from 'path'
@@ -12,7 +12,10 @@ import { CacheLoader } from './cache-loader'
1212
import { defineCacheFunction } from './cache-function'
1313
import debugBuilder from 'debug'
1414
import type { VisitorKeys } from '../types'
15+
// @ts-expect-error -- ignore
16+
import { Legacy } from '@eslint/eslintrc'
1517
const debug = debugBuilder('eslint-plugin-vue-i18n:collect-keys')
18+
const { CascadingConfigArrayFactory } = Legacy
1619

1720
/**
1821
*
@@ -100,11 +103,11 @@ function getParser(
100103
function collectKeysFromText(
101104
text: string,
102105
filename: string,
103-
cliEngine: CLIEngine
106+
getConfigForFile: (filePath: string) => Linter.Config<Linter.RulesRecord>
104107
) {
105108
const effectiveFilename = filename || '<text>'
106109
debug(`collectKeysFromFile ${effectiveFilename}`)
107-
const config = cliEngine.getConfigForFile(effectiveFilename)
110+
const config = getConfigForFile(effectiveFilename)
108111
const parser = getParser(config.parser)
109112

110113
const parserOptions = Object.assign({}, config.parserOptions, {
@@ -132,12 +135,15 @@ function collectKeysFromText(
132135
* Collect the used keys from files.
133136
* @returns {ResourceLoader[]}
134137
*/
135-
function collectKeyResourcesFromFiles(fileNames: string[]) {
138+
function collectKeyResourcesFromFiles(fileNames: string[], cwd: string) {
136139
debug('collectKeysFromFiles', fileNames)
137140

138-
const cliEngine = new CLIEngine({})
139-
// eslint-disable-next-line @typescript-eslint/no-var-requires
140-
cliEngine.addPlugin('@intlify/vue-i18n', require('../index')) // for Test
141+
const configArrayFactory = new CascadingConfigArrayFactory({
142+
additionalPluginPool: new Map([['@intlify/vue-i18n', require('../index')]]),
143+
cwd,
144+
eslintRecommendedPath: require.resolve('../../files/empty.json'),
145+
eslintAllPath: require.resolve('../../files/empty.json')
146+
})
141147

142148
const results = []
143149

@@ -148,12 +154,20 @@ function collectKeyResourcesFromFiles(fileNames: string[]) {
148154
results.push(
149155
new ResourceLoader(resolve(filename), () => {
150156
const text = readFileSync(resolve(filename), 'utf8')
151-
return collectKeysFromText(text, filename, cliEngine)
157+
return collectKeysFromText(text, filename, getConfigForFile)
152158
})
153159
)
154160
}
155161

156162
return results
163+
164+
function getConfigForFile(filePath: string) {
165+
const absolutePath = resolve(cwd, filePath)
166+
return configArrayFactory
167+
.getConfigArrayForFile(absolutePath)
168+
.extractConfig(absolutePath)
169+
.toCompatibleObjectAsConfigFileContent()
170+
}
157171
}
158172

159173
/**
@@ -246,9 +260,11 @@ class UsedKeysCache {
246260
.filter(f => !f.ignored && extensions.includes(extname(f.filename)))
247261
.map(f => f.filename)
248262
})
249-
this._collectKeyResourcesFromFiles = defineCacheFunction(fileNames => {
250-
return collectKeyResourcesFromFiles(fileNames)
251-
})
263+
this._collectKeyResourcesFromFiles = defineCacheFunction(
264+
(fileNames, cwd) => {
265+
return collectKeyResourcesFromFiles(fileNames, cwd)
266+
}
267+
)
252268
}
253269
/**
254270
* Collect the used keys from files.

Diff for: package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
}
2525
},
2626
"dependencies": {
27+
"@eslint/eslintrc": "^0.4.2",
2728
"@intlify/message-compiler": "^9.1.6",
2829
"@intlify/message-resolver": "^9.1.6",
2930
"debug": "^4.3.1",
@@ -75,7 +76,8 @@
7576
"node": ">=10.13.0 || >=12.0.0 || >= 14.0.0"
7677
},
7778
"files": [
78-
"dist"
79+
"dist",
80+
"files"
7981
],
8082
"homepage": "https://github.com/intlify/eslint-plugin-vue-i18n#readme",
8183
"keywords": [

Diff for: yarn.lock

+27
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,21 @@
977977
minimatch "^3.0.4"
978978
strip-json-comments "^3.1.1"
979979

980+
"@eslint/eslintrc@^0.4.2":
981+
version "0.4.2"
982+
resolved "https://registry.yarnpkg.com/@eslint/eslintrc/-/eslintrc-0.4.2.tgz#f63d0ef06f5c0c57d76c4ab5f63d3835c51b0179"
983+
integrity sha512-8nmGq/4ycLpIwzvhI4tNDmQztZ8sp+hI7cyG8i1nQDhkAbRzHpXPidRAHlNvCZQpJTKw5ItIpMw9RSToGF00mg==
984+
dependencies:
985+
ajv "^6.12.4"
986+
debug "^4.1.1"
987+
espree "^7.3.0"
988+
globals "^13.9.0"
989+
ignore "^4.0.6"
990+
import-fresh "^3.2.1"
991+
js-yaml "^3.13.1"
992+
minimatch "^3.0.4"
993+
strip-json-comments "^3.1.1"
994+
980995
"@intlify/message-compiler@^9.1.6":
981996
version "9.1.6"
982997
resolved "https://registry.yarnpkg.com/@intlify/message-compiler/-/message-compiler-9.1.6.tgz#e3e99165c1e6ecc496211017799ae59e15b05a18"
@@ -5186,6 +5201,13 @@ globals@^12.1.0:
51865201
dependencies:
51875202
type-fest "^0.8.1"
51885203

5204+
globals@^13.9.0:
5205+
version "13.9.0"
5206+
resolved "https://registry.yarnpkg.com/globals/-/globals-13.9.0.tgz#4bf2bf635b334a173fb1daf7c5e6b218ecdc06cb"
5207+
integrity sha512-74/FduwI/JaIrr1H8e71UbDE+5x7pIPs1C2rrwC52SszOo043CsWOZEMW7o2Y58xwm9b+0RBKDxY5n2sUpEFxA==
5208+
dependencies:
5209+
type-fest "^0.20.2"
5210+
51895211
globby@^10.0.1:
51905212
version "10.0.2"
51915213
resolved "https://registry.yarnpkg.com/globby/-/globby-10.0.2.tgz#277593e745acaa4646c3ab411289ec47a0392543"
@@ -10471,6 +10493,11 @@ type-fest@^0.11.0:
1047110493
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.11.0.tgz#97abf0872310fed88a5c466b25681576145e33f1"
1047210494
integrity sha512-OdjXJxnCN1AvyLSzeKIgXTXxV+99ZuXl3Hpo9XpJAv9MBcHrrJOQ5kV7ypXOuQie+AmWG25hLbiKdwYTifzcfQ==
1047310495

10496+
type-fest@^0.20.2:
10497+
version "0.20.2"
10498+
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.20.2.tgz#1bf207f4b28f91583666cb5fbd327887301cd5f4"
10499+
integrity sha512-Ne+eE4r0/iWnpAxD852z3A+N0Bt5RN//NjJwRd2VFHEmrywxf5vsZlh4R6lixl6B+wz/8d+maTSAkN1FIkI3LQ==
10500+
1047410501
type-fest@^0.8.0, type-fest@^0.8.1:
1047510502
version "0.8.1"
1047610503
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.8.1.tgz#09e249ebde851d3b1e48d27c105444667f17b83d"

0 commit comments

Comments
 (0)