(pattern: ReadonlyArray<string>)
| 520 | // Port of Go's `filepath.Match(pattern, ".")`, which `PatternMatcher` uses to |
| 521 | // reject bad syntax. Only chunks reached before a mismatch are checked. |
| 522 | const isGoMatchSyntax = (pattern: ReadonlyArray<string>): boolean => { |
| 523 | // an escaped class member followed by more of the chunk, or `undefined` |
| 524 | const member = ( |
| 525 | chunk: ReadonlyArray<string>, |
| 526 | at: number |
| 527 | ): { readonly code: number; readonly end: number } | undefined => { |
| 528 | if (at >= chunk.length || chunk[at] === "-" || chunk[at] === "]") { |
| 529 | return undefined |
| 530 | } |
| 531 | if (chunk[at] === "\\" && ++at >= chunk.length) { |
| 532 | return undefined |
| 533 | } |
| 534 | return at + 1 < chunk.length ? { code: codePoint(chunk[at]), end: at + 1 } : undefined |
| 535 | } |
| 536 | // the rest of `name` after a match, `false` on a mismatch, `undefined` on bad syntax |
| 537 | const matchChunk = (chunk: ReadonlyArray<string>, name: ReadonlyArray<string>): number | false | undefined => { |
| 538 | let failed = false |
| 539 | let k = 0 |
| 540 | for (let c = 0; c < chunk.length;) { |
| 541 | failed ||= k === name.length |
| 542 | const char = chunk[c] |
| 543 | if (char === "[") { |
| 544 | const code: number = failed ? -1 : codePoint(name[k++]) |
| 545 | c++ |
| 546 | const negated = chunk[c] === "^" |
| 547 | if (negated) { |
| 548 | c++ |
| 549 | } |
| 550 | let matched = false |
| 551 | for (let ranges = 0; !(chunk[c] === "]" && ranges > 0); ranges++) { |
| 552 | const lo = member(chunk, c) |
| 553 | if (lo === undefined) { |
| 554 | return undefined |
| 555 | } |
| 556 | let hi = lo |
| 557 | if (chunk[lo.end] === "-") { |
| 558 | const parsed = member(chunk, lo.end + 1) |
| 559 | if (parsed === undefined) { |
| 560 | return undefined |
| 561 | } |
| 562 | hi = parsed |
| 563 | } |
| 564 | matched ||= lo.code <= code && code <= hi.code |
| 565 | c = hi.end |
| 566 | } |
| 567 | c++ |
| 568 | failed ||= matched === negated |
| 569 | } else if (char === "?") { |
| 570 | if (!failed) { |
| 571 | failed = name[k++] === "/" |
| 572 | } |
| 573 | c++ |
| 574 | } else { |
| 575 | if (char === "\\" && ++c === chunk.length) { |
| 576 | return undefined |
| 577 | } |
| 578 | if (!failed) { |
| 579 | failed = chunk[c] !== name[k++] |
no test coverage detected