2
2
* @fileoverview Collect localization keys
3
3
* @author kazuya kawaguchi (a.k.a. kazupon)
4
4
*/
5
- import { CLIEngine } from 'eslint'
5
+ import type { Linter } from 'eslint'
6
6
import { parseForESLint , AST as VAST } from 'vue-eslint-parser'
7
7
import { readFileSync } from 'fs'
8
8
import { resolve , extname } from 'path'
@@ -12,7 +12,10 @@ import { CacheLoader } from './cache-loader'
12
12
import { defineCacheFunction } from './cache-function'
13
13
import debugBuilder from 'debug'
14
14
import type { VisitorKeys } from '../types'
15
+ // @ts -expect-error -- ignore
16
+ import { Legacy } from '@eslint/eslintrc'
15
17
const debug = debugBuilder ( 'eslint-plugin-vue-i18n:collect-keys' )
18
+ const { CascadingConfigArrayFactory } = Legacy
16
19
17
20
/**
18
21
*
@@ -100,11 +103,11 @@ function getParser(
100
103
function collectKeysFromText (
101
104
text : string ,
102
105
filename : string ,
103
- cliEngine : CLIEngine
106
+ getConfigForFile : ( filePath : string ) => Linter . Config < Linter . RulesRecord >
104
107
) {
105
108
const effectiveFilename = filename || '<text>'
106
109
debug ( `collectKeysFromFile ${ effectiveFilename } ` )
107
- const config = cliEngine . getConfigForFile ( effectiveFilename )
110
+ const config = getConfigForFile ( effectiveFilename )
108
111
const parser = getParser ( config . parser )
109
112
110
113
const parserOptions = Object . assign ( { } , config . parserOptions , {
@@ -132,12 +135,15 @@ function collectKeysFromText(
132
135
* Collect the used keys from files.
133
136
* @returns {ResourceLoader[] }
134
137
*/
135
- function collectKeyResourcesFromFiles ( fileNames : string [ ] ) {
138
+ function collectKeyResourcesFromFiles ( fileNames : string [ ] , cwd : string ) {
136
139
debug ( 'collectKeysFromFiles' , fileNames )
137
140
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
+ } )
141
147
142
148
const results = [ ]
143
149
@@ -148,12 +154,20 @@ function collectKeyResourcesFromFiles(fileNames: string[]) {
148
154
results . push (
149
155
new ResourceLoader ( resolve ( filename ) , ( ) => {
150
156
const text = readFileSync ( resolve ( filename ) , 'utf8' )
151
- return collectKeysFromText ( text , filename , cliEngine )
157
+ return collectKeysFromText ( text , filename , getConfigForFile )
152
158
} )
153
159
)
154
160
}
155
161
156
162
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
+ }
157
171
}
158
172
159
173
/**
@@ -246,9 +260,11 @@ class UsedKeysCache {
246
260
. filter ( f => ! f . ignored && extensions . includes ( extname ( f . filename ) ) )
247
261
. map ( f => f . filename )
248
262
} )
249
- this . _collectKeyResourcesFromFiles = defineCacheFunction ( fileNames => {
250
- return collectKeyResourcesFromFiles ( fileNames )
251
- } )
263
+ this . _collectKeyResourcesFromFiles = defineCacheFunction (
264
+ ( fileNames , cwd ) => {
265
+ return collectKeyResourcesFromFiles ( fileNames , cwd )
266
+ }
267
+ )
252
268
}
253
269
/**
254
270
* Collect the used keys from files.
0 commit comments