@@ -4,13 +4,13 @@ import path from 'path';
4
4
import { getPackageJson } from './get-package-json.js' ;
5
5
import { getFilename , getSourceCode } from './compat.js' ;
6
6
7
- const isRunOnBrowser = ! fs . readFileSync ;
7
+ const isRunInBrowser = ! fs . readFileSync ;
8
8
9
9
export type SvelteContext = {
10
- svelteVersion : string ;
10
+ svelteVersion : '3/4' | 5 ;
11
11
fileType : '.svelte' | '.svelte.[js|ts]' ;
12
12
runes : boolean ;
13
- svelteKitVersion : string | null ;
13
+ svelteKitVersion : '1-next' | 1 | 2 | null ;
14
14
svelteKitFileType :
15
15
| '+page.svelte'
16
16
| '+page.js'
@@ -77,14 +77,14 @@ function getSvelteKitContext(
77
77
context : RuleContext
78
78
) : Pick < SvelteContext , 'svelteKitFileType' | 'svelteKitVersion' > {
79
79
const filePath = getFilename ( context ) ;
80
- const svelteKitVersion = gteSvelteKitVersion ( filePath ) ;
80
+ const svelteKitVersion = getSvelteKitVersion ( filePath ) ;
81
81
if ( svelteKitVersion == null ) {
82
82
return {
83
83
svelteKitFileType : null ,
84
84
svelteKitVersion : null
85
85
} ;
86
86
}
87
- if ( isRunOnBrowser ) {
87
+ if ( isRunInBrowser ) {
88
88
return {
89
89
svelteKitVersion,
90
90
// Judge by only file path if it runs on browser.
@@ -120,32 +120,51 @@ function getSvelteKitContext(
120
120
* @param filePath A file path.
121
121
* @returns
122
122
*/
123
- function gteSvelteKitVersion ( filePath : string ) : string | null {
123
+ function getSvelteKitVersion ( filePath : string ) : SvelteContext [ 'svelteKitVersion' ] {
124
124
// Hack: if it runs on browser, it regards as SvelteKit project.
125
- if ( isRunOnBrowser ) return '2.15.1' ;
125
+ if ( isRunInBrowser ) return 2 ;
126
126
try {
127
127
const packageJson = getPackageJson ( filePath ) ;
128
128
if ( ! packageJson ) return null ;
129
129
if ( packageJson . name === 'eslint-plugin-svelte' )
130
130
// Hack: CI removes `@sveltejs/kit` and it returns false and test failed.
131
131
// So always it returns true if it runs on the package.
132
- return '2.15.1' ;
132
+ return 2 ;
133
133
134
134
const version =
135
135
packageJson . dependencies ?. [ '@sveltejs/kit' ] ?? packageJson . devDependencies ?. [ '@sveltejs/kit' ] ;
136
- return typeof version === 'string' ? version : null ;
136
+ if ( typeof version !== 'string' ) {
137
+ return null ;
138
+ }
139
+ if ( version . startsWith ( '1.0.0-next.' ) ) {
140
+ return '1-next' ;
141
+ } else if ( version . startsWith ( '1.' ) ) {
142
+ return 1 ;
143
+ } else if ( version . startsWith ( '2.' ) ) {
144
+ return 2 ;
145
+ }
146
+ // If unknown version, it recognize as v2.
147
+ return 2 ;
137
148
} catch {
138
149
return null ;
139
150
}
140
151
}
141
152
153
+ function getSvelteVersion ( compilerVersion : string ) : SvelteContext [ 'svelteVersion' ] {
154
+ const version = parseInt ( compilerVersion . split ( '.' ) [ 0 ] , 10 ) ;
155
+ if ( version === 3 || version === 4 ) {
156
+ return '3/4' ;
157
+ }
158
+ return 5 ;
159
+ }
160
+
142
161
/**
143
162
* Gets a project root folder path.
144
163
* @param filePath A file path to lookup.
145
164
* @returns A found project root folder path or null.
146
165
*/
147
166
function getProjectRootDir ( filePath : string ) : string | null {
148
- if ( isRunOnBrowser ) return null ;
167
+ if ( isRunInBrowser ) return null ;
149
168
const packageJsonFilePath = getPackageJson ( filePath ) ?. filePath ;
150
169
if ( ! packageJsonFilePath ) return null ;
151
170
return path . dirname ( path . resolve ( packageJsonFilePath ) ) ;
@@ -173,7 +192,7 @@ export function getSvelteContext(context: RuleContext): SvelteContext | null {
173
192
}
174
193
175
194
return {
176
- svelteVersion : compilerVersion ,
195
+ svelteVersion : getSvelteVersion ( compilerVersion ) ,
177
196
runes,
178
197
fileType,
179
198
svelteKitVersion : svelteKitContext . svelteKitVersion ,
0 commit comments