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'
66import { parseForESLint , AST as VAST } from 'vue-eslint-parser'
77import { readFileSync } from 'fs'
88import { resolve , extname } from 'path'
@@ -12,7 +12,10 @@ import { CacheLoader } from './cache-loader'
1212import { defineCacheFunction } from './cache-function'
1313import debugBuilder from 'debug'
1414import type { VisitorKeys } from '../types'
15+ // @ts -expect-error -- ignore
16+ import { Legacy } from '@eslint/eslintrc'
1517const debug = debugBuilder ( 'eslint-plugin-vue-i18n:collect-keys' )
18+ const { CascadingConfigArrayFactory } = Legacy
1619
1720/**
1821 *
@@ -100,11 +103,11 @@ function getParser(
100103function 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.
0 commit comments