UNPKG

15.5 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8/** @typedef {(string|number|undefined|[])[]} SemVerRange */
9
10/**
11 * @param {string} str version string
12 * @returns {(string|number|undefined|[])[]} parsed version
13 */
14const parseVersion = str => {
15 var splitAndConvert = function (str) {
16 return str.split(".").map(function (item) {
17 // eslint-disable-next-line eqeqeq
18 return +item == item ? +item : item;
19 });
20 };
21 var match = /^([^-+]+)?(?:-([^+]+))?(?:\+(.+))?$/.exec(str);
22 /** @type {(string|number|undefined|[])[]} */
23 var ver = match[1] ? splitAndConvert(match[1]) : [];
24 if (match[2]) {
25 ver.length++;
26 ver.push.apply(ver, splitAndConvert(match[2]));
27 }
28 if (match[3]) {
29 ver.push([]);
30 ver.push.apply(ver, splitAndConvert(match[3]));
31 }
32 return ver;
33};
34exports.parseVersion = parseVersion;
35
36/* eslint-disable eqeqeq */
37/**
38 * @param {string} a version
39 * @param {string} b version
40 * @returns {boolean} true, iff a < b
41 */
42const versionLt = (a, b) => {
43 // @ts-expect-error
44 a = parseVersion(a);
45 // @ts-expect-error
46 b = parseVersion(b);
47 var i = 0;
48 for (;;) {
49 // a b EOA object undefined number string
50 // EOA a == b a < b b < a a < b a < b
51 // object b < a (0) b < a a < b a < b
52 // undefined a < b a < b (0) a < b a < b
53 // number b < a b < a b < a (1) a < b
54 // string b < a b < a b < a b < a (1)
55 // EOA end of array
56 // (0) continue on
57 // (1) compare them via "<"
58
59 // Handles first row in table
60 if (i >= a.length) return i < b.length && (typeof b[i])[0] != "u";
61
62 var aValue = a[i];
63 var aType = (typeof aValue)[0];
64
65 // Handles first column in table
66 if (i >= b.length) return aType == "u";
67
68 var bValue = b[i];
69 var bType = (typeof bValue)[0];
70
71 if (aType == bType) {
72 if (aType != "o" && aType != "u" && aValue != bValue) {
73 return aValue < bValue;
74 }
75 i++;
76 } else {
77 // Handles remaining cases
78 if (aType == "o" && bType == "n") return true;
79 return bType == "s" || aType == "u";
80 }
81 }
82};
83/* eslint-enable eqeqeq */
84exports.versionLt = versionLt;
85
86/**
87 * @param {string} str range string
88 * @returns {SemVerRange} parsed range
89 */
90exports.parseRange = str => {
91 const splitAndConvert = str => {
92 return str.split(".").map(item => (`${+item}` === item ? +item : item));
93 };
94 // see https://docs.npmjs.com/misc/semver#range-grammar for grammar
95 const parsePartial = str => {
96 const match = /^([^-+]+)?(?:-([^+]+))?(?:\+(.+))?$/.exec(str);
97 /** @type {(string|number|undefined|[])[]} */
98 const ver = match[1] ? [0, ...splitAndConvert(match[1])] : [0];
99 if (match[2]) {
100 ver.length++;
101 ver.push.apply(ver, splitAndConvert(match[2]));
102 }
103
104 // remove trailing any matchers
105 let last = ver[ver.length - 1];
106 while (
107 ver.length &&
108 (last === undefined || /^[*xX]$/.test(/** @type {string} */ (last)))
109 ) {
110 ver.pop();
111 last = ver[ver.length - 1];
112 }
113
114 return ver;
115 };
116 const toFixed = range => {
117 if (range.length === 1) {
118 // Special case for "*" is "x.x.x" instead of "="
119 return [0];
120 } else if (range.length === 2) {
121 // Special case for "1" is "1.x.x" instead of "=1"
122 return [1, ...range.slice(1)];
123 } else if (range.length === 3) {
124 // Special case for "1.2" is "1.2.x" instead of "=1.2"
125 return [2, ...range.slice(1)];
126 } else {
127 return [range.length, ...range.slice(1)];
128 }
129 };
130 const negate = range => {
131 return [-range[0] - 1, ...range.slice(1)];
132 };
133 const parseSimple = str => {
134 // simple ::= primitive | partial | tilde | caret
135 // primitive ::= ( '<' | '>' | '>=' | '<=' | '=' ) partial
136 // tilde ::= '~' partial
137 // caret ::= '^' partial
138 const match = /^(\^|~|<=|<|>=|>|=|v|!)/.exec(str);
139 const start = match ? match[0] : "";
140 const remainder = parsePartial(str.slice(start.length));
141 switch (start) {
142 case "^":
143 if (remainder.length > 1 && remainder[1] === 0) {
144 if (remainder.length > 2 && remainder[2] === 0) {
145 return [3, ...remainder.slice(1)];
146 }
147 return [2, ...remainder.slice(1)];
148 }
149 return [1, ...remainder.slice(1)];
150 case "~":
151 return [2, ...remainder.slice(1)];
152 case ">=":
153 return remainder;
154 case "=":
155 case "v":
156 case "":
157 return toFixed(remainder);
158 case "<":
159 return negate(remainder);
160 case ">": {
161 // and( >=, not( = ) ) => >=, =, not, and
162 const fixed = toFixed(remainder);
163 // eslint-disable-next-line no-sparse-arrays
164 return [, fixed, 0, remainder, 2];
165 }
166 case "<=":
167 // or( <, = ) => <, =, or
168 // eslint-disable-next-line no-sparse-arrays
169 return [, toFixed(remainder), negate(remainder), 1];
170 case "!": {
171 // not =
172 const fixed = toFixed(remainder);
173 // eslint-disable-next-line no-sparse-arrays
174 return [, fixed, 0];
175 }
176 default:
177 throw new Error("Unexpected start value");
178 }
179 };
180 const combine = (items, fn) => {
181 if (items.length === 1) return items[0];
182 const arr = [];
183 for (const item of items.slice().reverse()) {
184 if (0 in item) {
185 arr.push(item);
186 } else {
187 arr.push(...item.slice(1));
188 }
189 }
190 // eslint-disable-next-line no-sparse-arrays
191 return [, ...arr, ...items.slice(1).map(() => fn)];
192 };
193 const parseRange = str => {
194 // range ::= hyphen | simple ( ' ' simple ) * | ''
195 // hyphen ::= partial ' - ' partial
196 const items = str.split(" - ");
197 if (items.length === 1) {
198 const items = str.trim().split(/\s+/g).map(parseSimple);
199 return combine(items, 2);
200 }
201 const a = parsePartial(items[0]);
202 const b = parsePartial(items[1]);
203 // >=a <=b => and( >=a, or( <b, =b ) ) => >=a, <b, =b, or, and
204 // eslint-disable-next-line no-sparse-arrays
205 return [, toFixed(b), negate(b), 1, a, 2];
206 };
207 const parseLogicalOr = str => {
208 // range-set ::= range ( logical-or range ) *
209 // logical-or ::= ( ' ' ) * '||' ( ' ' ) *
210 const items = str.split(/\s*\|\|\s*/).map(parseRange);
211 return combine(items, 1);
212 };
213 return parseLogicalOr(str);
214};
215
216/* eslint-disable eqeqeq */
217const rangeToString = range => {
218 var fixCount = range[0];
219 var str = "";
220 if (range.length === 1) {
221 return "*";
222 } else if (fixCount + 0.5) {
223 str +=
224 fixCount == 0
225 ? ">="
226 : fixCount == -1
227 ? "<"
228 : fixCount == 1
229 ? "^"
230 : fixCount == 2
231 ? "~"
232 : fixCount > 0
233 ? "="
234 : "!=";
235 var needDot = 1;
236 // eslint-disable-next-line no-redeclare
237 for (var i = 1; i < range.length; i++) {
238 var item = range[i];
239 var t = (typeof item)[0];
240 needDot--;
241 str +=
242 t == "u"
243 ? // undefined: prerelease marker, add an "-"
244 "-"
245 : // number or string: add the item, set flag to add an "." between two of them
246 (needDot > 0 ? "." : "") + ((needDot = 2), item);
247 }
248 return str;
249 } else {
250 var stack = [];
251 // eslint-disable-next-line no-redeclare
252 for (var i = 1; i < range.length; i++) {
253 // eslint-disable-next-line no-redeclare
254 var item = range[i];
255 stack.push(
256 item === 0
257 ? "not(" + pop() + ")"
258 : item === 1
259 ? "(" + pop() + " || " + pop() + ")"
260 : item === 2
261 ? stack.pop() + " " + stack.pop()
262 : rangeToString(item)
263 );
264 }
265 return pop();
266 }
267 function pop() {
268 return stack.pop().replace(/^\((.+)\)$/, "$1");
269 }
270};
271/* eslint-enable eqeqeq */
272exports.rangeToString = rangeToString;
273
274/* eslint-disable eqeqeq */
275/**
276 * @param {SemVerRange} range version range
277 * @param {string} version the version
278 * @returns {boolean} if version satisfy the range
279 */
280const satisfy = (range, version) => {
281 if (0 in range) {
282 // @ts-expect-error
283 version = parseVersion(version);
284 var fixCount = range[0];
285 // when negated is set it swill set for < instead of >=
286 var negated = fixCount < 0;
287 if (negated) fixCount = -fixCount - 1;
288 for (var i = 0, j = 1, isEqual = true; ; j++, i++) {
289 // cspell:word nequal nequ
290
291 // when isEqual = true:
292 // range version: EOA/object undefined number string
293 // EOA equal block big-ver big-ver
294 // undefined bigger next big-ver big-ver
295 // number smaller block cmp big-cmp
296 // fixed number smaller block cmp-fix differ
297 // string smaller block differ cmp
298 // fixed string smaller block small-cmp cmp-fix
299
300 // when isEqual = false:
301 // range version: EOA/object undefined number string
302 // EOA nequal block next-ver next-ver
303 // undefined nequal block next-ver next-ver
304 // number nequal block next next
305 // fixed number nequal block next next (this never happens)
306 // string nequal block next next
307 // fixed string nequal block next next (this never happens)
308
309 // EOA end of array
310 // equal (version is equal range):
311 // when !negated: return true,
312 // when negated: return false
313 // bigger (version is bigger as range):
314 // when fixed: return false,
315 // when !negated: return true,
316 // when negated: return false,
317 // smaller (version is smaller as range):
318 // when !negated: return false,
319 // when negated: return true
320 // nequal (version is not equal range (> resp <)): return true
321 // block (version is in different prerelease area): return false
322 // differ (version is different from fixed range (string vs. number)): return false
323 // next: continues to the next items
324 // next-ver: when fixed: return false, continues to the next item only for the version, sets isEqual=false
325 // big-ver: when fixed || negated: return false, continues to the next item only for the version, sets isEqual=false
326 // next-nequ: continues to the next items, sets isEqual=false
327 // cmp (negated === false): version < range => return false, version > range => next-nequ, else => next
328 // cmp (negated === true): version > range => return false, version < range => next-nequ, else => next
329 // cmp-fix: version == range => next, else => return false
330 // big-cmp: when negated => return false, else => next-nequ
331 // small-cmp: when negated => next-nequ, else => return false
332
333 var rangeType = j < range.length ? (typeof range[j])[0] : "";
334
335 var versionValue;
336 var versionType;
337
338 // Handles first column in both tables (end of version or object)
339 if (
340 i >= version.length ||
341 ((versionValue = version[i]),
342 (versionType = (typeof versionValue)[0]) == "o")
343 ) {
344 // Handles nequal
345 if (!isEqual) return true;
346 // Handles bigger
347 if (rangeType == "u") return j > fixCount && !negated;
348 // Handles equal and smaller: (range === EOA) XOR negated
349 return (rangeType == "") != negated; // equal + smaller
350 }
351
352 // Handles second column in both tables (version = undefined)
353 if (versionType == "u") {
354 if (!isEqual || rangeType != "u") {
355 return false;
356 }
357 }
358
359 // switch between first and second table
360 else if (isEqual) {
361 // Handle diagonal
362 if (rangeType == versionType) {
363 if (j <= fixCount) {
364 // Handles "cmp-fix" cases
365 if (versionValue != range[j]) {
366 return false;
367 }
368 } else {
369 // Handles "cmp" cases
370 if (negated ? versionValue > range[j] : versionValue < range[j]) {
371 return false;
372 }
373 if (versionValue != range[j]) isEqual = false;
374 }
375 }
376
377 // Handle big-ver
378 else if (rangeType != "s" && rangeType != "n") {
379 if (negated || j <= fixCount) return false;
380 isEqual = false;
381 j--;
382 }
383
384 // Handle differ, big-cmp and small-cmp
385 else if (j <= fixCount || versionType < rangeType != negated) {
386 return false;
387 } else {
388 isEqual = false;
389 }
390 } else {
391 // Handles all "next-ver" cases in the second table
392 if (rangeType != "s" && rangeType != "n") {
393 isEqual = false;
394 j--;
395 }
396
397 // next is applied by default
398 }
399 }
400 }
401 /** @type {(boolean | number)[]} */
402 var stack = [];
403 var p = stack.pop.bind(stack);
404 // eslint-disable-next-line no-redeclare
405 for (var i = 1; i < range.length; i++) {
406 var item = /** @type {SemVerRange | 0 | 1 | 2} */ (range[i]);
407 stack.push(
408 item == 1
409 ? p() | p()
410 : item == 2
411 ? p() & p()
412 : item
413 ? satisfy(item, version)
414 : !p()
415 );
416 }
417 return !!p();
418};
419/* eslint-enable eqeqeq */
420exports.satisfy = satisfy;
421
422exports.stringifyHoley = json => {
423 switch (typeof json) {
424 case "undefined":
425 return "";
426 case "object":
427 if (Array.isArray(json)) {
428 let str = "[";
429 for (let i = 0; i < json.length; i++) {
430 if (i !== 0) str += ",";
431 str += this.stringifyHoley(json[i]);
432 }
433 str += "]";
434 return str;
435 } else {
436 return JSON.stringify(json);
437 }
438 default:
439 return JSON.stringify(json);
440 }
441};
442
443//#region runtime code: parseVersion
444exports.parseVersionRuntimeCode = runtimeTemplate =>
445 `var parseVersion = ${runtimeTemplate.basicFunction("str", [
446 "// see webpack/lib/util/semver.js for original code",
447 `var p=${
448 runtimeTemplate.supportsArrowFunction() ? "p=>" : "function(p)"
449 }{return p.split(".").map((${
450 runtimeTemplate.supportsArrowFunction() ? "p=>" : "function(p)"
451 }{return+p==p?+p:p}))},n=/^([^-+]+)?(?:-([^+]+))?(?:\\+(.+))?$/.exec(str),r=n[1]?p(n[1]):[];return n[2]&&(r.length++,r.push.apply(r,p(n[2]))),n[3]&&(r.push([]),r.push.apply(r,p(n[3]))),r;`
452 ])}`;
453//#endregion
454
455//#region runtime code: versionLt
456exports.versionLtRuntimeCode = runtimeTemplate =>
457 `var versionLt = ${runtimeTemplate.basicFunction("a, b", [
458 "// see webpack/lib/util/semver.js for original code",
459 'a=parseVersion(a),b=parseVersion(b);for(var r=0;;){if(r>=a.length)return r<b.length&&"u"!=(typeof b[r])[0];var e=a[r],n=(typeof e)[0];if(r>=b.length)return"u"==n;var t=b[r],f=(typeof t)[0];if(n!=f)return"o"==n&&"n"==f||("s"==f||"u"==n);if("o"!=n&&"u"!=n&&e!=t)return e<t;r++}'
460 ])}`;
461//#endregion
462
463//#region runtime code: rangeToString
464exports.rangeToStringRuntimeCode = runtimeTemplate =>
465 `var rangeToString = ${runtimeTemplate.basicFunction("range", [
466 "// see webpack/lib/util/semver.js for original code",
467 'var r=range[0],n="";if(1===range.length)return"*";if(r+.5){n+=0==r?">=":-1==r?"<":1==r?"^":2==r?"~":r>0?"=":"!=";for(var e=1,a=1;a<range.length;a++){e--,n+="u"==(typeof(t=range[a]))[0]?"-":(e>0?".":"")+(e=2,t)}return n}var g=[];for(a=1;a<range.length;a++){var t=range[a];g.push(0===t?"not("+o()+")":1===t?"("+o()+" || "+o()+")":2===t?g.pop()+" "+g.pop():rangeToString(t))}return o();function o(){return g.pop().replace(/^\\((.+)\\)$/,"$1")}'
468 ])}`;
469//#endregion
470
471//#region runtime code: satisfy
472exports.satisfyRuntimeCode = runtimeTemplate =>
473 `var satisfy = ${runtimeTemplate.basicFunction("range, version", [
474 "// see webpack/lib/util/semver.js for original code",
475 'if(0 in range){version=parseVersion(version);var e=range[0],r=e<0;r&&(e=-e-1);for(var n=0,i=1,a=!0;;i++,n++){var f,s,g=i<range.length?(typeof range[i])[0]:"";if(n>=version.length||"o"==(s=(typeof(f=version[n]))[0]))return!a||("u"==g?i>e&&!r:""==g!=r);if("u"==s){if(!a||"u"!=g)return!1}else if(a)if(g==s)if(i<=e){if(f!=range[i])return!1}else{if(r?f>range[i]:f<range[i])return!1;f!=range[i]&&(a=!1)}else if("s"!=g&&"n"!=g){if(r||i<=e)return!1;a=!1,i--}else{if(i<=e||s<g!=r)return!1;a=!1}else"s"!=g&&"n"!=g&&(a=!1,i--)}}var t=[],o=t.pop.bind(t);for(n=1;n<range.length;n++){var u=range[n];t.push(1==u?o()|o():2==u?o()&o():u?satisfy(u,version):!o())}return!!o();'
476 ])}`;
477//#endregion