@@ -3,6 +3,7 @@ import fs from 'fs';
3
3
import path from 'path' ;
4
4
import { getPackageJsons } from './get-package-json.js' ;
5
5
import { getFilename , getSourceCode } from './compat.js' ;
6
+ import { createCache } from './cache.js' ;
6
7
7
8
const isRunInBrowser = ! fs . readFileSync ;
8
9
@@ -96,23 +97,51 @@ function getSvelteKitFileTypeFromFilePath(filePath: string): SvelteContext['svel
96
97
}
97
98
}
98
99
100
+ function extractMajorVersion ( version : string , recognizePrereleaseVersion : boolean ) : string | null {
101
+ if ( recognizePrereleaseVersion ) {
102
+ const match = / ^ (?: \^ | ~ ) ? ( \d + \. 0 \. 0 - n e x t ) / . exec ( version ) ;
103
+ if ( match && match [ 1 ] ) {
104
+ return match [ 1 ] ;
105
+ }
106
+ }
107
+
108
+ const match = / ^ (?: \^ | ~ ) ? ( \d + ) \. / . exec ( version ) ;
109
+ if ( match && match [ 1 ] ) {
110
+ return match [ 1 ] ;
111
+ }
112
+ return null ;
113
+ }
114
+
115
+ const svelteKitContextCache = createCache < Pick <
116
+ SvelteContext ,
117
+ 'svelteKitFileType' | 'svelteKitVersion'
118
+ > | null > ( ) ;
119
+
99
120
function getSvelteKitContext (
100
121
context : RuleContext
101
122
) : Pick < SvelteContext , 'svelteKitFileType' | 'svelteKitVersion' > {
102
123
const filePath = getFilename ( context ) ;
124
+
125
+ const cached = svelteKitContextCache . get ( filePath ) ;
126
+ if ( cached ) return cached ;
127
+
103
128
const svelteKitVersion = getSvelteKitVersion ( filePath ) ;
104
129
if ( svelteKitVersion == null ) {
105
- return {
130
+ const result : Pick < SvelteContext , 'svelteKitFileType' | 'svelteKitVersion' > = {
106
131
svelteKitFileType : null ,
107
132
svelteKitVersion : null
108
133
} ;
134
+ svelteKitContextCache . set ( filePath , result ) ;
135
+ return result ;
109
136
}
110
137
if ( isRunInBrowser ) {
111
- return {
138
+ const result : Pick < SvelteContext , 'svelteKitFileType' | 'svelteKitVersion' > = {
112
139
svelteKitVersion,
113
140
// Judge by only file path if it runs in browser.
114
141
svelteKitFileType : getSvelteKitFileTypeFromFilePath ( filePath )
115
142
} ;
143
+ svelteKitContextCache . set ( filePath , result ) ;
144
+ return result ;
116
145
}
117
146
118
147
const routes =
@@ -123,21 +152,34 @@ function getSvelteKitContext(
123
152
const projectRootDir = getProjectRootDir ( getFilename ( context ) ) ?? '' ;
124
153
125
154
if ( ! filePath . startsWith ( path . join ( projectRootDir , routes ) ) ) {
126
- return {
155
+ const result : Pick < SvelteContext , 'svelteKitFileType' | 'svelteKitVersion' > = {
127
156
svelteKitVersion,
128
157
svelteKitFileType : null
129
158
} ;
159
+ svelteKitContextCache . set ( filePath , result ) ;
160
+ return result ;
130
161
}
131
162
132
- return {
163
+ const result : Pick < SvelteContext , 'svelteKitFileType' | 'svelteKitVersion' > = {
133
164
svelteKitVersion,
134
165
svelteKitFileType : getSvelteKitFileTypeFromFilePath ( filePath )
135
166
} ;
167
+ svelteKitContextCache . set ( filePath , result ) ;
168
+ return result ;
136
169
}
137
170
138
- function getSvelteVersion ( filePath : string ) : SvelteContext [ 'svelteVersion' ] {
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 ;
176
+
139
177
// Hack: if it runs in browser, it regards as Svelte project.
140
- if ( isRunInBrowser ) return '5' ;
178
+ if ( isRunInBrowser ) {
179
+ svelteVersionCache . set ( filePath , '5' ) ;
180
+ return '5' ;
181
+ }
182
+
141
183
try {
142
184
const packageJsons = getPackageJsons ( filePath ) ;
143
185
for ( const packageJson of packageJsons ) {
@@ -147,17 +189,22 @@ function getSvelteVersion(filePath: string): SvelteContext['svelteVersion'] {
147
189
}
148
190
const major = extractMajorVersion ( version , false ) ;
149
191
if ( major === '3' || major === '4' ) {
192
+ svelteVersionCache . set ( filePath , '3/4' ) ;
150
193
return '3/4' ;
151
194
}
195
+ svelteVersionCache . set ( filePath , major as SvelteContext [ 'svelteVersion' ] ) ;
152
196
return major as SvelteContext [ 'svelteVersion' ] ;
153
197
}
154
198
} catch {
155
199
/** do nothing */
156
200
}
157
201
202
+ svelteVersionCache . set ( filePath , null ) ;
158
203
return null ;
159
204
}
160
205
206
+ const svelteKitVersionCache = createCache < SvelteContext [ 'svelteKitVersion' ] > ( ) ;
207
+
161
208
/**
162
209
* Check givin file is under SvelteKit project.
163
210
*
@@ -167,14 +214,22 @@ function getSvelteVersion(filePath: string): SvelteContext['svelteVersion'] {
167
214
* @returns
168
215
*/
169
216
function getSvelteKitVersion ( filePath : string ) : SvelteContext [ 'svelteKitVersion' ] {
217
+ const cached = svelteKitVersionCache . get ( filePath ) ;
218
+ if ( cached ) return cached ;
219
+
170
220
// Hack: if it runs in browser, it regards as SvelteKit project.
171
- if ( isRunInBrowser ) return '2' ;
221
+ if ( isRunInBrowser ) {
222
+ svelteKitVersionCache . set ( filePath , '2' ) ;
223
+ return '2' ;
224
+ }
225
+
172
226
try {
173
227
const packageJsons = getPackageJsons ( filePath ) ;
174
228
if ( packageJsons . length === 0 ) return null ;
175
229
if ( packageJsons [ 0 ] . name === 'eslint-plugin-svelte' ) {
176
230
// Hack: CI removes `@sveltejs/kit` and it returns false and test failed.
177
231
// So always it returns 2 if it runs on the package.
232
+ svelteKitVersionCache . set ( filePath , '2' ) ;
178
233
return '2' ;
179
234
}
180
235
@@ -183,32 +238,22 @@ function getSvelteKitVersion(filePath: string): SvelteContext['svelteKitVersion'
183
238
packageJson . dependencies ?. [ '@sveltejs/kit' ] ??
184
239
packageJson . devDependencies ?. [ '@sveltejs/kit' ] ;
185
240
if ( typeof version !== 'string' ) {
241
+ svelteKitVersionCache . set ( filePath , null ) ;
186
242
return null ;
187
243
}
188
-
189
- return extractMajorVersion ( version , true ) as SvelteContext [ 'svelteKitVersion' ] ;
244
+ const major = extractMajorVersion ( version , true ) as SvelteContext [ 'svelteKitVersion' ] ;
245
+ svelteKitVersionCache . set ( filePath , major ) ;
246
+ return major ;
190
247
}
191
248
} catch {
192
249
/** do nothing */
193
250
}
194
251
252
+ svelteKitVersionCache . set ( filePath , null ) ;
195
253
return null ;
196
254
}
197
255
198
- function extractMajorVersion ( version : string , recognizePrereleaseVersion : boolean ) : string | null {
199
- if ( recognizePrereleaseVersion ) {
200
- const match = / ^ (?: \^ | ~ ) ? ( \d + \. 0 \. 0 - n e x t ) / . exec ( version ) ;
201
- if ( match && match [ 1 ] ) {
202
- return match [ 1 ] ;
203
- }
204
- }
205
-
206
- const match = / ^ (?: \^ | ~ ) ? ( \d + ) \. / . exec ( version ) ;
207
- if ( match && match [ 1 ] ) {
208
- return match [ 1 ] ;
209
- }
210
- return null ;
211
- }
256
+ const projectRootDirCache = createCache < string | null > ( ) ;
212
257
213
258
/**
214
259
* Gets a project root folder path.
@@ -217,58 +262,81 @@ function extractMajorVersion(version: string, recognizePrereleaseVersion: boolea
217
262
*/
218
263
function getProjectRootDir ( filePath : string ) : string | null {
219
264
if ( isRunInBrowser ) return null ;
265
+ const cached = projectRootDirCache . get ( filePath ) ;
266
+ if ( cached ) return cached ;
267
+
220
268
const packageJsons = getPackageJsons ( filePath ) ;
221
269
if ( packageJsons . length === 0 ) {
270
+ projectRootDirCache . set ( filePath , null ) ;
222
271
return null ;
223
272
}
224
273
const packageJsonFilePath = packageJsons [ 0 ] . filePath ;
225
- if ( ! packageJsonFilePath ) return null ;
226
- return path . dirname ( path . resolve ( packageJsonFilePath ) ) ;
274
+ if ( ! packageJsonFilePath ) {
275
+ projectRootDirCache . set ( filePath , null ) ;
276
+ return null ;
277
+ }
278
+ const projectRootDir = path . dirname ( path . resolve ( packageJsonFilePath ) ) ;
279
+ projectRootDirCache . set ( filePath , projectRootDir ) ;
280
+ return projectRootDir ;
227
281
}
228
282
283
+ const svelteContextCache = createCache < SvelteContext | null > ( ) ;
284
+
229
285
export function getSvelteContext ( context : RuleContext ) : SvelteContext | null {
230
286
const { parserServices } = getSourceCode ( context ) ;
231
287
const { svelteParseContext } = parserServices ;
232
288
const filePath = getFilename ( context ) ;
289
+
290
+ const cached = svelteContextCache . get ( filePath ) ;
291
+ if ( cached ) return cached ;
292
+
233
293
const svelteKitContext = getSvelteKitContext ( context ) ;
234
294
const svelteVersion = getSvelteVersion ( filePath ) ;
235
295
const svelteFileType = getSvelteFileType ( filePath ) ;
236
296
237
297
if ( svelteVersion == null ) {
238
- return {
298
+ const result : SvelteContext = {
239
299
svelteVersion : null ,
240
300
svelteFileType : null ,
241
301
runes : null ,
242
302
svelteKitVersion : svelteKitContext . svelteKitVersion ,
243
303
svelteKitFileType : svelteKitContext . svelteKitFileType
244
304
} ;
305
+ svelteContextCache . set ( filePath , result ) ;
306
+ return result ;
245
307
}
246
308
247
309
if ( svelteVersion === '3/4' ) {
248
- return {
310
+ const result : SvelteContext = {
249
311
svelteVersion,
250
312
svelteFileType : svelteFileType === '.svelte' ? '.svelte' : null ,
251
313
runes : null ,
252
314
svelteKitVersion : svelteKitContext . svelteKitVersion ,
253
315
svelteKitFileType : svelteKitContext . svelteKitFileType
254
316
} ;
317
+ svelteContextCache . set ( filePath , result ) ;
318
+ return result ;
255
319
}
256
320
257
321
if ( svelteFileType == null ) {
258
- return {
322
+ const result : SvelteContext = {
259
323
svelteVersion,
260
324
svelteFileType : null ,
261
325
runes : null ,
262
326
svelteKitVersion : svelteKitContext . svelteKitVersion ,
263
327
svelteKitFileType : svelteKitContext . svelteKitFileType
264
328
} ;
329
+ svelteContextCache . set ( filePath , result ) ;
330
+ return result ;
265
331
}
266
332
267
- return {
333
+ const result : SvelteContext = {
268
334
svelteVersion,
269
335
runes : svelteParseContext ?. runes ?? 'undetermined' ,
270
336
svelteFileType,
271
337
svelteKitVersion : svelteKitContext . svelteKitVersion ,
272
338
svelteKitFileType : svelteKitContext . svelteKitFileType
273
339
} ;
340
+ svelteContextCache . set ( filePath , result ) ;
341
+ return result ;
274
342
}
0 commit comments