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