2
2
* @fileoverview Collect localization keys
3
3
* @author kazuya kawaguchi (a.k.a. kazupon)
4
4
*/
5
- import type { Linter } from 'eslint'
6
- import { parseForESLint , AST as VAST } from 'vue-eslint-parser'
7
- import { readFileSync } from 'fs'
5
+ import { AST as VAST } from 'vue-eslint-parser'
8
6
import { resolve , extname } from 'path'
9
7
import { listFilesToProcess } from './glob-utils'
10
8
import { ResourceLoader } from './resource-loader'
11
9
import { CacheLoader } from './cache-loader'
12
10
import { defineCacheFunction } from './cache-function'
13
11
import debugBuilder from 'debug'
14
12
import type { RuleContext , VisitorKeys } from '../types'
15
- // @ts -expect-error -- ignore
16
- import { Legacy } from '@eslint/eslintrc'
17
13
import { getCwd } from './get-cwd'
18
14
import { isStaticLiteral , getStaticLiteralValue } from './index'
19
- import importFresh from 'import-fresh'
15
+ import type { Parser } from './parser-config-resolver'
16
+ import { buildParserFromConfig } from './parser-config-resolver'
20
17
const debug = debugBuilder ( 'eslint-plugin-vue-i18n:collect-keys' )
21
- const { CascadingConfigArrayFactory } = Legacy
22
18
23
19
/**
24
20
*
@@ -74,56 +70,20 @@ function getKeyFromI18nComponent(node: VAST.VAttribute) {
74
70
}
75
71
}
76
72
77
- function getParser ( parser : string | undefined ) : {
78
- parseForESLint ?: typeof parseForESLint
79
- parse : ( code : string , options : unknown ) => VAST . ESLintProgram
80
- } {
81
- if ( parser ) {
82
- try {
83
- return require ( parser )
84
- } catch ( _e ) {
85
- // ignore
86
- }
87
- }
88
- return {
89
- parseForESLint,
90
- parse ( code : string , options : unknown ) {
91
- return parseForESLint ( code , options ) . ast
92
- }
93
- }
94
- }
95
-
96
73
/**
97
74
* Collect the used keys from source code text.
98
75
* @param {string } text
99
76
* @param {string } filename
100
77
* @returns {string[] }
101
78
*/
102
- function collectKeysFromText (
103
- text : string ,
104
- filename : string ,
105
- getConfigForFile : ( filePath : string ) => Linter . Config < Linter . RulesRecord >
106
- ) {
79
+ function collectKeysFromText ( filename : string , parser : Parser ) {
107
80
const effectiveFilename = filename || '<text>'
108
81
debug ( `collectKeysFromFile ${ effectiveFilename } ` )
109
- const config = getConfigForFile ( effectiveFilename )
110
- const parser = getParser ( config . parser )
111
-
112
- const parserOptions = Object . assign ( { } , config . parserOptions , {
113
- loc : true ,
114
- range : true ,
115
- raw : true ,
116
- tokens : true ,
117
- comment : true ,
118
- eslintVisitorKeys : true ,
119
- eslintScopeManager : true ,
120
- filePath : effectiveFilename
121
- } )
122
82
try {
123
- const parseResult =
124
- typeof parser . parseForESLint === 'function'
125
- ? parser . parseForESLint ( text , parserOptions )
126
- : { ast : parser . parse ( text , parserOptions ) }
83
+ const parseResult = parser ( filename )
84
+ if ( ! parseResult ) {
85
+ return [ ]
86
+ }
127
87
return collectKeysFromAST ( parseResult . ast , parseResult . visitorKeys )
128
88
} catch ( _e ) {
129
89
return [ ]
@@ -137,20 +97,7 @@ function collectKeysFromText(
137
97
function collectKeyResourcesFromFiles ( fileNames : string [ ] , cwd : string ) {
138
98
debug ( 'collectKeysFromFiles' , fileNames )
139
99
140
- const configArrayFactory = new CascadingConfigArrayFactory ( {
141
- additionalPluginPool : new Map ( [
142
- [ '@intlify/vue-i18n' , importFresh ( '../index' ) ]
143
- ] ) ,
144
- cwd,
145
- async getEslintRecommendedConfig ( ) {
146
- return await import ( '../../files/empty.json' )
147
- } ,
148
- async getEslintAllConfig ( ) {
149
- return await import ( '../../files/empty.json' )
150
- } ,
151
- eslintRecommendedPath : require . resolve ( '../../files/empty.json' ) ,
152
- eslintAllPath : require . resolve ( '../../files/empty.json' )
153
- } )
100
+ const parser = buildParserFromConfig ( cwd )
154
101
155
102
const results = [ ]
156
103
@@ -160,21 +107,12 @@ function collectKeyResourcesFromFiles(fileNames: string[], cwd: string) {
160
107
161
108
results . push (
162
109
new ResourceLoader ( resolve ( filename ) , ( ) => {
163
- const text = readFileSync ( resolve ( filename ) , 'utf8' )
164
- return collectKeysFromText ( text , filename , getConfigForFile )
110
+ return collectKeysFromText ( filename , parser )
165
111
} )
166
112
)
167
113
}
168
114
169
115
return results
170
-
171
- function getConfigForFile ( filePath : string ) {
172
- const absolutePath = resolve ( cwd , filePath )
173
- return configArrayFactory
174
- . getConfigArrayForFile ( absolutePath )
175
- . extractConfig ( absolutePath )
176
- . toCompatibleObjectAsConfigFileContent ( )
177
- }
178
116
}
179
117
180
118
/**
0 commit comments