From 711f878f248785e0f95a97833a470b70d4da8a33 Mon Sep 17 00:00:00 2001 From: baseballyama Date: Sun, 12 Jan 2025 15:34:01 +0900 Subject: [PATCH 1/2] fix: update method for extracting major version --- .changeset/two-students-pump.md | 5 +++++ .../src/utils/svelte-context.ts | 16 ++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 .changeset/two-students-pump.md 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..a6d8fc3d2 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); if (major === '3' || major === '4') { return '3/4'; } @@ -185,7 +185,11 @@ function getSvelteKitVersion(filePath: string): SvelteContext['svelteKitVersion' if (typeof version !== 'string') { return null; } - return version.split('.')[0] as SvelteContext['svelteKitVersion']; + + if (version.includes('1.0.0-next')) { + return '1.0.0-next'; + } + return extractMajorVersion(version) as SvelteContext['svelteKitVersion']; } } catch { /** do nothing */ @@ -194,6 +198,14 @@ function getSvelteKitVersion(filePath: string): SvelteContext['svelteKitVersion' return null; } +function extractMajorVersion(version: string): string | null { + 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. From f3e8ef1b6b770968870739fb98697db095eb2fe7 Mon Sep 17 00:00:00 2001 From: baseballyama Date: Sun, 12 Jan 2025 15:42:17 +0900 Subject: [PATCH 2/2] refactor --- .../src/utils/svelte-context.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/eslint-plugin-svelte/src/utils/svelte-context.ts b/packages/eslint-plugin-svelte/src/utils/svelte-context.ts index a6d8fc3d2..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 = extractMajorVersion(version); + const major = extractMajorVersion(version, false); if (major === '3' || major === '4') { return '3/4'; } @@ -186,10 +186,7 @@ function getSvelteKitVersion(filePath: string): SvelteContext['svelteKitVersion' return null; } - if (version.includes('1.0.0-next')) { - return '1.0.0-next'; - } - return extractMajorVersion(version) as SvelteContext['svelteKitVersion']; + return extractMajorVersion(version, true) as SvelteContext['svelteKitVersion']; } } catch { /** do nothing */ @@ -198,7 +195,14 @@ function getSvelteKitVersion(filePath: string): SvelteContext['svelteKitVersion' return null; } -function extractMajorVersion(version: string): string | 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];