4
4
ManifestDependencyFieldNames as PackageManifestDependenciesFieldNames ,
5
5
} from '@metamask/action-utils' ;
6
6
import { isPlainObject } from '@metamask/utils' ;
7
+ import validateNPMPackageName from 'validate-npm-package-name' ;
7
8
import { readJsonObjectFile } from './fs.js' ;
8
9
import { isTruthyString } from './misc-utils.js' ;
9
10
import { semver , SemVer } from './semver.js' ;
@@ -144,8 +145,10 @@ function isValidPackageManifestVersionField(
144
145
145
146
/**
146
147
* 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.
149
152
*
150
153
* @param version - The value to check.
151
154
* @returns `true` if the version is a valid string that either
@@ -155,9 +158,35 @@ function isValidPackageManifestVersionField(
155
158
function isValidPackageManifestDependencyValue (
156
159
version : unknown ,
157
160
) : 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
+ }
161
190
}
162
191
163
192
/**
0 commit comments