Skip to content

Commit 56c6b93

Browse files
committed
fix: correctly handle version spec
1 parent 9c8b129 commit 56c6b93

2 files changed

Lines changed: 25 additions & 54 deletions

File tree

src/utils/version.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,17 @@ const PROTOCOL_PATTERN = /^[a-z]+:/
3131

3232
export function formatUpgradeVersion(dep: ResolvedDependencyInfo, target: string): string {
3333
const { rawName, rawSpec, resolvedName, resolvedSpec, protocol } = dep
34-
const prefix = getVersionRangePrefix(resolvedSpec)
3534

35+
const isAlias = resolvedName !== rawName
36+
const prefix = getVersionRangePrefix(resolvedSpec)
3637
const result = prefix === '*' ? '*' : `${prefix}${target}`
3738

39+
if (!isAlias)
40+
return result
41+
3842
const declaredProtocol = PROTOCOL_PATTERN.test(rawSpec) ? protocol : null
3943
if (!declaredProtocol)
4044
return result
4145

42-
const isAlias = resolvedName !== rawName
43-
const versionPart = isAlias ? formatPackageId(resolvedName, result) : result
44-
return `${declaredProtocol}:${versionPart}`
46+
return `${declaredProtocol}:${formatPackageId(resolvedName, result)}`
4547
}

tests/utils/version.test.ts

Lines changed: 19 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,25 @@
1+
import type { ResolvedDependencyInfo } from '#types/context'
12
import { describe, expect, it } from 'vitest'
23
import { formatUpgradeVersion } from '../../src/utils/version'
34

45
describe('formatUpgradeVersion', () => {
5-
it('should preserve ^ prefix', () => {
6-
expect(formatUpgradeVersion({ protocol: 'npm', resolvedName: 'foo', rawName: 'foo', resolvedSpec: '^1.0.0', rawSpec: '^1.0.0' }, '2.0.0')).toBe('^2.0.0')
7-
})
8-
9-
it('should preserve ~ prefix', () => {
10-
expect(formatUpgradeVersion({ protocol: 'npm', resolvedName: 'foo', rawName: 'foo', resolvedSpec: '~1.0.0', rawSpec: '~1.0.0' }, '1.1.0')).toBe('~1.1.0')
11-
})
12-
13-
it('should handle pinned version', () => {
14-
expect(formatUpgradeVersion({ protocol: 'npm', resolvedName: 'foo', rawName: 'foo', resolvedSpec: '1.0.0', rawSpec: '1.0.0' }, '2.0.0')).toBe('2.0.0')
15-
})
16-
17-
it('should preserve >= prefix', () => {
18-
expect(formatUpgradeVersion({ protocol: 'npm', resolvedName: 'foo', rawName: 'foo', resolvedSpec: '>=1.0.0', rawSpec: '>=1.0.0' }, '2.0.0')).toBe('>=2.0.0')
19-
})
20-
21-
it('should return * for wildcard', () => {
22-
expect(formatUpgradeVersion({ protocol: 'npm', resolvedName: 'foo', rawName: 'foo', resolvedSpec: '*', rawSpec: '*' }, '2.0.0')).toBe('*')
23-
})
24-
25-
it('should return * for empty semver', () => {
26-
expect(formatUpgradeVersion({ protocol: 'npm', resolvedName: 'foo', rawName: 'foo', resolvedSpec: '', rawSpec: '' }, '2.0.0')).toBe('*')
27-
})
28-
29-
it('should handle x-range major wildcard', () => {
30-
expect(formatUpgradeVersion({ protocol: 'npm', resolvedName: 'foo', rawName: 'foo', resolvedSpec: 'x', rawSpec: 'x' }, '2.0.0')).toBe('*')
31-
})
32-
33-
it('should handle x-range minor wildcard as ^', () => {
34-
expect(formatUpgradeVersion({ protocol: 'npm', resolvedName: 'foo', rawName: 'foo', resolvedSpec: '1.x', rawSpec: '1.x' }, '2.0.0')).toBe('^2.0.0')
35-
})
36-
37-
it('should handle x-range patch wildcard as ~', () => {
38-
expect(formatUpgradeVersion({ protocol: 'npm', resolvedName: 'foo', rawName: 'foo', resolvedSpec: '1.0.x', rawSpec: '1.0.x' }, '1.1.0')).toBe('~1.1.0')
39-
})
40-
41-
it('should include protocol in result', () => {
42-
expect(formatUpgradeVersion({ protocol: 'npm', resolvedName: 'foo', rawName: 'foo', resolvedSpec: '^1.0.0', rawSpec: 'npm:^1.0.0' }, '2.0.0')).toBe('npm:^2.0.0')
43-
})
44-
45-
it('should handle pinned version with protocol', () => {
46-
expect(formatUpgradeVersion({ protocol: 'npm', resolvedName: 'foo', rawName: 'foo', resolvedSpec: '1.0.0', rawSpec: 'npm:1.0.0' }, '2.0.0')).toBe('npm:2.0.0')
47-
})
48-
49-
it('should preserve protocol for wildcard', () => {
50-
expect(formatUpgradeVersion({ protocol: 'npm', resolvedName: 'foo', rawName: 'foo', resolvedSpec: '*', rawSpec: 'npm:*' }, '2.0.0')).toBe('npm:*')
51-
})
52-
53-
it('should preserve alias name in formatted version', () => {
54-
expect(formatUpgradeVersion({ protocol: 'npm', resolvedName: 'lodash', rawName: 'my-lodash', resolvedSpec: '~3.0.0', rawSpec: 'npm:lodash@~3.0.0' }, '4.0.0')).toBe('npm:lodash@~4.0.0')
6+
it.each([
7+
[['^1.0.0'], '2.0.0', '^2.0.0'],
8+
[['~1.0.0'], '1.1.0', '~1.1.0'],
9+
[['1.0.0'], '2.0.0', '2.0.0'],
10+
[['1.x'], '2.0.0', '^2.0.0'],
11+
[['1.0.x'], '1.1.0', '~1.1.0'],
12+
[['>=1.0.0'], '2.0.0', '>=2.0.0'],
13+
[['*'], '2.0.0', '*'],
14+
[[''], '2.0.0', '*'],
15+
[['x'], '2.0.0', '*'],
16+
[['^1.0.0', 'npm:foo@^1.0.0'], '2.0.0', '^2.0.0'],
17+
[['1.0.0', 'npm:foo@1.0.0'], '2.0.0', '2.0.0'],
18+
[['*', 'npm:foo@*'], '2.0.0', '*'],
19+
[['^1.0.0', 'npm:foo@^1.0.0', 'my-foo'], '2.0.0', 'npm:foo@^2.0.0'],
20+
])('should preserve $0', ([resolvedSpec, rawSpec = resolvedSpec, rawName = 'foo', protocol = 'npm'], target, expected) => {
21+
expect(
22+
formatUpgradeVersion({ protocol, rawName, rawSpec, resolvedName: 'foo', resolvedSpec } as ResolvedDependencyInfo, target),
23+
).toBe(expected)
5524
})
5625
})

0 commit comments

Comments
 (0)