Skip to content

Commit 9383e13

Browse files
committed
refactor: extract resolveUpgradeTargetVersion
1 parent f5716f0 commit 9383e13

2 files changed

Lines changed: 42 additions & 34 deletions

File tree

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
import type { DependencyInfo } from '#types/extractor'
2-
import type { ParsedVersion } from '#utils/version'
3-
import type { DiagnosticRule, NodeDiagnosticInfo } from '..'
1+
import type { DiagnosticRule } from '..'
42
import { npmxPackageUrl } from '#utils/links'
3+
import { resolveUpgradeTargetVersion } from '#utils/upgrade'
54
import { formatUpgradeVersion } from '#utils/version'
6-
import gt from 'semver/functions/gt'
7-
import lte from 'semver/functions/lte'
8-
import prerelease from 'semver/functions/prerelease'
95
import { DiagnosticSeverity, Uri } from 'vscode'
106

11-
function createUpgradeDiagnostic(dep: DependencyInfo, parsed: ParsedVersion, target: string): NodeDiagnosticInfo {
7+
export const checkUpgrade: DiagnosticRule = ({ dep, pkg, parsed, exactVersion }) => {
8+
if (!parsed || !exactVersion)
9+
return
10+
11+
const target = resolveUpgradeTargetVersion(pkg, exactVersion)
12+
if (!target)
13+
return
14+
1215
return {
1316
node: dep.versionNode,
1417
severity: DiagnosticSeverity.Hint,
@@ -19,30 +22,3 @@ function createUpgradeDiagnostic(dep: DependencyInfo, parsed: ParsedVersion, tar
1922
},
2023
}
2124
}
22-
23-
export const checkUpgrade: DiagnosticRule = ({ dep, pkg, parsed, exactVersion }) => {
24-
if (!parsed || !exactVersion)
25-
return
26-
27-
if (Object.hasOwn(pkg.distTags, exactVersion))
28-
return
29-
30-
const { latest } = pkg.distTags
31-
if (gt(latest, exactVersion))
32-
return createUpgradeDiagnostic(dep, parsed, latest)
33-
34-
const currentPreId = prerelease(exactVersion)?.[0]
35-
if (currentPreId == null)
36-
return
37-
38-
for (const [tag, tagVersion] of Object.entries(pkg.distTags)) {
39-
if (tag === 'latest')
40-
continue
41-
if (prerelease(tagVersion)?.[0] !== currentPreId)
42-
continue
43-
if (lte(tagVersion, exactVersion))
44-
continue
45-
46-
return createUpgradeDiagnostic(dep, parsed, tagVersion)
47-
}
48-
}

src/utils/upgrade.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { PackageInfo } from '#utils/api/package'
2+
import gt from 'semver/functions/gt'
3+
import lte from 'semver/functions/lte'
4+
import prerelease from 'semver/functions/prerelease'
5+
6+
/**
7+
* Resolve the next upgrade target from npm dist-tags based on current exact version.
8+
* Mirrors the existing diagnostics upgrade rule behavior so other providers can reuse it.
9+
*/
10+
export function resolveUpgradeTargetVersion(pkg: PackageInfo, exactVersion: string): string | undefined {
11+
if (Object.hasOwn(pkg.distTags, exactVersion))
12+
return
13+
14+
const { latest } = pkg.distTags
15+
if (gt(latest, exactVersion))
16+
return latest
17+
18+
const currentPreId = prerelease(exactVersion)?.[0]
19+
if (currentPreId == null)
20+
return
21+
22+
for (const [tag, tagVersion] of Object.entries(pkg.distTags)) {
23+
if (tag === 'latest')
24+
continue
25+
if (prerelease(tagVersion)?.[0] !== currentPreId)
26+
continue
27+
if (lte(tagVersion, exactVersion))
28+
continue
29+
30+
return tagVersion
31+
}
32+
}

0 commit comments

Comments
 (0)