@@ -5,6 +5,7 @@ import { getPackageJsons } from './get-package-json.js';
5
5
import { getNodeModule } from './get-node-module.js' ;
6
6
import { getFilename , getSourceCode } from './compat.js' ;
7
7
import { createCache } from './cache.js' ;
8
+ import { VERSION as SVELTE_VERSION } from 'svelte/compiler' ;
8
9
9
10
const isRunInBrowser = ! fs . readFileSync ;
10
11
@@ -169,68 +170,24 @@ function getSvelteKitContext(
169
170
return result ;
170
171
}
171
172
172
- const svelteVersionCache = createCache < SvelteContext [ 'svelteVersion' ] > ( ) ;
173
-
174
- function checkAndSetSvelteVersion (
175
- version : string ,
176
- filePath : string
177
- ) : SvelteContext [ 'svelteVersion' ] | null {
173
+ function checkAndSetSvelteVersion ( version : string ) : SvelteContext [ 'svelteVersion' ] | null {
178
174
const major = extractMajorVersion ( version , false ) ;
179
175
if ( major == null ) {
180
- svelteVersionCache . set ( filePath , null ) ;
181
176
return null ;
182
177
}
183
178
if ( major === '3' || major === '4' ) {
184
- svelteVersionCache . set ( filePath , '3/4' ) ;
185
179
return '3/4' ;
186
180
}
187
- svelteVersionCache . set ( filePath , major as SvelteContext [ 'svelteVersion' ] ) ;
188
181
return major as SvelteContext [ 'svelteVersion' ] ;
189
182
}
190
183
191
- export function getSvelteVersion ( filePath : string ) : SvelteContext [ 'svelteVersion' ] {
192
- const cached = svelteVersionCache . get ( filePath ) ;
193
- if ( cached ) return cached ;
194
-
184
+ export function getSvelteVersion ( ) : SvelteContext [ 'svelteVersion' ] {
195
185
// Hack: if it runs in browser, it regards as Svelte project.
196
186
if ( isRunInBrowser ) {
197
- svelteVersionCache . set ( filePath , '5' ) ;
198
187
return '5' ;
199
188
}
200
189
201
- const nodeModule = getNodeModule ( 'svelte' , filePath ) ;
202
- if ( nodeModule ) {
203
- try {
204
- const packageJson = JSON . parse (
205
- fs . readFileSync ( path . join ( nodeModule , 'package.json' ) , 'utf8' )
206
- ) ;
207
- const result = checkAndSetSvelteVersion ( packageJson . version , filePath ) ;
208
- if ( result != null ) {
209
- return result ;
210
- }
211
- } catch {
212
- /** do nothing */
213
- }
214
- }
215
-
216
- try {
217
- const packageJsons = getPackageJsons ( filePath ) ;
218
- for ( const packageJson of packageJsons ) {
219
- const version = packageJson . dependencies ?. svelte ?? packageJson . devDependencies ?. svelte ;
220
- if ( typeof version !== 'string' ) {
221
- continue ;
222
- }
223
- const result = checkAndSetSvelteVersion ( version , filePath ) ;
224
- if ( result != null ) {
225
- return result ;
226
- }
227
- }
228
- } catch {
229
- /** do nothing */
230
- }
231
-
232
- svelteVersionCache . set ( filePath , null ) ;
233
- return null ;
190
+ return checkAndSetSvelteVersion ( SVELTE_VERSION ) ;
234
191
}
235
192
236
193
const svelteKitVersionCache = createCache < SvelteContext [ 'svelteKitVersion' ] > ( ) ;
@@ -337,7 +294,7 @@ export function getSvelteContext(context: RuleContext): SvelteContext | null {
337
294
if ( cached ) return cached ;
338
295
339
296
const svelteKitContext = getSvelteKitContext ( context ) ;
340
- const svelteVersion = getSvelteVersion ( filePath ) ;
297
+ const svelteVersion = getSvelteVersion ( ) ;
341
298
const svelteFileType = getSvelteFileType ( filePath ) ;
342
299
343
300
if ( svelteVersion == null ) {
0 commit comments