|
22 | 22 | */ |
23 | 23 |
|
24 | 24 | import npmPackageArg from 'npm-package-arg' |
| 25 | +import type { AliasResult } from 'npm-package-arg' |
25 | 26 |
|
26 | 27 | import { NPM } from '../constants.mts' |
27 | 28 | import { createPurlObject } from './purl.mts' |
@@ -98,38 +99,37 @@ export function safeParseNpmSpec( |
98 | 99 | } |
99 | 100 | } |
100 | 101 |
|
101 | | - // Extract name and version from parsed spec. |
102 | | - const name = parsed.name || pkgSpec |
| 102 | + // npm aliases carry the target package in subSpec. Use it for PURLs and |
| 103 | + // version extraction so the alias label does not become the package name. |
| 104 | + const spec = |
| 105 | + parsed.type === 'alias' ? (parsed as AliasResult).subSpec : parsed |
| 106 | + const name = spec.name || pkgSpec |
103 | 107 | let version: string | undefined |
104 | 108 |
|
105 | 109 | // Handle different spec types from npm-package-arg. |
106 | | - if ( |
107 | | - parsed.type === 'tag' || |
108 | | - parsed.type === 'version' || |
109 | | - parsed.type === 'range' |
110 | | - ) { |
| 110 | + if (spec.type === 'tag' || spec.type === 'version' || spec.type === 'range') { |
111 | 111 | // For npm registry packages: |
112 | 112 | // - type 'tag': latest, beta, etc. |
113 | 113 | // - type 'version': exact version like 1.0.0 |
114 | 114 | // - type 'range': version range like ^1.0.0, ~1.0.0, or * for bare names |
115 | 115 | // Don't include '*' as a version - it means "any version". |
116 | | - if (parsed.fetchSpec && parsed.fetchSpec !== '*') { |
117 | | - version = parsed.fetchSpec |
| 116 | + if (spec.fetchSpec && spec.fetchSpec !== '*') { |
| 117 | + version = spec.fetchSpec |
118 | 118 | } else if ( |
119 | | - parsed.rawSpec && |
120 | | - parsed.rawSpec !== '*' && |
121 | | - parsed.rawSpec !== parsed.name |
| 119 | + spec.rawSpec && |
| 120 | + spec.rawSpec !== '*' && |
| 121 | + spec.rawSpec !== spec.name |
122 | 122 | ) { |
123 | | - version = parsed.rawSpec |
| 123 | + version = spec.rawSpec |
124 | 124 | } |
125 | 125 | } else if ( |
126 | | - parsed.type === 'git' || |
127 | | - parsed.type === 'remote' || |
128 | | - parsed.type === 'file' |
| 126 | + spec.type === 'git' || |
| 127 | + spec.type === 'remote' || |
| 128 | + spec.type === 'file' |
129 | 129 | ) { |
130 | 130 | // For non-registry specs, use rawSpec if different from name. |
131 | | - if (parsed.rawSpec && parsed.rawSpec !== parsed.name) { |
132 | | - version = parsed.rawSpec |
| 131 | + if (spec.rawSpec && spec.rawSpec !== spec.name) { |
| 132 | + version = spec.rawSpec |
133 | 133 | } |
134 | 134 | } |
135 | 135 |
|
|
0 commit comments