Skip to content

Commit 8e9199a

Browse files
authored
fix: update method for extracting major version (#1007)
1 parent d4c49e7 commit 8e9199a

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

.changeset/two-students-pump.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'eslint-plugin-svelte': patch
3+
---
4+
5+
fix: update method for extracting major version

packages/eslint-plugin-svelte/src/utils/svelte-context.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ function getSvelteVersion(filePath: string): SvelteContext['svelteVersion'] {
145145
if (typeof version !== 'string') {
146146
continue;
147147
}
148-
const major = version.split('.')[0];
148+
const major = extractMajorVersion(version, false);
149149
if (major === '3' || major === '4') {
150150
return '3/4';
151151
}
@@ -185,7 +185,8 @@ function getSvelteKitVersion(filePath: string): SvelteContext['svelteKitVersion'
185185
if (typeof version !== 'string') {
186186
return null;
187187
}
188-
return version.split('.')[0] as SvelteContext['svelteKitVersion'];
188+
189+
return extractMajorVersion(version, true) as SvelteContext['svelteKitVersion'];
189190
}
190191
} catch {
191192
/** do nothing */
@@ -194,6 +195,21 @@ function getSvelteKitVersion(filePath: string): SvelteContext['svelteKitVersion'
194195
return null;
195196
}
196197

198+
function extractMajorVersion(version: string, recognizePrereleaseVersion: boolean): string | null {
199+
if (recognizePrereleaseVersion) {
200+
const match = /^(?:\^|~)?(\d+\.0\.0-next)/.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+
}
212+
197213
/**
198214
* Gets a project root folder path.
199215
* @param filePath A file path to lookup.

0 commit comments

Comments
 (0)