@@ -2,8 +2,10 @@ import type { RuleContext } from '../types.js';
2
2
import fs from 'fs' ;
3
3
import path from 'path' ;
4
4
import { getPackageJsons } from './get-package-json.js' ;
5
+ import { getNodeModule } from './get-node-module.js' ;
5
6
import { getFilename , getSourceCode } from './compat.js' ;
6
7
import { createCache } from './cache.js' ;
8
+ import { VERSION as SVELTE_VERSION } from 'svelte/compiler' ;
7
9
8
10
const isRunInBrowser = ! fs . readFileSync ;
9
11
@@ -168,51 +170,37 @@ function getSvelteKitContext(
168
170
return result ;
169
171
}
170
172
171
- const svelteVersionCache = createCache < SvelteContext [ 'svelteVersion' ] > ( ) ;
172
-
173
- export function getSvelteVersion ( filePath : string ) : SvelteContext [ 'svelteVersion' ] {
174
- const cached = svelteVersionCache . get ( filePath ) ;
175
- if ( cached ) return cached ;
173
+ function checkAndSetSvelteVersion ( version : string ) : SvelteContext [ 'svelteVersion' ] | null {
174
+ const major = extractMajorVersion ( version , false ) ;
175
+ if ( major == null ) {
176
+ return null ;
177
+ }
178
+ if ( major === '3' || major === '4' ) {
179
+ return '3/4' ;
180
+ }
181
+ return major as SvelteContext [ 'svelteVersion' ] ;
182
+ }
176
183
184
+ export function getSvelteVersion ( ) : SvelteContext [ 'svelteVersion' ] {
177
185
// Hack: if it runs in browser, it regards as Svelte project.
178
186
if ( isRunInBrowser ) {
179
- svelteVersionCache . set ( filePath , '5' ) ;
180
187
return '5' ;
181
188
}
182
189
183
- try {
184
- const packageJsons = getPackageJsons ( filePath ) ;
185
- for ( const packageJson of packageJsons ) {
186
- const version = packageJson . dependencies ?. svelte ?? packageJson . devDependencies ?. svelte ;
187
- if ( typeof version !== 'string' ) {
188
- continue ;
189
- }
190
- const major = extractMajorVersion ( version , false ) ;
191
- if ( major === '3' || major === '4' ) {
192
- svelteVersionCache . set ( filePath , '3/4' ) ;
193
- return '3/4' ;
194
- }
195
- svelteVersionCache . set ( filePath , major as SvelteContext [ 'svelteVersion' ] ) ;
196
- return major as SvelteContext [ 'svelteVersion' ] ;
197
- }
198
- } catch {
199
- /** do nothing */
200
- }
201
-
202
- svelteVersionCache . set ( filePath , null ) ;
203
- return null ;
190
+ return checkAndSetSvelteVersion ( SVELTE_VERSION ) ;
204
191
}
205
192
206
193
const svelteKitVersionCache = createCache < SvelteContext [ 'svelteKitVersion' ] > ( ) ;
207
194
208
- /**
209
- * Check givin file is under SvelteKit project.
210
- *
211
- * If it runs on browser, it always returns true.
212
- *
213
- * @param filePath A file path.
214
- * @returns
215
- */
195
+ function checkAndSetSvelteKitVersion (
196
+ version : string ,
197
+ filePath : string
198
+ ) : SvelteContext [ 'svelteKitVersion' ] {
199
+ const major = extractMajorVersion ( version , true ) as SvelteContext [ 'svelteKitVersion' ] ;
200
+ svelteKitVersionCache . set ( filePath , major ) ;
201
+ return major ;
202
+ }
203
+
216
204
function getSvelteKitVersion ( filePath : string ) : SvelteContext [ 'svelteKitVersion' ] {
217
205
const cached = svelteKitVersionCache . get ( filePath ) ;
218
206
if ( cached ) return cached ;
@@ -223,27 +211,42 @@ function getSvelteKitVersion(filePath: string): SvelteContext['svelteKitVersion'
223
211
return '2' ;
224
212
}
225
213
214
+ const nodeModule = getNodeModule ( '@sveltejs/kit' , filePath ) ;
215
+ if ( nodeModule ) {
216
+ try {
217
+ const packageJson = JSON . parse (
218
+ fs . readFileSync ( path . join ( nodeModule , 'package.json' ) , 'utf8' )
219
+ ) ;
220
+ const result = checkAndSetSvelteKitVersion ( packageJson . version , filePath ) ;
221
+ if ( result != null ) {
222
+ return result ;
223
+ }
224
+ } catch {
225
+ /** do nothing */
226
+ }
227
+ }
228
+
226
229
try {
227
230
const packageJsons = getPackageJsons ( filePath ) ;
228
- if ( packageJsons . length === 0 ) return null ;
229
- if ( packageJsons [ 0 ] . name === 'eslint-plugin-svelte' ) {
230
- // Hack: CI removes `@sveltejs/kit` and it returns false and test failed.
231
- // So always it returns 2 if it runs on the package.
232
- svelteKitVersionCache . set ( filePath , '2' ) ;
233
- return '2' ;
234
- }
231
+ if ( packageJsons . length > 0 ) {
232
+ if ( packageJsons [ 0 ] . name === 'eslint-plugin-svelte' ) {
233
+ // Hack: CI removes `@sveltejs/kit` and it returns false and test failed.
234
+ // So always it returns 2 if it runs on the package.
235
+ svelteKitVersionCache . set ( filePath , '2' ) ;
236
+ return '2' ;
237
+ }
235
238
236
- for ( const packageJson of packageJsons ) {
237
- const version =
238
- packageJson . dependencies ?. [ '@sveltejs/kit' ] ??
239
- packageJson . devDependencies ?. [ '@sveltejs/kit' ] ;
240
- if ( typeof version !== 'string' ) {
241
- svelteKitVersionCache . set ( filePath , null ) ;
242
- return null ;
239
+ for ( const packageJson of packageJsons ) {
240
+ const version =
241
+ packageJson . dependencies ?. [ '@sveltejs/kit' ] ??
242
+ packageJson . devDependencies ?. [ '@sveltejs/kit' ] ;
243
+ if ( typeof version === 'string' ) {
244
+ const result = checkAndSetSvelteKitVersion ( version , filePath ) ;
245
+ if ( result != null ) {
246
+ return result ;
247
+ }
248
+ }
243
249
}
244
- const major = extractMajorVersion ( version , true ) as SvelteContext [ 'svelteKitVersion' ] ;
245
- svelteKitVersionCache . set ( filePath , major ) ;
246
- return major ;
247
250
}
248
251
} catch {
249
252
/** do nothing */
@@ -291,7 +294,7 @@ export function getSvelteContext(context: RuleContext): SvelteContext | null {
291
294
if ( cached ) return cached ;
292
295
293
296
const svelteKitContext = getSvelteKitContext ( context ) ;
294
- const svelteVersion = getSvelteVersion ( filePath ) ;
297
+ const svelteVersion = getSvelteVersion ( ) ;
295
298
const svelteFileType = getSvelteFileType ( filePath ) ;
296
299
297
300
if ( svelteVersion == null ) {
0 commit comments