Skip to content

Commit

Permalink
fix: update method for extracting major version (#1007)
Browse files Browse the repository at this point in the history
  • Loading branch information
baseballyama authored Jan 12, 2025
1 parent d4c49e7 commit 8e9199a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-students-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-svelte': patch
---

fix: update method for extracting major version
20 changes: 18 additions & 2 deletions packages/eslint-plugin-svelte/src/utils/svelte-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down Expand Up @@ -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 */
Expand All @@ -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.
Expand Down

0 comments on commit 8e9199a

Please sign in to comment.