44 ManifestDependencyFieldNames as PackageManifestDependenciesFieldNames ,
55} from '@metamask/action-utils' ;
66import { isPlainObject } from '@metamask/utils' ;
7+ import validateNPMPackageName from 'validate-npm-package-name' ;
78import { readJsonObjectFile } from './fs.js' ;
89import { isTruthyString } from './misc-utils.js' ;
910import { semver , SemVer } from './semver.js' ;
@@ -144,8 +145,10 @@ function isValidPackageManifestVersionField(
144145
145146/**
146147 * Type guard to ensure that the provided version value is a valid dependency version
147- * specifier for a package manifest. This function validates both semantic versioning
148- * ranges and the special 'workspace:^' notation.
148+ * specifier for a package manifest. This function validates:
149+ * semantic versioning ranges
150+ * 'workspace:^' notation
151+ * 'npm:{packageName}:{semverRange}' redirections.
149152 *
150153 * @param version - The value to check.
151154 * @returns `true` if the version is a valid string that either
@@ -155,9 +158,35 @@ function isValidPackageManifestVersionField(
155158function isValidPackageManifestDependencyValue (
156159 version : unknown ,
157160) : version is string {
158- return (
159- isValidPackageManifestVersionField ( version ) || version === 'workspace:^'
160- ) ;
161+ if ( typeof version !== 'string' ) {
162+ return false ;
163+ }
164+
165+ if (
166+ isValidPackageManifestVersionField ( version ) ||
167+ version === 'workspace:^'
168+ ) {
169+ return true ;
170+ }
171+
172+ const redirectedDependencyRegexp = / ^ n p m : ( .* ) @ ( .* ?) $ / u;
173+
174+ try {
175+ const redirectedDependencyMatch = redirectedDependencyRegexp . exec ( version ) ;
176+
177+ /* istanbul ignore if */
178+ if ( ! redirectedDependencyMatch ) {
179+ return false ;
180+ }
181+
182+ const [ , redirectedName , redirectedVersion ] = redirectedDependencyMatch ;
183+ return (
184+ validateNPMPackageName ( redirectedName ) ?. validForOldPackages &&
185+ isValidPackageManifestVersionField ( redirectedVersion )
186+ ) ;
187+ } catch ( e ) /* istanbul ignore next */ {
188+ return false ;
189+ }
161190}
162191
163192/**
0 commit comments