@@ -4,13 +4,13 @@ import path from 'path';
44import { getPackageJson } from './get-package-json.js' ;
55import { getFilename , getSourceCode } from './compat.js' ;
66
7- const isRunOnBrowser = ! fs . readFileSync ;
7+ const isRunInBrowser = ! fs . readFileSync ;
88
99export type SvelteContext = {
10- svelteVersion : string ;
10+ svelteVersion : '3/4' | 5 ;
1111 fileType : '.svelte' | '.svelte.[js|ts]' ;
1212 runes : boolean ;
13- svelteKitVersion : string | null ;
13+ svelteKitVersion : '1-next' | 1 | 2 | null ;
1414 svelteKitFileType :
1515 | '+page.svelte'
1616 | '+page.js'
@@ -77,14 +77,14 @@ function getSvelteKitContext(
7777 context : RuleContext
7878) : Pick < SvelteContext , 'svelteKitFileType' | 'svelteKitVersion' > {
7979 const filePath = getFilename ( context ) ;
80- const svelteKitVersion = gteSvelteKitVersion ( filePath ) ;
80+ const svelteKitVersion = getSvelteKitVersion ( filePath ) ;
8181 if ( svelteKitVersion == null ) {
8282 return {
8383 svelteKitFileType : null ,
8484 svelteKitVersion : null
8585 } ;
8686 }
87- if ( isRunOnBrowser ) {
87+ if ( isRunInBrowser ) {
8888 return {
8989 svelteKitVersion,
9090 // Judge by only file path if it runs on browser.
@@ -120,32 +120,51 @@ function getSvelteKitContext(
120120 * @param filePath A file path.
121121 * @returns
122122 */
123- function gteSvelteKitVersion ( filePath : string ) : string | null {
123+ function getSvelteKitVersion ( filePath : string ) : SvelteContext [ 'svelteKitVersion' ] {
124124 // Hack: if it runs on browser, it regards as SvelteKit project.
125- if ( isRunOnBrowser ) return '2.15.1' ;
125+ if ( isRunInBrowser ) return 2 ;
126126 try {
127127 const packageJson = getPackageJson ( filePath ) ;
128128 if ( ! packageJson ) return null ;
129129 if ( packageJson . name === 'eslint-plugin-svelte' )
130130 // Hack: CI removes `@sveltejs/kit` and it returns false and test failed.
131131 // So always it returns true if it runs on the package.
132- return '2.15.1' ;
132+ return 2 ;
133133
134134 const version =
135135 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 ;
137148 } catch {
138149 return null ;
139150 }
140151}
141152
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+
142161/**
143162 * Gets a project root folder path.
144163 * @param filePath A file path to lookup.
145164 * @returns A found project root folder path or null.
146165 */
147166function getProjectRootDir ( filePath : string ) : string | null {
148- if ( isRunOnBrowser ) return null ;
167+ if ( isRunInBrowser ) return null ;
149168 const packageJsonFilePath = getPackageJson ( filePath ) ?. filePath ;
150169 if ( ! packageJsonFilePath ) return null ;
151170 return path . dirname ( path . resolve ( packageJsonFilePath ) ) ;
@@ -173,7 +192,7 @@ export function getSvelteContext(context: RuleContext): SvelteContext | null {
173192 }
174193
175194 return {
176- svelteVersion : compilerVersion ,
195+ svelteVersion : getSvelteVersion ( compilerVersion ) ,
177196 runes,
178197 fileType,
179198 svelteKitVersion : svelteKitContext . svelteKitVersion ,
0 commit comments