@@ -6,8 +6,11 @@ import { getFilename, getSourceCode } from './compat.js';
6
6
7
7
const isRunOnBrowser = ! fs . readFileSync ;
8
8
9
- type FileType = '.svelte' | '.svelte.[js|ts]' ;
10
9
export type SvelteContext = {
10
+ svelteVersion : string ;
11
+ fileType : '.svelte' | '.svelte.[js|ts]' ;
12
+ runes : boolean ;
13
+ svelteKitVersion : string | null ;
11
14
svelteKitFileType :
12
15
| '+page.svelte'
13
16
| '+page.js'
@@ -18,18 +21,9 @@ export type SvelteContext = {
18
21
| '+layout.server.js'
19
22
| '+server.js'
20
23
| null ;
21
- } & (
22
- | {
23
- version : 3 | 4 ;
24
- }
25
- | {
26
- version : 5 ;
27
- runes : boolean ;
28
- fileType : FileType ;
29
- }
30
- ) ;
31
-
32
- function getFileType ( filePath : string ) : FileType | null {
24
+ } ;
25
+
26
+ function getFileType ( filePath : string ) : SvelteContext [ 'fileType' ] | null {
33
27
if ( filePath . endsWith ( '.svelte' ) ) {
34
28
return '.svelte' ;
35
29
}
@@ -79,16 +73,23 @@ function getSvelteKitFileTypeFromFilePath(filePath: string): SvelteContext['svel
79
73
}
80
74
}
81
75
82
- function getSvelteKitFileType ( context : RuleContext ) : SvelteContext [ 'svelteKitFileType' ] {
76
+ function getSvelteKitContext (
77
+ context : RuleContext
78
+ ) : Pick < SvelteContext , 'svelteKitFileType' | 'svelteKitVersion' > {
83
79
const filePath = getFilename ( context ) ;
84
-
85
- // Hack: if it runs on browser, it regards as SvelteKit project.
86
- if ( isRunOnBrowser ) {
87
- return getSvelteKitFileTypeFromFilePath ( filePath ) ;
80
+ const svelteKitVersion = gteSvelteKitVersion ( filePath ) ;
81
+ if ( svelteKitVersion == null ) {
82
+ return {
83
+ svelteKitFileType : null ,
84
+ svelteKitVersion : null
85
+ } ;
88
86
}
89
-
90
- if ( ! hasSvelteKit ( getFilename ( context ) ) ) {
91
- return null ;
87
+ if ( isRunOnBrowser ) {
88
+ return {
89
+ svelteKitVersion,
90
+ // Judge by only file path if it runs on browser.
91
+ svelteKitFileType : getSvelteKitFileTypeFromFilePath ( filePath )
92
+ } ;
92
93
}
93
94
94
95
const routes =
@@ -99,10 +100,16 @@ function getSvelteKitFileType(context: RuleContext): SvelteContext['svelteKitFil
99
100
const projectRootDir = getProjectRootDir ( getFilename ( context ) ) ?? '' ;
100
101
101
102
if ( ! filePath . startsWith ( path . join ( projectRootDir , routes ) ) ) {
102
- return null ;
103
+ return {
104
+ svelteKitVersion,
105
+ svelteKitFileType : null
106
+ } ;
103
107
}
104
108
105
- return getSvelteKitFileTypeFromFilePath ( filePath ) ;
109
+ return {
110
+ svelteKitVersion,
111
+ svelteKitFileType : getSvelteKitFileTypeFromFilePath ( filePath )
112
+ } ;
106
113
}
107
114
108
115
/**
@@ -113,21 +120,22 @@ function getSvelteKitFileType(context: RuleContext): SvelteContext['svelteKitFil
113
120
* @param filePath A file path.
114
121
* @returns
115
122
*/
116
- function hasSvelteKit ( filePath : string ) : boolean {
123
+ function gteSvelteKitVersion ( filePath : string ) : string | null {
117
124
// Hack: if it runs on browser, it regards as SvelteKit project.
118
- if ( isRunOnBrowser ) return true ;
125
+ if ( isRunOnBrowser ) return '2.15.1' ;
119
126
try {
120
127
const packageJson = getPackageJson ( filePath ) ;
121
- if ( ! packageJson ) return false ;
128
+ if ( ! packageJson ) return null ;
122
129
if ( packageJson . name === 'eslint-plugin-svelte' )
123
130
// Hack: CI removes `@sveltejs/kit` and it returns false and test failed.
124
131
// So always it returns true if it runs on the package.
125
- return true ;
126
- return Boolean (
127
- packageJson . dependencies ?. [ '@sveltejs/kit' ] ?? packageJson . devDependencies ?. [ '@sveltejs/kit' ]
128
- ) ;
132
+ return '2.15.1' ;
133
+
134
+ const version =
135
+ packageJson . dependencies ?. [ '@sveltejs/kit' ] ?? packageJson . devDependencies ?. [ '@sveltejs/kit' ] ;
136
+ return typeof version === 'string' ? version : null ;
129
137
} catch {
130
- return false ;
138
+ return null ;
131
139
}
132
140
}
133
141
@@ -156,21 +164,7 @@ export function getSvelteContext(context: RuleContext): SvelteContext | null {
156
164
}
157
165
158
166
const filePath = getFilename ( context ) ;
159
- const svelteKitFileType = getSvelteKitFileType ( context ) ;
160
-
161
- if ( compilerVersion . startsWith ( '3' ) ) {
162
- return {
163
- version : 3 ,
164
- svelteKitFileType
165
- } ;
166
- }
167
-
168
- if ( compilerVersion . startsWith ( '4' ) ) {
169
- return {
170
- version : 4 ,
171
- svelteKitFileType
172
- } ;
173
- }
167
+ const svelteKitContext = getSvelteKitContext ( context ) ;
174
168
175
169
const runes = svelteParseContext . runes === true ;
176
170
const fileType = getFileType ( filePath ) ;
@@ -179,9 +173,10 @@ export function getSvelteContext(context: RuleContext): SvelteContext | null {
179
173
}
180
174
181
175
return {
182
- version : 5 ,
176
+ svelteVersion : compilerVersion ,
183
177
runes,
184
178
fileType,
185
- svelteKitFileType
179
+ svelteKitVersion : svelteKitContext . svelteKitVersion ,
180
+ svelteKitFileType : svelteKitContext . svelteKitFileType
186
181
} ;
187
182
}
0 commit comments