@@ -2,6 +2,7 @@ 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' ;
7
8
@@ -170,6 +171,23 @@ function getSvelteKitContext(
170
171
171
172
const svelteVersionCache = createCache < SvelteContext [ 'svelteVersion' ] > ( ) ;
172
173
174
+ function checkAndSetSvelteVersion (
175
+ version : string ,
176
+ filePath : string
177
+ ) : SvelteContext [ 'svelteVersion' ] | null {
178
+ const major = extractMajorVersion ( version , false ) ;
179
+ if ( major == null ) {
180
+ svelteVersionCache . set ( filePath , null ) ;
181
+ return null ;
182
+ }
183
+ if ( major === '3' || major === '4' ) {
184
+ svelteVersionCache . set ( filePath , '3/4' ) ;
185
+ return '3/4' ;
186
+ }
187
+ svelteVersionCache . set ( filePath , major as SvelteContext [ 'svelteVersion' ] ) ;
188
+ return major as SvelteContext [ 'svelteVersion' ] ;
189
+ }
190
+
173
191
export function getSvelteVersion ( filePath : string ) : SvelteContext [ 'svelteVersion' ] {
174
192
const cached = svelteVersionCache . get ( filePath ) ;
175
193
if ( cached ) return cached ;
@@ -187,32 +205,45 @@ export function getSvelteVersion(filePath: string): SvelteContext['svelteVersion
187
205
if ( typeof version !== 'string' ) {
188
206
continue ;
189
207
}
190
- const major = extractMajorVersion ( version , false ) ;
191
- if ( major === '3' || major === '4' ) {
192
- svelteVersionCache . set ( filePath , '3/4' ) ;
193
- return '3/4' ;
208
+ const result = checkAndSetSvelteVersion ( version , filePath ) ;
209
+ if ( result != null ) {
210
+ return result ;
194
211
}
195
- svelteVersionCache . set ( filePath , major as SvelteContext [ 'svelteVersion' ] ) ;
196
- return major as SvelteContext [ 'svelteVersion' ] ;
197
212
}
198
213
} catch {
199
214
/** do nothing */
200
215
}
201
216
217
+ const nodeModule = getNodeModule ( 'svelte' , filePath ) ;
218
+ if ( nodeModule ) {
219
+ try {
220
+ const packageJson = JSON . parse (
221
+ fs . readFileSync ( path . join ( nodeModule , 'package.json' ) , 'utf8' )
222
+ ) ;
223
+ const result = checkAndSetSvelteVersion ( packageJson . version , filePath ) ;
224
+ if ( result != null ) {
225
+ return result ;
226
+ }
227
+ } catch {
228
+ /** do nothing */
229
+ }
230
+ }
231
+
202
232
svelteVersionCache . set ( filePath , null ) ;
203
233
return null ;
204
234
}
205
235
206
236
const svelteKitVersionCache = createCache < SvelteContext [ 'svelteKitVersion' ] > ( ) ;
207
237
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
- */
238
+ function checkAndSetSvelteKitVersion (
239
+ version : string ,
240
+ filePath : string
241
+ ) : SvelteContext [ 'svelteKitVersion' ] {
242
+ const major = extractMajorVersion ( version , true ) as SvelteContext [ 'svelteKitVersion' ] ;
243
+ svelteKitVersionCache . set ( filePath , major ) ;
244
+ return major ;
245
+ }
246
+
216
247
function getSvelteKitVersion ( filePath : string ) : SvelteContext [ 'svelteKitVersion' ] {
217
248
const cached = svelteKitVersionCache . get ( filePath ) ;
218
249
if ( cached ) return cached ;
@@ -225,30 +256,45 @@ function getSvelteKitVersion(filePath: string): SvelteContext['svelteKitVersion'
225
256
226
257
try {
227
258
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
- }
259
+ if ( packageJsons . length > 0 ) {
260
+ if ( packageJsons [ 0 ] . name === 'eslint-plugin-svelte' ) {
261
+ // Hack: CI removes `@sveltejs/kit` and it returns false and test failed.
262
+ // So always it returns 2 if it runs on the package.
263
+ svelteKitVersionCache . set ( filePath , '2' ) ;
264
+ return '2' ;
265
+ }
235
266
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 ;
267
+ for ( const packageJson of packageJsons ) {
268
+ const version =
269
+ packageJson . dependencies ?. [ '@sveltejs/kit' ] ??
270
+ packageJson . devDependencies ?. [ '@sveltejs/kit' ] ;
271
+ if ( typeof version === 'string' ) {
272
+ const result = checkAndSetSvelteKitVersion ( version , filePath ) ;
273
+ if ( result != null ) {
274
+ return result ;
275
+ }
276
+ }
243
277
}
244
- const major = extractMajorVersion ( version , true ) as SvelteContext [ 'svelteKitVersion' ] ;
245
- svelteKitVersionCache . set ( filePath , major ) ;
246
- return major ;
247
278
}
248
279
} catch {
249
280
/** do nothing */
250
281
}
251
282
283
+ const nodeModule = getNodeModule ( '@sveltejs/kit' , filePath ) ;
284
+ if ( nodeModule ) {
285
+ try {
286
+ const packageJson = JSON . parse (
287
+ fs . readFileSync ( path . join ( nodeModule , 'package.json' ) , 'utf8' )
288
+ ) ;
289
+ const result = checkAndSetSvelteKitVersion ( packageJson . version , filePath ) ;
290
+ if ( result != null ) {
291
+ return result ;
292
+ }
293
+ } catch {
294
+ /** do nothing */
295
+ }
296
+ }
297
+
252
298
svelteKitVersionCache . set ( filePath , null ) ;
253
299
return null ;
254
300
}
0 commit comments