source: imaps-frontend/node_modules/webpack/lib/util/semver.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

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