Changeset 33517cc for node_modules/minimatch/dist/commonjs/ast.js
- Timestamp:
- 09/19/26 10:30:30 (10 days ago)
- Branches:
- finki-main, main
- Children:
- 06ebe74
- Parents:
- 62b2964
- File:
-
- 1 edited
-
node_modules/minimatch/dist/commonjs/ast.js (modified) (19 diffs)
Legend:
- Unmodified
- Added
- Removed
-
node_modules/minimatch/dist/commonjs/ast.js
r62b2964 r33517cc 1 1 "use strict"; 2 2 // parse a single path portion 3 var _a; 3 4 Object.defineProperty(exports, "__esModule", { value: true }); 4 5 exports.AST = void 0; … … 7 8 const types = new Set(['!', '?', '+', '*', '@']); 8 9 const isExtglobType = (c) => types.has(c); 10 const isExtglobAST = (c) => isExtglobType(c.type); 11 // Map of which extglob types can adopt the children of a nested extglob 12 // 13 // anything but ! can adopt a matching type: 14 // +(a|+(b|c)|d) => +(a|b|c|d) 15 // *(a|*(b|c)|d) => *(a|b|c|d) 16 // @(a|@(b|c)|d) => @(a|b|c|d) 17 // ?(a|?(b|c)|d) => ?(a|b|c|d) 18 // 19 // * can adopt anything, because 0 or repetition is allowed 20 // *(a|?(b|c)|d) => *(a|b|c|d) 21 // *(a|+(b|c)|d) => *(a|b|c|d) 22 // *(a|@(b|c)|d) => *(a|b|c|d) 23 // 24 // + can adopt @, because 1 or repetition is allowed 25 // +(a|@(b|c)|d) => +(a|b|c|d) 26 // 27 // + and @ CANNOT adopt *, because 0 would be allowed 28 // +(a|*(b|c)|d) => would match "", on *(b|c) 29 // @(a|*(b|c)|d) => would match "", on *(b|c) 30 // 31 // + and @ CANNOT adopt ?, because 0 would be allowed 32 // +(a|?(b|c)|d) => would match "", on ?(b|c) 33 // @(a|?(b|c)|d) => would match "", on ?(b|c) 34 // 35 // ? can adopt @, because 0 or 1 is allowed 36 // ?(a|@(b|c)|d) => ?(a|b|c|d) 37 // 38 // ? and @ CANNOT adopt * or +, because >1 would be allowed 39 // ?(a|*(b|c)|d) => would match bbb on *(b|c) 40 // @(a|*(b|c)|d) => would match bbb on *(b|c) 41 // ?(a|+(b|c)|d) => would match bbb on +(b|c) 42 // @(a|+(b|c)|d) => would match bbb on +(b|c) 43 // 44 // ! CANNOT adopt ! (nothing else can either) 45 // !(a|!(b|c)|d) => !(a|b|c|d) would fail to match on b (not not b|c) 46 // 47 // ! can adopt @ 48 // !(a|@(b|c)|d) => !(a|b|c|d) 49 // 50 // ! CANNOT adopt * 51 // !(a|*(b|c)|d) => !(a|b|c|d) would match on bbb, not allowed 52 // 53 // ! CANNOT adopt + 54 // !(a|+(b|c)|d) => !(a|b|c|d) would match on bbb, not allowed 55 // 56 // ! CANNOT adopt ? 57 // x!(a|?(b|c)|d) => x!(a|b|c|d) would fail to match "x" 58 const adoptionMap = new Map([ 59 ['!', ['@']], 60 ['?', ['?', '@']], 61 ['@', ['@']], 62 ['*', ['*', '+', '?', '@']], 63 ['+', ['+', '@']], 64 ]); 65 // nested extglobs that can be adopted in, but with the addition of 66 // a blank '' element. 67 const adoptionWithSpaceMap = new Map([ 68 ['!', ['?']], 69 ['@', ['?']], 70 ['+', ['?', '*']], 71 ]); 72 // union of the previous two maps 73 const adoptionAnyMap = new Map([ 74 ['!', ['?', '@']], 75 ['?', ['?', '@']], 76 ['@', ['?', '@']], 77 ['*', ['*', '+', '?', '@']], 78 ['+', ['+', '@', '?', '*']], 79 ]); 80 // Extglobs that can take over their parent if they are the only child 81 // the key is parent, value maps child to resulting extglob parent type 82 // '@' is omitted because it's a special case. An `@` extglob with a single 83 // member can always be usurped by that subpattern. 84 const usurpMap = new Map([ 85 ['!', new Map([['!', '@']])], 86 [ 87 '?', 88 new Map([ 89 ['*', '*'], 90 ['+', '*'], 91 ]), 92 ], 93 [ 94 '@', 95 new Map([ 96 ['!', '!'], 97 ['?', '?'], 98 ['@', '@'], 99 ['*', '*'], 100 ['+', '+'], 101 ]), 102 ], 103 [ 104 '+', 105 new Map([ 106 ['?', '*'], 107 ['*', '*'], 108 ]), 109 ], 110 ]); 9 111 // Patterns that get prepended to bind to the start of either the 10 112 // entire string, or just a single path portion, to prevent dots … … 30 132 // remove the \ chars that we added if we end up doing a nonmagic compare 31 133 // const deslash = (s: string) => s.replace(/\\(.)/g, '$1') 134 let ID = 0; 32 135 class AST { 33 136 type; … … 45 148 // (which really means one child of '') 46 149 #emptyExt = false; 150 id = ++ID; 151 get depth() { 152 return (this.#parent?.depth ?? -1) + 1; 153 } 154 [Symbol.for('nodejs.util.inspect.custom')]() { 155 return { 156 '@@type': 'AST', 157 id: this.id, 158 type: this.type, 159 root: this.#root.id, 160 parent: this.#parent?.id, 161 depth: this.depth, 162 partsLength: this.#parts.length, 163 parts: this.#parts, 164 }; 165 } 47 166 constructor(type, parent, options = {}) { 48 167 this.type = type; … … 74 193 // reconstructs the pattern 75 194 toString() { 76 if (this.#toString !== undefined) 77 return this.#toString; 78 if (!this.type) { 79 return (this.#toString = this.#parts.map(p => String(p)).join('')); 80 } 81 else { 82 return (this.#toString = 83 this.type + '(' + this.#parts.map(p => String(p)).join('|') + ')'); 84 } 195 return (this.#toString !== undefined ? this.#toString 196 : !this.type ? 197 (this.#toString = this.#parts.map(p => String(p)).join('')) 198 : (this.#toString = 199 this.type + 200 '(' + 201 this.#parts.map(p => String(p)).join('|') + 202 ')')); 85 203 } 86 204 #fillNegs() { … … 124 242 /* c8 ignore start */ 125 243 if (typeof p !== 'string' && 126 !(p instanceof AST&& p.#parent === this)) {244 !(p instanceof _a && p.#parent === this)) { 127 245 throw new Error('invalid part: ' + p); 128 246 } … … 158 276 for (let i = 0; i < this.#parentIndex; i++) { 159 277 const pp = p.#parts[i]; 160 if (!(pp instanceof AST&& pp.type === '!')) {278 if (!(pp instanceof _a && pp.type === '!')) { 161 279 return false; 162 280 } … … 186 304 } 187 305 clone(parent) { 188 const c = new AST(this.type, parent);306 const c = new _a(this.type, parent); 189 307 for (const p of this.#parts) { 190 308 c.copyIn(p); … … 192 310 return c; 193 311 } 194 static #parseAST(str, ast, pos, opt) { 312 static #parseAST(str, ast, pos, opt, extDepth) { 313 const maxDepth = opt.maxExtglobRecursion ?? 2; 195 314 let escaping = false; 196 315 let inBrace = false; … … 229 348 continue; 230 349 } 231 if (!opt.noext && isExtglobType(c) && str.charAt(i) === '(') { 350 // we don't have to check for adoption here, because that's 351 // done at the other recursion point. 352 const doRecurse = !opt.noext && 353 isExtglobType(c) && 354 str.charAt(i) === '(' && 355 extDepth <= maxDepth; 356 if (doRecurse) { 232 357 ast.push(acc); 233 358 acc = ''; 234 const ext = new AST(c, ast);235 i = AST.#parseAST(str, ext, i, opt);359 const ext = new _a(c, ast); 360 i = _a.#parseAST(str, ext, i, opt, extDepth + 1); 236 361 ast.push(ext); 237 362 continue; … … 245 370 // find the next | or ) 246 371 let i = pos + 1; 247 let part = new AST(null, ast);372 let part = new _a(null, ast); 248 373 const parts = []; 249 374 let acc = ''; … … 276 401 continue; 277 402 } 278 if (isExtglobType(c) && str.charAt(i) === '(') { 403 const doRecurse = !opt.noext && 404 isExtglobType(c) && 405 str.charAt(i) === '(' && 406 /* c8 ignore start - the maxDepth is sufficient here */ 407 (extDepth <= maxDepth || (ast && ast.#canAdoptType(c))); 408 /* c8 ignore stop */ 409 if (doRecurse) { 410 const depthAdd = ast && ast.#canAdoptType(c) ? 0 : 1; 279 411 part.push(acc); 280 412 acc = ''; 281 const ext = new AST(c, part);413 const ext = new _a(c, part); 282 414 part.push(ext); 283 i = AST.#parseAST(str, ext, i, opt);415 i = _a.#parseAST(str, ext, i, opt, extDepth + depthAdd); 284 416 continue; 285 417 } … … 288 420 acc = ''; 289 421 parts.push(part); 290 part = new AST(null, ast);422 part = new _a(null, ast); 291 423 continue; 292 424 } … … 310 442 return i; 311 443 } 444 #canAdoptWithSpace(child) { 445 return this.#canAdopt(child, adoptionWithSpaceMap); 446 } 447 #canAdopt(child, map = adoptionMap) { 448 if (!child || 449 typeof child !== 'object' || 450 child.type !== null || 451 child.#parts.length !== 1 || 452 this.type === null) { 453 return false; 454 } 455 const gc = child.#parts[0]; 456 if (!gc || typeof gc !== 'object' || gc.type === null) { 457 return false; 458 } 459 return this.#canAdoptType(gc.type, map); 460 } 461 #canAdoptType(c, map = adoptionAnyMap) { 462 return !!map.get(this.type)?.includes(c); 463 } 464 #adoptWithSpace(child, index) { 465 const gc = child.#parts[0]; 466 const blank = new _a(null, gc, this.options); 467 blank.#parts.push(''); 468 gc.push(blank); 469 this.#adopt(child, index); 470 } 471 #adopt(child, index) { 472 const gc = child.#parts[0]; 473 this.#parts.splice(index, 1, ...gc.#parts); 474 for (const p of gc.#parts) { 475 if (typeof p === 'object') 476 p.#parent = this; 477 } 478 this.#toString = undefined; 479 } 480 #canUsurpType(c) { 481 const m = usurpMap.get(this.type); 482 return !!m?.has(c); 483 } 484 #canUsurp(child) { 485 if (!child || 486 typeof child !== 'object' || 487 child.type !== null || 488 child.#parts.length !== 1 || 489 this.type === null || 490 this.#parts.length !== 1) { 491 return false; 492 } 493 const gc = child.#parts[0]; 494 if (!gc || typeof gc !== 'object' || gc.type === null) { 495 return false; 496 } 497 return this.#canUsurpType(gc.type); 498 } 499 #usurp(child) { 500 const m = usurpMap.get(this.type); 501 const gc = child.#parts[0]; 502 const nt = m?.get(gc.type); 503 /* c8 ignore start - impossible */ 504 if (!nt) 505 return false; 506 /* c8 ignore stop */ 507 this.#parts = gc.#parts; 508 for (const p of this.#parts) { 509 if (typeof p === 'object') { 510 p.#parent = this; 511 } 512 } 513 this.type = nt; 514 this.#toString = undefined; 515 this.#emptyExt = false; 516 } 312 517 static fromGlob(pattern, options = {}) { 313 const ast = new AST(null, undefined, options);314 AST.#parseAST(pattern, ast, 0, options);518 const ast = new _a(null, undefined, options); 519 _a.#parseAST(pattern, ast, 0, options, 0); 315 520 return ast; 316 521 } … … 416 621 toRegExpSource(allowDot) { 417 622 const dot = allowDot ?? !!this.#options.dot; 418 if (this.#root === this) 623 if (this.#root === this) { 624 this.#flatten(); 419 625 this.#fillNegs(); 420 if (!this.type) { 626 } 627 if (!isExtglobAST(this)) { 421 628 const noEmpty = this.isStart() && 422 629 this.isEnd() && … … 425 632 .map(p => { 426 633 const [re, _, hasMagic, uflag] = typeof p === 'string' ? 427 AST.#parseGlob(p, this.#hasMagic, noEmpty)634 _a.#parseGlob(p, this.#hasMagic, noEmpty) 428 635 : p.toRegExpSource(allowDot); 429 636 this.#hasMagic = this.#hasMagic || hasMagic; … … 487 694 // the entire path portion. 488 695 const s = this.toString(); 489 this.#parts = [s]; 490 this.type = null; 491 this.#hasMagic = undefined; 696 const me = this; 697 me.#parts = [s]; 698 me.type = null; 699 me.#hasMagic = undefined; 492 700 return [s, (0, unescape_js_1.unescape)(this.toString()), false, false]; 493 701 } 494 // XXX abstract out this map method495 702 let bodyDotAllowed = !repeated || allowDot || dot || !startNoDot ? 496 703 '' … … 528 735 ]; 529 736 } 737 #flatten() { 738 if (!isExtglobAST(this)) { 739 for (const p of this.#parts) { 740 if (typeof p === 'object') { 741 p.#flatten(); 742 } 743 } 744 } 745 else { 746 // do up to 10 passes to flatten as much as possible 747 let iterations = 0; 748 let done = false; 749 do { 750 done = true; 751 for (let i = 0; i < this.#parts.length; i++) { 752 const c = this.#parts[i]; 753 if (typeof c === 'object') { 754 c.#flatten(); 755 if (this.#canAdopt(c)) { 756 done = false; 757 this.#adopt(c, i); 758 } 759 else if (this.#canAdoptWithSpace(c)) { 760 done = false; 761 this.#adoptWithSpace(c, i); 762 } 763 else if (this.#canUsurp(c)) { 764 done = false; 765 this.#usurp(c); 766 } 767 } 768 } 769 } while (!done && ++iterations < 10); 770 } 771 this.#toString = undefined; 772 } 530 773 #partsToRegExp(dot) { 531 774 return this.#parts … … 599 842 } 600 843 exports.AST = AST; 844 _a = AST; 601 845 //# sourceMappingURL=ast.js.map
Note:
See TracChangeset
for help on using the changeset viewer.
