diff --git a/.changeset/two-students-pump.md b/.changeset/two-students-pump.md new file mode 100644 index 000000000..c0e36c9d2 --- /dev/null +++ b/.changeset/two-students-pump.md @@ -0,0 +1,5 @@ +--- +'eslint-plugin-svelte': patch +--- + +fix: update method for extracting major version diff --git a/packages/eslint-plugin-svelte/src/utils/svelte-context.ts b/packages/eslint-plugin-svelte/src/utils/svelte-context.ts index e3334ff1e..42c556267 100644 --- a/packages/eslint-plugin-svelte/src/utils/svelte-context.ts +++ b/packages/eslint-plugin-svelte/src/utils/svelte-context.ts @@ -145,7 +145,7 @@ function getSvelteVersion(filePath: string): SvelteContext['svelteVersion'] { if (typeof version !== 'string') { continue; } - const major = version.split('.')[0]; + const major = extractMajorVersion(version, false); if (major === '3' || major === '4') { return '3/4'; } @@ -185,7 +185,8 @@ function getSvelteKitVersion(filePath: string): SvelteContext['svelteKitVersion' if (typeof version !== 'string') { return null; } - return version.split('.')[0] as SvelteContext['svelteKitVersion']; + + return extractMajorVersion(version, true) as SvelteContext['svelteKitVersion']; } } catch { /** do nothing */ @@ -194,6 +195,21 @@ function getSvelteKitVersion(filePath: string): SvelteContext['svelteKitVersion' return null; } +function extractMajorVersion(version: string, recognizePrereleaseVersion: boolean): string | null { + if (recognizePrereleaseVersion) { + const match = /^(?:\^|~)?(\d+\.0\.0-next)/.exec(version); + if (match && match[1]) { + return match[1]; + } + } + + const match = /^(?:\^|~)?(\d+)\./.exec(version); + if (match && match[1]) { + return match[1]; + } + return null; +} + /** * Gets a project root folder path. * @param filePath A file path to lookup.